2016-02-02 61 views
0

好傢伙我試圖從我的ASP.Net Web應用程序發送電子郵件我用這對我的web.config:ASP.Net C#發送電子郵件的SMTP問題

<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靜得我得到這個錯誤和電子郵件到我的帳戶有人試圖在未經許可的情況下使用我的電子郵件。任何人都可以幫忙?

+2

可能重複:http://stackoverflow.com/questions/2391079/gmail-c-sharp-web-config-send-mail-works-programmatically-throws-exception –

回答

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); 
+0

不,不工作我甚至沒有收到錯誤信息,但謝謝你的試用:) –