If you set the credentials first and then say that you don't won't to use default credentials it sets the
existing credentials to null which causes an authentication error.
The Credentials property should not be overridden by setting UseDefaultCredentials to true which is by default.Choosing credentials should be done on sending.
Working code:
let smtpClient = new SmtpClient()
smtpClient.Host <- "smtp.gmail.com"
smtpClient.Port <- 587
smtpClient.EnableSsl <- true
smtpClient.DeliveryMethod <- SmtpDeliveryMethod.Network
//right order
smtpClient.UseDefaultCredentials <- false
smtpClient.Credentials <- new System.Net.NetworkCredential("***********", "**********")
use mail = new MailMessage()
mail.Body <- "Hello from Mocosha"
mail.Subject <- "Test"
mail.From <- new MailAddress("foo", "foo")
mail.To.Add(new MailAddress("bar"))
smtpClient.Send(mail)
Not working code:
let smtpClient = new SmtpClient()
smtpClient.Host <- "smtp.gmail.com"
smtpClient.Port <- 587
smtpClient.EnableSsl <- true
smtpClient.DeliveryMethod <- SmtpDeliveryMethod.Network
//problematic lines
//different order
smtpClient.Credentials <- new System.Net.NetworkCredential("***********", "**********")
smtpClient.UseDefaultCredentials <- false
use mail = new MailMessage()
mail.Body <- "Hello from Mocosha"
mail.Subject <- "Test"
mail.From <- new MailAddress("foo", "foo")
mail.To.Add(new MailAddress("bar"))
smtpClient.Send(mail)
https://github.com/Microsoft/referencesource/blob/3b1eaf5203992df69de44c783a3eda37d3d4cd10/System/net/System/Net/mail/SmtpClient.cs#L267
If you set the credentials first and then say that you don't won't to use default credentials it sets the existing credentials to null which causes an authentication error. The Credentials property should not be overridden by setting UseDefaultCredentials to true which is by default.Choosing credentials should be done on sending.
Working code:
Not working code: