使用计时器每分钟发送一次电子邮件

时间:2012-10-03 02:05:15

标签: c# winforms timer

我正在尝试使用计时器每分钟发送一封电子邮件。每封电子邮件的信息取自我在表单上的每一行组件的值。我可以不使用计时器发送电子邮件,所以我知道电子邮件方法有效。但是当我尝试实现计时器时,电子邮件不会被发送。我阅读了计时器类的文档,我相信这应该有用。但我不熟悉它,真的不知道该怎么做。这是代码:

这是电子邮件方法:

//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

这是计时器事件:

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);
    }       
}

这是在表单上的发送电子邮件按钮内创建计时器的位置,理论上应该调用电子邮件方法:

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");
    }
}

我为表单上的每一行组件发送一封电子邮件(用户根据需要添加行) 电子邮件通过outlook发送(必须使用Exchange服务器通过outlook发送) 我需要延迟它,因为服务器不会立即接受它们。我对计时器类的理解必须有缺陷。我无法弄清楚为什么这不起作用。

2 个答案:

答案 0 :(得分:0)

您似乎需要致电.Start()上的Timer

请注意,在计时器打勾之前不会发生任何事情,因此第一封电子邮件不会立即发出。

此外,您的计时器间隔设置为5分钟(300,000毫秒)而不是1(60,000毫秒)。

答案 1 :(得分:-3)

在尝试执行此类操作时,您应该使用线程。创建一个类并让它实现Runnable。然后在实现的函数运行中,创建一个while循环,然后在最后添加Thread.sleep(60000)。