ASP.Net C#发送电子邮件SMTP问题

时间:2016-02-02 20:27:56

标签: c# asp.net email smtp

大家好我试图从我的ASP.Net网络应用程序发送一封电子邮件,我在我的网站上使用它。:

<system.net>
<mailSettings>
  <smtp>
    <network host="smtp.gmail.com" userName="myemail" password="mypass" port="587" enableSsl="true" />
  </smtp>
</mailSettings>

并且它都是正确的,但我收到此错误消息:

System.Net.Mail.SmtpException: 
The SMTP server requires a secure connection or not done client authentication. 
The server response was: 5.5.1 Authentication Required. 
Learn more at at System.Net.Mail.MailCommand.CheckResponse 
(SmtpStatusCode statusCode, String response) in System.Net.Mail.MailCommand.Send 
(SmtpConnection conn, Byte [] command, MailAddress from, Boolean allowUnicode) 
in System.Net. Mail.SmtpTransport.SendMail (MailAddress sender, MailAddressCollection 
recipients, String deliveryNotify, Boolean allowUnicode, 
SmtpFailedRecipientException & exception) in System.Net.Mail.SmtpClient.Send 
(MailMessage message) at Account_Registration.Registration () 
in c: \ Users \ User \ Documents \ Visual Studio 2013 \ WebSites \ 
MyWebsite \ Account \ Registration.aspx.cs: line 177

正如您在我的Web.config文件中看到的那样,我已经启用了Ssl,所以我仍然收到此错误并向我的帐户发送了一封电子邮件,有人试图在未经许可的情况下使用我的电子邮件。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

<appSettings> <add key="EmailHost" value="smtp.gmail.com" /> <add key="EmailPort" value="587" /> <add key="EmailUsername" value="myemail" /> <add key="EmailPassword" value="mypass" />

        var mailClient = new SmtpClient(
            Convert.ToString(ConfigurationManager.AppSettings["EmailHost"]),
            Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]))
        {
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials =
                new NetworkCredential(
                    Convert.ToString(ConfigurationManager.AppSettings["EmailUsername"]),
                    Convert.ToString(ConfigurationManager.AppSettings["EmailPassword"]))
            //EnableSsl = true
        };

        mailClient.SendCompleted += (s, e) => {
            mailClient.Dispose();
        };
        return mailClient.SendMailAsync(email);