Question

I have a strange problem with C# and sendmail async. When I use the normal send method all works fine and my E-Mail is sending to the right SMTP-Server. But when I use the SendAsync-Method it doesn't work.

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

The SendAsync-Method runs always in the e.Cancelled stuff without any exceptions or errors. I really don't know why.

Has anyone a tip, clue? Thanks!

Update

Here is the full code:

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));
  }
}
Was it helpful?

Solution

SendAsync is asynchronous, it completes in the future. Apparently, the scope of the method where your call SendMail ends before SendCompleted is fired. For example, a console application might simply end before you have a chance to handle this event and process the completion of the asynchronous operation.

Any further suggestion would depend on what the execution environment of your application is: ASP.NET, WCF, Console, WinForms, WPF, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top