Why SmtpClient SendAsync throws Exceptions with combinations of UseDefaultCredentials and EnableSsl, while Send works fine

StackOverflow https://stackoverflow.com/questions/15101502

  •  15-03-2022
  •  | 
  •  

Question

I'm using the System.Net.Mail. There are some topics around this area, but they are ASP.NET and often VB related. I'm isolating the code into a desktop skeleton code, I use .NET 3.5 and C#. So Send works in all scenario what I tried (EnableSsl false/true, UseDefaultCredentials false/true). But SendAsync only works if UseDefaultCredentials is not set to true (turns out that it can even matter if you explicitly set it to false, it's false by default), EnableSsl is true (OK, that can be server settings too), and hard-code my credentials. I want to be able to SendAsync using UseDefaultCredentials. Code:

void sendmail() {
  MailMessage email = new MailMessage();
  email.From = new MailAddress("tcs@software.com");
  email.To.Add("tcs@software.com");
  email.Subject = "Simple test email subject";
  email.Body = "Simple test email body";
  string mHost = "mail.software.com";
  string mUsername = @"DOMAIN\tcs";
  string mPassword = "myfreakinpassword?Really???";
  SmtpClient smtpCli = new SmtpClient(mHost);
  //smtpCli.UseDefaultCredentials = true;
  smtpCli.Credentials = new NetworkCredential(mUsername, mPassword);
  smtpCli.EnableSsl = true;
  smtpCli.SendCompleted += smtpCli_SendCompleted;
  try {
    //smtpCli.Send(email);
    smtpCli.SendAsync(email, null); // This mofo only works in this combination
  }
  catch (Exception ex) {
    Console.WriteLine(ex);
  }
}
void smtpCli_SendCompleted(object sender, AsyncCompletedEventArgs e) {
  if (e.Error != null)
    Console.WriteLine(e.Error);
}
Was it helpful?

Solution

Use messaging pattern like Prism's EventAggregator (specifying ThreadOption.BackgroundThread). This way the caller sends a message (the message would contain From, To, Subject, Body), and it is asynchronous from the sender's point of view. Then use the synchronous Send function of System.Net.Mail in the consumer/handler of the message. This probably works because the executing background thread has more proper privileges than the one spawns the SendAsync.

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