2017-06-11 231 views
0
獲得一個忘記密碼鏈接
// send Email 
string toEmailAddress = dt.Rows[0][4].ToString(); 
string Username = dt.Rows[0][1].ToString(); 
string EmailBody = "Hi "+Username+ "<br/> click the link below to reset your password<br/> http://localhost:51583/RecoveryPass.aspx?Uid="+myGUID; 
MailMessage PassRecMail = new MailMessage("[email protected]",toEmailAddress); 
PassRecMail.Body = EmailBody; 
PassRecMail.IsBodyHtml = true; 
PassRecMail.Subject = "Reset Password"; 

SmtpClient SMTP = new SmtpClient(); 

SMTP.Port = 587; 
SMTP.Host = "smtp.gmail.com"; 
SMTP.EnableSsl = true; 
SMTP.UseDefaultCredentials = false; 
SMTP.DeliveryMethod = SmtpDeliveryMethod.Network; 
SMTP.Credentials = new NetworkCredential() 
{ 
    UserName= "[email protected]", 
    Password= "yourGmailPassword" 
}; 
SMTP.UseDefaultCredentials = false; 

SMTP.Send(PassRecMail); 

我收到此錯誤:C#通過電子郵件

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. at line SMTP.Send(PassRecMail);

我已經打開了谷歌少保,然後還我收到此錯誤。 我該如何解決它?

+1

有https://stackoverflow.com/questions/17227532/gmail-530-5-5-1-authentication-required-learn-more-at和https://productforums.google的讀取。 com/forum /#!topic/gmail/vtTz1ngyH8o。 – mjwills

+0

[通過Gmail在.NET中發送電子郵件]的可能副本(https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – VDWWD

回答

1

1您將定義.......

using System.Net.Mail;

using System.Data.SqlClient;

using System.Data;

using System.Net;

using System.Drawing;

然後,我們可以創建新的Gmail ID(我認爲最好),然後.......

SmtpClient smtpClient =新SmtpClient( 「smtp.gmail.com」,587);

smtpClient.Credentials = new System.Net.NetworkCredential() 
    { 
     UserName = "[email protected]", 
     Password = "********" 
    }; 

    smtpClient.EnableSsl = true; 
    smtpClient.Send(mailMessage);