c# SmtpClient class not able to send email using gmail

前端 未结 7 1100
-上瘾入骨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 16:59

    This works, but it's not very performance friendly. Check out client.SendAsync: http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx

    An example use case:

     var message = new MailMessage("from", "to", "subject", "body");
     var client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential("login", "password"),
                    EnableSsl = true
                };
                client.SendCompleted += (s, e) =>
                {
                    client.Dispose();
                    message.Dispose();
                };
                client.SendAsync(message, null);
    

提交回复
热议问题