我有一個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));
}
}
發表您的全部代碼。 –
請不要包含關於問題標題中使用的語言的信息,除非在沒有它的情況下沒有意義。標籤用於此目的。 –