错误:SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。
我的代码在这里:
protected void Button1_Click(object sender, EventArgs e)
{
Random r = new Random();
int rand = r.Next(10000, 90000);
string from = "srkdbest12@gmail.com";
string to = "ankushjain358@gmail.com";
string subject = "User Authentification";
string body = "click on the below link to AUTHENTICATE your account<br>";
body += "confirmation.aspx?confirm=" + rand;
try
{
MailMessage mailObject = new MailMessage(from, to, subject, body);
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Credentials = new NetworkCredential("srkdbest12@gmail.com", "MyPassword");
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Send(mailObject);
}
catch (Exception ee)
{
throw new ApplicationException("Confirmation email was not sent please try again or Try with a new email id<br>" + ee.Message);
}
答案 0 :(得分:2)
请勿设置UseDefaultCredentials
。
答案 1 :(得分:1)
以下代码适用于我:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("login", "password");
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
更新:正如 akiller 正确建议的那样,UseDefaultCredentials
属性应该在分配之前设置为false
证书。这就是使这段代码有效的原因。