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