I’m trying to use a timer to send an email every minute. The information for each email is taken from values for each row of components I have on a form. I can send the emails without using the timer, so I know the email method works. But when I try to implement the timer, the emails do not get sent. I read the docs on the timer class, and I believe this should work. But I’m not familiar with it enough to really know what to do. Here is the code:
Here is the email method:
//method to send email to outlook
public void sendEMailThroughOUTLOOK(string recipient,
string subject, string body)
{
try
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Create a new mail item.
Outlook.MailItem oMsg =
(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//add the body of the email
oMsg.Body = body;
oMsg.Subject = subject;
// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient);
oRecip.Resolve();
// Send.
oMsg.Send();
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}//end of try block
catch (Exception ex)
{
} //end of catch
} //end of Email Method
And here is the timer event:
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
foreach (rowClass row in this.rows)
{
string recipientAddress = "roomcheckstest@gmail.com";
string subjectLine = "GPC " + (string)row.buildingComboBox.SelectedItem
+ " " + (string)row.roomComboBox.SelectedItem + "-Room Check";
string senderline = "Sender=ctsstaff.ithelpcentral@ttu.edu" + "\t";
string newlinespaces = Environment.NewLine + Environment.NewLine +
Environment.NewLine + Environment.NewLine + Environment.NewLine;
string legalLastName = "Legal Last Name=" +
(string)row.buildingComboBox.SelectedItem;
string legalFirstName = "Legal First Name=" +
(string)row.roomComboBox.SelectedItem;
string timeLine = "Time= 15m";
string requestType = "Initial Request Type=Onsite";
string classRoomMaintenace = "Classroom maintenance. " +
"Regular Classroom weekly check.";
string closingEmailSent = "Closing Email Sent=yes";
string currentlyClosed = "Currently Closed=yes";
string assignTo = "Assign To=ITHC CTS Staff";
string typeLine = "Type=Hardware";
string category = "Category=Internal Component";
string subCategory = "Subcategory=Performance";
string agentType = "Type (Agent)=Hardware";
string agentCategory = "Type (Agent)=Hardware";
string subCategoryAgent = "Subcategory (Agent)=Performance";
string labelLine = "Label=Service Request";
string status = "Status=Closed";
string finalbody = senderline + newlinespaces + legalLastName
+ newlinespaces + legalFirstName + newlinespaces + timeLine
+ newlinespaces + requestType + newlinespaces + classRoomMaintenace
+ newlinespaces + closingEmailSent + newlinespaces
+ currentlyClosed + newlinespaces + assignTo + newlinespaces
+ typeLine + newlinespaces + category + newlinespaces
+ subCategory + newlinespaces + agentType + newlinespaces
+ agentCategory + newlinespaces + subCategoryAgent
+ newlinespaces + labelLine + newlinespaces + status;
sendEMailThroughOUTLOOK(recipientAddress, subjectLine, finalbody);
MessageBox.Show("email");
//Thread.Sleep(60000);
//sendEMailThroughOUTLOOK(recipientAddress, subjectLine, finalbody);
}
}
Here is where the timer is created inside a send email button on the form, which should in theory call the email method:
private void submitButton_Click(object sender, EventArgs e)
{
if (rows[0].buildingComboBox.SelectedIndex > -1)
{
System.Timers.Timer time = new System.Timers.Timer();
time.Interval = 300000;
time.Enabled = true;
time.Elapsed += new ElapsedEventHandler(OnTimedEvent);
MessageBox.Show("Issues Sent");
}
}
I send an email for each row of components on the form (the user adds rows as needed)
The email is sent through outlook (it has to be sent through outlook using exchange server)
I need to delay it because the server will not accept them all at once. My understanding of the timer class must be flawed. I cant figure out why this isn’t working.
It looks like you need to call
.Start()on theTimer.Note that nothing will happen until the timer ticks, so the first e-mail won’t go out immediately.
Also, your timer interval is set to 5 minutes (300,000ms) instead of 1 (60,000ms).