microsoft / referencesource

Source from the Microsoft .NET Reference Source that represent a subset of the .NET Framework
https://referencesource.microsoft.com/
MIT License
3.16k stars 1.27k forks source link

Issue with the order when setting properties of client #60

Closed milos92100 closed 5 years ago

milos92100 commented 6 years ago

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:

    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)
terrajobst commented 5 years ago

This repo only tracks infrastructure issues regarding the following items:

For issues regarding functionality, please use the following resources: