2016-05-13 140 views
-1

我製作了一個測試頁面,其中包含一個電子郵件地址文本框,一個提交按鈕和一個錯誤標籤。無法通過ASP.NET發送電子郵件給客戶端

下面是C#代碼,我在這裏看到的論壇:

try 
{ 
    MailMessage mail = new MailMessage("[email protected]", (string)txtEmail.Text); 
    SmtpClient client = new SmtpClient(); 
    client.Port = 25; 
    client.DeliveryMethod = SmtpDeliveryMethod.Network; 
    client.UseDefaultCredentials = false; 
    client.Host = "smtp.google.com"; 
    mail.Subject = "this is a test email."; 
    mail.Body = "this is my test email body"; 
    client.Send(mail); 

    lblError.Text = "Message sent!"; 
} 
catch (Exception ex) 
{ 
    lblError.Text = ex.ToString(); 
} 

完整的錯誤它告訴我的是:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.google.com' at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at email.btnSend_Click(Object sender, EventArgs e) in d:\WebDev\Test\email.aspx.cs:line 28

但它不是爲我工作,我don`不知道該怎麼幫助我!

+0

什麼是「它不是不工作「是什麼意思?它是否給出錯誤信息? –

+1

你會得到什麼錯誤?您將不得不告訴我們「它不適合我」 – user1666620

+0

對不起,我已將完整的錯誤添加到問題主體 – D4NieLDev

回答

1

您可以通過添加此

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
       <network host="smtp.host.net" userName="yourUsername" password="yourPassword" port="587" enableSsl="true" /> 
     </smtp> 
    </mailSettings> 
</system.net> 

設置的所有SMTP客戶端的憑據在web.config然後你的代碼發送電子郵件變得更簡單

try 
{ 
    using (var client = new SmtpClient()) 
    { 
     MailMessage mail = new MailMessage("[email protected]", (string)txtEmail.Text); 
     mail.Subject = "this is a test email."; 
     mail.Body = "this is my test email body"; 
     client.Send(mail); 
    } 

    lblError.Text = "Message sent!"; 
} 
catch (Exception ex) 
{ 
    lblError.Text = ex.ToString(); 
} 
+0

哇謝謝人!你真的幫助了我! – D4NieLDev

3

你的主機名和端口是不正確 - Gmail使用smtp.gmail.comsmtp.google.com和端口587不是25

你的代碼也沒有提供任何的網絡憑據。您已將UseDefaultCredentials正確設置爲false,但您還需要提供新的NetworkCredentials。

client.UseDefaultCredentials = false; 
client.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword"); 

您還需要啓用SSL:

client.EnableSsl = true; 
+0

它顯示我錯誤'NetworkCredential'(我不能定義這個類的對象)應該導入一個特殊的類嗎? – D4NieLDev

+1

@ D4NieLDev添加了一個名稱空間參考。 – user1666620

+0

現在我有這個錯誤: ** System.Net.Mail.SmtpException:SMTP服務器需要安全連接或客戶端未通過身份驗證。服務器響應是:5.7.0必須首先發出STARTTLS命令。** – D4NieLDev

相關問題