创建用户帐户时,尝试发送包含密码的电子邮件。(在帮助下) 如何从 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)}
有任何想法吗?