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

发布于 2024-10-30 04:44:14 字数 2013 浏览 4 评论 0 原文

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

,这是我的 C# 代码:

string from =  "[email protected]"; //Replace this with your own correct Gmail Address
string to = "[email protected]";  //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.

非常感谢大家!

I can't send a email message using gmail settings. i already tried client.Host ="localhost" it's working but not in client.Host ="smtp.gmail.com".. Please help me guys.. I need use client.Host ="smtp.gmail.com".. thanks

here's my C# code:

string from =  "[email protected]"; //Replace this with your own correct Gmail Address
string to = "[email protected]";  //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

here's the error:

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

Much thanks guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下凄凉 2024-11-06 04:44:14

您需要使用 SSL 安全证书

MailMessage msgMail = new MailMessage("[email protected]", "[email protected]", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "a");
try
{
   smtp.Send(msgMail);
}
catch (Exception ex)
{
}

参考来获取邮件并将其发送到 GMail:http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4

You need to get and send mail to GMail by using SSL secutiry certificate

MailMessage msgMail = new MailMessage("[email protected]", "[email protected]", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "a");
try
{
   smtp.Send(msgMail);
}
catch (Exception ex)
{
}

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

梦与时光遇 2024-11-06 04:44:14
 public static string sendMail(string to, string title, string subject, string body)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                if (to == "")
                    to = "[email protected]";
                MailAddressCollection m = new MailAddressCollection();
                m.Add(to);
                mail.Subject = subject;
                mail.From = new MailAddress( "[email protected]");
                mail.Body = body;
                mail.IsBodyHtml = true;
                mail.ReplyTo = new MailAddress("[email protected]");
                mail.To.Add(m[0]);
                smtp.Host = "smtp.gmail.com";
                 client.Port = 587;
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "####");
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; 

                smtp.Send(mail);

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

                smtp.Send(mail);

                return "done";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文