2014-02-16 61 views
1

我有一個C#和sendmail異步的奇怪問題。 當我使用正常的發送方法時,所有工作正常,我的電子郵件發送到正確的SMTP服務器。但是當我使用SendAsync-Method時,它不起作用。SendMail SendAsync

mailClient.SendCompleted += SendCompleted; 

private static void SendCompleted (object sender, AsyncCompletedEventArgs e) 
{ 
    string token = (string) e.UserState; 

    if (e.Cancelled) 
    { 
    MessageBox.Show (string.Format ("{0}\n\"{1}\"", Application.Current.FindResource ("MailCannotSend"), token), LogFile.MSGBOX_ERROR, MessageBoxButton.OK, MessageBoxImage.Error); 
    return; 
    } 

    MessageBox.Show (e.Error != null ? string.Format ("{0}\n\"{1}\"", e.Error, token) : "Complete!", LogFile.MSGBOX_ERROR, MessageBoxButton.OK, MessageBoxImage.Error); 
} 

SendAsync-Method總是在e.Cancelled內運行,沒有任何異常或錯誤。我真的不知道爲什麼。

有沒有人一個提示,線索?謝謝!

更新

下面是完整的代碼:

public void InitClient() 
{ 
    try 
    { 
    mailClient = new SmtpClient (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.SmtpServerName, SettingsHelper.TailSettings.AlertSettings.SmtpSettings.SmtpPort) 
    { 
     UseDefaultCredentials = false, 
     Timeout = 30000, 
     DeliveryMethod = SmtpDeliveryMethod.Network 
    }; 
    string decryptPassword = StringEncryption.Decrypt (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.Password, LogFile.ENCRYPT_PASSPHRASE); 
    NetworkCredential authInfo = new NetworkCredential (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.LoginName, decryptPassword); 
    mailClient.Credentials = authInfo; 
    mailClient.SendCompleted += SendCompleted; 

    if (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.SSL) 
     mailClient.EnableSsl = true; 
    if (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.TLS) 
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 

    MailAddress from = new MailAddress (SettingsHelper.TailSettings.AlertSettings.SmtpSettings.FromAddress); 
    MailAddress to = new MailAddress (SettingsHelper.TailSettings.AlertSettings.EMailAddress); 

    mailMessage = new MailMessage (from, to) 
    { 
     Subject = SettingsHelper.TailSettings.AlertSettings.SmtpSettings.Subject, 
     SubjectEncoding = System.Text.Encoding.UTF8, 
     BodyEncoding = System.Text.Encoding.UTF8, 
     IsBodyHtml = false 
    }; 
} 
    catch (Exception ex) 
    { 
    ErrorLog.WriteLog (ErrorFlags.Error, GetType ().Name, string.Format ("{1}, exception: {0}", ex, System.Reflection.MethodBase.GetCurrentMethod ().Name)); 
    } 
} 

/// <summary> 
/// Send E-Mail 
/// </summary> 
/// <param name="userToken">User token</param> 
/// <param name="bodyMessage">Message to be send</param> 
public void SendMail (string userToken, string bodyMessage = null) 
{ 
    try 
    { 
    string userState = userToken; 

    if (bodyMessage != null) 
     mailMessage.Body = bodyMessage; 

    if (String.Compare (userState, "testMessage", StringComparison.Ordinal) == 0) 
     mailMessage.Body = string.Format ("Testmail from {0}", LogFile.APPLICATION_CAPTION); 

    mailClient.SendAsync (mailMessage, userToken); 

    if (String.Compare (userState, "testMessage", StringComparison.Ordinal) == 0) 
     return; 
} 
    catch (Exception ex) 
    { 
    ErrorLog.WriteLog (ErrorFlags.Error, GetType ().Name, string.Format ("{1}, exception: {0}", ex, System.Reflection.MethodBase.GetCurrentMethod ().Name)); 
    } 
} 
+0

發表您的全部代碼。 –

+0

請不要包含關於問題標題中使用的語言的信息,除非在沒有它的情況下沒有意義。標籤用於此目的。 –

回答

1

SendAsync是異步的,它在未來完成。顯然,在這裏你的電話SendMail結束之前SendCompleted方法的範圍內被激發。例如,在您有機會處理此事件並處理完成異步操作之前,控制檯應用程序可能會簡單地結束。

任何進一步的建議將取決於您的應用程序的運行環境是什麼:ASP.NET,WCF,控制檯的WinForms,WPF等

+0

這是一個WPF應用程序 – simmeone

+0

@simmeone,那麼我會懷疑'SendAsync'開始的操作完成之前調用了mailClient.Dispose。 – Noseratio

+0

哼!這是一個很好的暗示,我會試一試。 – simmeone