2009-06-24 58 views
0

我在我的WinForm應用程序編碼在C#中有一些奇怪的行爲。c#從窗體窗體發送郵件不觸發

我: private void buttonSave_Click(object sender, EventArgs e)

林打電話給我的功能: functions.sendStatusEmail();

奇怪的是,當我按下保存按鈕,然後郵件發送不會被觸發。但是,如果我關閉我的應用程序,郵件將被處理併發送。

我是否遺漏了一些東西,或者需要手動觸發som應用程序事件來運行sendout。

(我嘗試使用client.SendAsync(mail,null);那麼triggerd在點擊,但郵件是空的)

在此先感謝

- 編輯:代碼expamples

private void buttonSave_Click(object sender, EventArgs e) 
{ 
    // checks if a ticket is active 
    if (workingTicketId > 0) 
    { 
     // update ticket information 
     functions.updateTicketInfo(workingTicketId, comboBoxPriority.SelectedIndex, 
            comboBoxStatus.SelectedIndex, textBoxComment.Text); 

     // gives feedback 
     labelFeedback.Text = "Updated"; 

     // updates the active ticket list 
     populateActiveTicketList(); 

     // marks working ticket row in list 
     dataGridActiveTicketList.Rows[workingGridIndex].Selected = true; 

     // checks for change of ticket status 
     if (comboBoxStatus.SelectedIndex != workingTicketStatus) 
     { 
      // checks if contact person exists 
      if (labelContactPersonValue.Text.ToString() != "") 
      { 
       // sends email to contact person 
       functions.sendStatusEmail(labelContactPersonValue.Text, comboBoxStatus.SelectedIndex, workingTicketId, textBoxDescription.Text); 
      } 

      // updates working ticket status 
      workingTicketStatus = comboBoxStatus.SelectedIndex; 
     } 

    } 
} 

和發送電子郵件功能:

// sends a status email to contact person 
// returns noting 
public void sendStatusEmail(string email, int newStatus, int ticketId, string ticketText) 
{ 
    // defines variables 
    string emailSubject; 
    string emailBody; 


    // some exkluded mailcontent handling 

    // sends mail 
    MailMessage mail = new MailMessage("[email protected]n.com",email,emailSubject,emailBody); 
    SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["MailSMTP"]); 

    mail.IsBodyHtml = true; 
    client.Send(mail); 

    // dispose 
    mail.Dispose(); 
} 
+3

郵政sendStatusEmail() – Hemant 2009-06-24 08:35:04

+0

的代碼......並且也採取了buttonSave_Click – 2009-06-24 08:38:55

回答

1

無法打開說明爲什麼它不起作用。我用下面的功能併成功發送電子郵件:

public static bool SendEmail (string smtpServer, string fromAddress, string fromDisplayName, 
    string toAddress, string subject, string contents, bool important) { 

    MailAddress from = new MailAddress (fromAddress, fromDisplayName); 
    MailPriority priority = important ? MailPriority.High : MailPriority.Normal; 

    MailMessage m = new MailMessage { 
     From = from, 
     Subject = subject, 
     Body = contents, 
     Priority = priority, 
     IsBodyHtml = false 
    }; 

    MailAddress to = new MailAddress (toAddress); 
    m.To.Add (to); 

    SmtpClient c = new SmtpClient (smtpServer) { UseDefaultCredentials = false }; 

    c.Send (m); 
    return true; 
} 

沒有違法,但你確定它關閉,導致電子郵件發送的申請。大多數情況下,由於SMTP服務器上的流量,發送電子郵件和收到電子郵件之間存在延遲。嘗試按下該按鈕,然後等待一段時間(3-4分鐘),然後嘗試在此期間刷新收件箱。

1

順便說一句,SendAsync沒有用,因爲你在異步調用之後調用了mail.dispose()。 做到這一點在異步是正確的做法,

private void button1_Click(object sender, EventArgs e) 
{ 
    MailMessage mail = new MailMessage("[email protected]", "[email protected]", "subj", "body"); 
    SmtpClient client = new SmtpClient("SMTPHOST"); 
    mail.IsBodyHtml = true; 
    client.SendCompleted += new 
    SendCompletedEventHandler(SendCompletedCallback); 

    client.SendAsync(mail,mail);//send the mail object itself as argument to callback 

} 

private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) 
{ 
    if (e.Cancelled) 
    { 
     //Mail sending is cancelled 
    } 
    if (e.Error != null) 
    { 
     //There is an error,e.Error will contain the exception 
    } 
    else 
    { 
     //Do any other success processing 
    } 

    ((MailMessage)e.UserState).Dispose();//Dispose 
}