2013-10-21 71 views
0

創建用戶帳戶時嘗試發送包含密碼的電子郵件。 (在幫助下)How to send email from Asp.net Mvc-3?在創建用戶帳戶時發送包含密碼的電子郵件

public void SendEmail(string address, string subject, string message, string password) 
    { 
     string email = "emailAddress"; 
     //string password = "put-your-GMAIL-password-here"; 

     var loginInfo = new NetworkCredential(email, password); 
     var msg = new MailMessage(); 
     var smtpClient = new SmtpClient("smtp.gmail.com", 587); 

     msg.From = new MailAddress(email); 
     msg.To.Add(new MailAddress(address)); 
     msg.Subject = subject; 
     msg.Body = message; 
     msg.IsBodyHtml = true; 

     smtpClient.EnableSsl = true; 
     smtpClient.UseDefaultCredentials = false; 
     smtpClient.Credentials = loginInfo; 
     smtpClient.Send(msg); 
    } 

但是smtpClient.Send(msg);返回以下錯誤:

{System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at 
    at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 
    at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) 
    at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) 
    at System.Net.Mail.SmtpClient.Send(MailMessage message) 
    at ACCS.StockControl.Controllers.UserLoginRecordController.SendEmail(String address, String subject, String message, String password) 
    at ACCS.StockControl.Controllers.UserLoginRecordController.EditModalPOST(UserLoginRecordEditModalVM model)} 

有什麼想法嗎?

+0

你見過[這](http://stackoverflow.com/questions/17227532/gm ail-530-5-5-1-authentication-required-learn-more-at) – Liam

+0

[無法通過MailMessage&smtpClient授權給Gmail smtp](http://stackoverflow.com/questions/9104645/ cant-auth-to-gmail-smtp-via-mailmessage-smtpclient) – Liam

+0

任何進度?請在SSL中進行更改,然後重試。將啓用SSL設置爲false –

回答

0

它可能只是與錯誤的密碼和用戶名。

請使用另一個虛假密碼(例如「thisPasswordIsSurelyDontWork」)。 如果您例外是一樣的,那麼你必須檢查您creditentals,用戶名,密碼

//或嘗試

smtpClient.UseDefaultCredentials = true; 
+0

好的感謝您的回覆,虐待現在嘗試 – John

+0

它現在是如何工作的? – Krekkon

+0

這是現在的作品嗎? :) – Krekkon

0
MailMessage loginInfo = new MailMessage(); 
loginInfo.To.Add("Emailaddress"); 
loginInfo.From = new MailAddress("FromEmailaddress"); 
loginInfo.Subject = "Subject"; 
loginInfo.Body = "Body of the mail"; 
loginInfo.IsBodyHtml = true; 

SmtpClient smtp = new SmtpClient(); 
smtp.Host = hostname; 
smtp.Port = 25; //in my case i am using 25 
smtp.EnableSsl = false; //enable ssl is set to false 
smtp.Credentials = new System.Net.NetworkCredential(sEmailId, sPassword); 
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
smtp.Send(loginInfo); 
{ 
    Label.Visible = true; 
    Label.Text = "A mail has been sent to you with the your username. Please check your inbox."; 
} 
0

嘗試通過

smtpClient.EnableSsl = false; 
相關問題