0

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

这是电子邮件方法:

//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 发送)我需要延迟它,因为服务器不会接受它们一次全部。我对定时器类的理解一定是有缺陷的。我无法弄清楚为什么这不起作用。

4

2 回答 2

0

看来您需要调用.Start().Timer

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

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

于 2012-10-03T02:10:00.447 回答
-3

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

于 2012-10-03T02:11:31.010 回答