How to send email at a specified time?

  • 19 October 2005
  • 2 replies
  • 4 views

Badge +4
I have a process at the end of which, an email needs to been sent to several people as a reminder for an upcoming event.

The email won't be sent till a certain days, say 2 days, before the event date.

How can I control this in the K2 Studio? Should I use a timer or a loop to keep the K2 checking the time and when the it reaches the right time, send the email.

The following is the K2 code for a server mail event. I suppose we should do the looping in this section of code:

public void Main(ServerEventContext K2)
{
SMTPFunction(K2);
}
public void SMTPFunction(ServerEventContext K2)
{
System.Web.Mail.MailMessage objMsg = new System.Web.Mail.MailMessage();
System.Web.Mail.MailAttachment objAttachment;
object strFrom = "";
object strEmail = "";
object strSubject = "";
object strBody = "";
object strMailServer = "";
strMailServer = K2.StringTable["SMTP Server"];
System.Web.Mail.SmtpMail.SmtpServer = strMailServer.ToString();
strFrom = K2.ProcessInstance.Originator.Email;
strEmail = "abc@abc.com"! ;
if((strEmail==null)||(strEmail.ToString().Trim().Length == 0))
{
throw new System.Exception("No Email Address was supplied, please rectify and try again.");
}
strSubject = "Notification";
strBody = "There is an event on:" + System.Environment.NewLine + "" + System.Environment.NewLine + "Thanks," + System.Environment.NewLine + "" + K2.ProcessInstance.Originator.Name + "" + System.Environment.NewLine + "" + K2.ProcessInstance.Originator.Email;
objMsg.From = strFrom.ToString();
objMsg.Subject = strSubject.ToString();
objMsg.Body = strBody.ToString();
objMsg.To = strEmail.ToString();
objMsg.Subject = strSubject.ToString();
objMsg.Body = strBody.ToString();
System.Web.Mail.SmtpMail.Send(objMsg);
}

2 replies

Badge +2
What about creating an Activity with the Server Mail event to specify a <Preceding Rule>, which would check the Event date time and compare it to the System.DateTime.Now to make sure that it is the right time to send the email.

I hope it helps
Badge +2
Sorry,

I meant to say:

Create a <Start Rule> associated to the mail server event and specify the datetime that you need the Activity to be executed.

By default, the Start Rule assumes that the time specified means after the preceding Activity was initially executed. So you'll have to calculate the difference in time with the expected datetime to be able to provide the right parameters for the StartRule:

K2.SetStartRule( myDateTime, 0, 0, 0, 0);

Reply