使用Gmail设置发送电子邮件时出现问题

im *_*ess 2 c# asp.net email gmail smtp

我无法使用Gmail设置发送电子邮件.我已经尝试过client.Host ="localhost"它正在工作但不在client.Host ="smtp.gmail.com"..请帮助我们..我需要使用client.Host ="smtp.gmail.com"..谢谢

这是我的C#代码:

string from =  "aevalencia119@gmail.com"; //Replace this with your own correct Gmail Address
string to = "aevalencia191@gmail.com";  //Replace this with the Email Address  to whom you want to send the mail

 System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); 
 mail.To.Add(to);  mail.From = new
 MailAddress(from, "One Ghost" ,System.Text.Encoding.UTF8);
 mail.Subject = "This is a test mail" ;
 mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body =  "This is Email Body Text";
 mail.BodyEncoding = System.Text.Encoding.UTF8;
 mail.IsBodyHtml = true ; mail.Priority = MailPriority.High;

 SmtpClient client = new SmtpClient();  //Add the Creddentials- use your own email id and password

  client.Credentials = new System.Net.NetworkCredential(from, "iseedeadpoeple");

 client.Port = 587; // Gmail works on this port client.Host ="smtp.gmail.com";         

 client.EnableSsl = true; //Gmail works on Server Secured Layer
        try
         {
             client.Send(mail);
         }
         catch (Exception ex) 
         {
             Exception ex2 = ex;
             string errorMessage = string.Empty; 
             while (ex2 != null)
             {
                 errorMessage += ex2.ToString();
                 ex2 = ex2.InnerException;
             }    HttpContext.Current.Response.Write(errorMessage
);
         } // end try
Run Code Online (Sandbox Code Playgroud)

这是错误:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Run Code Online (Sandbox Code Playgroud)

非常感谢你们!

Ari*_*tos 5

您需要使用SSL secutiry证书获取并向GMail发送邮件

MailMessage msgMail = new MailMessage("a@gmail.com", "b@mail.me", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("a@gmail.com", "a");
try
{
   smtp.Send(msgMail);
}
catch (Exception ex)
{
}
Run Code Online (Sandbox Code Playgroud)

参考:http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4