c# SmtpClient class not able to send email using gmail

前端 未结 7 979
-上瘾入骨i
-上瘾入骨i 2020-11-27 16:24

I\'m having trouble sending email using my gmail account. Im pulling my hair out.

The same settings work fine in Thunderbird.

Here\'s the code. I\'ve also tr

相关标签:
7条回答
  • 2020-11-27 17:16

    You won't belive what fixed my problem.

    The Credentials property

    ss.Credentials = new NetworkCredential("username", "pass");
    

    must be declared after

    ss.UseDefaultCredentials = false;
    

    So the final working code listing is

    SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
    ss.EnableSsl = true;
    ss.Timeout = 10000;
    ss.DeliveryMethod = SmtpDeliveryMethod.Network;
    ss.UseDefaultCredentials = false;
    ss.Credentials = new NetworkCredential("username", "pass");
    
    MailMessage mm = new MailMessage("donotreply@domain.com", "destination@domain.com", "subject here", "my body");
    mm.BodyEncoding = UTF8Encoding.UTF8;
    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    ss.Send(mm);
    

    Is this a bug?

    0 讨论(0)
提交回复
热议问题