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

时间:2011-04-06 11:46:29

标签: c# asp.net email smtp gmail

我无法使用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

这是错误:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

非常感谢你们!

2 个答案:

答案 0 :(得分:5)

您需要使用SSL安全证书

获取并向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)
{
}

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

答案 1 :(得分:0)

 public static string sendMail(string to, string title, string subject, string body)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                if (to == "")
                    to = "aevalencia119@gmail.com";
                MailAddressCollection m = new MailAddressCollection();
                m.Add(to);
                mail.Subject = subject;
                mail.From = new MailAddress( "aevalencia119@gmail.com");
                mail.Body = body;
                mail.IsBodyHtml = true;
                mail.ReplyTo = new MailAddress("aevalencia119@gmail.com");
                mail.To.Add(m[0]);
                smtp.Host = "smtp.gmail.com";
                 client.Port = 587;
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential("aevalencia119@gmail.com", "####");
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; 

                smtp.Send(mail);

                return "done";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }