0

タイマーを使用して毎分メールを送信しようとしています。各電子メールの情報は、フォームにあるコンポーネントの各行の値から取得されます。タイマーを使用せずにメールを送信できるので、メールの方法が機能することがわかります。しかし、タイマーを実装しようとすると、メールが送信されません。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 を介して送信する必要があります) サーバーがそれらを受け入れないため、送信を遅らせる必要があります一斉に。timer クラスに関する私の理解には欠陥があるに違いありません。これが機能しない理由がわかりません。

4

2 に答える 2

0

に電話する必要があるよう.Start()ですTimer

タイマーが作動するまで何も起こらないので、最初の電子メールはすぐには送信されないことに注意してください。

また、タイマー間隔は 1 (60,000 ミリ秒) ではなく 5 分 (300,000 ミリ秒) に設定されています。

于 2012-10-03T02:10:00.447 に答える
-3

このようなことをしようとするときは、実際にスレッドを使用する必要があります。クラスを作成し、Runnable を実装します。次に、実装された関数の実行で while ループを作成し、最後に Thread.sleep(60000) を追加します。

于 2012-10-03T02:11:31.010 に答える