1

我对 C# 和 sendmail 异步有一个奇怪的问题。当我使用正常的发送方法时,一切正常,我的电子邮件正在发送到正确的 SMTP 服务器。但是当我使用 SendAsync-Method 时它不起作用。

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));
  }
}
4

1 回答 1

1

SendAsync是异步的,它将在未来完成。显然,调用之前SendMail结束的方法的范围会被触发。例如,控制台应用程序可能会在您有机会处理此事件并处理异步操作的完成之前简单地结束。 SendCompleted

任何进一步的建议将取决于您的应用程序的执行环境是什么:ASP.NET、WCF、控制台、WinForms、WPF 等。

于 2014-02-17T00:11:56.590 回答