.Net send mail through Gmail
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Sending email in .NET through Gmail
号
我正在尝试通过我创建的Gmail帐户用ASP.NET(MVC3)发送邮件。我在互联网上搜索了操作方法,但我尝试过的都没有奏效。
我有一个像这样的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public static bool SendEmail(string to,string subject,string body) { try { MailMessage mail = new MailMessage(); mail.From = new MailAddress("[email protected]"); mail.To.Add(to); mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = false; SmtpClient smtp = new SmtpClient(); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Host ="smtp.gmail.com"; smtp.Port = 587; smtp.Credentials = new System.Net.NetworkCredential("[email protected]","mypassword"); smtp.EnableSsl = true; smtp.Send(mail); return true; } catch { return false; } } |
当我使用此方法时,此方法返回false。我怎么解决这个问题?
我核实了以下工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | static void Main(string[] args) { //create the mail message MailMessage mail = new MailMessage(); var client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("[email protected]","yourpassword"), EnableSsl = true }; //set the addresses mail.From = new MailAddress("[email protected]"); mail.To.Add("[email protected]"); //set the content mail.Subject ="Test subject!"; mail.Body ="<html><body>your content</body></html>"; mail.IsBodyHtml = true; client.Send(mail); } |
号
尝试添加此行
1 | smtp.UseDefaultCredentials = false; |