2014-09-05 42 views
0

此問題已被要求&我已經嘗試過每個&我在堆棧溢出中發現的每件事我正在將我的頭撞到牆上而不是確定我在哪裏犯錯,無論如何這裏是我的代碼。SMTP服務器需要安全連接或客戶端未通過身份驗證 - 連接超時

using (MailMessage mm = new MailMessage("[email protected]", txtEmail.Text)) 
{ 
    mm.Subject = "Account Activation"; 
    string body = "Hello " + txtUsername.Text.Trim() + ","; 
    body += "<br /><br />Please click the following link to activate your account"; 
    body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>"; 
    body += "<br /><br />Thanks"; 
    mm.Body = body; 
    mm.IsBodyHtml = true; 
    SmtpClient smtp = new SmtpClient(); 
    smtp.Host = "smtp.gmail.com"; 
    smtp.Port = 587; 
    smtp.EnableSsl = true; 
    smtp.UseDefaultCredentials = false; 
    NetworkCredential NetworkCred = new NetworkCredential("[email protected]", "password"); 
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
    smtp.Credentials = NetworkCred; 
    smtp.Send(mm); 
} 

我試着將默認憑據更改爲false,我注意到的是當我更改端口號。從587到465我正在連接超時。

+0

可能重複的[SMTP服務器需要安全連接或客戶端未經過身份驗證。服務器響應是:5.5.1需要身份驗證](http://stackoverflow.com/questions/25660145/the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated) – 2014-09-05 14:58:58

回答

0

我想你可能會過度複雜化它。您不應該需要使用NetworkCredential。它應該是這樣的:

using (MailMessage mm = new MailMessage("[email protected]", txtEmail.Text)) 
{ 
    mm.Subject = "Account Activation"; 
    string body = "Hello " + txtUsername.Text.Trim() + ","; 
    body += "<br /><br />Please click the following link to activate your account"; 
    body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>"; 
    body += "<br /><br />Thanks"; 
    mm.Body = body; 
    mm.IsBodyHtml = true; 
    SmtpClient smtp = new SmtpClient(); 
    smtp.Host = "smtp.gmail.com"; 
    smtp.Port = 465; 
    smtp.ConnectType = SmtpConnectType.ConnectSSLAuto; 
    smtp.User = "[email protected]"; 
    smtp.Password = "yourpassword"; 
    smtp.Send(mm); 
} 
相關問題