2016-09-06 277 views
0

我正嘗試使用Gmail SMTP服務器發送電子郵件,但郵件始終無法發送。瀏覽器窗口關閉,Express for Web閃爍顯示此錯誤。任何幫助將不勝感激。 代碼已被列入下面Gmail Smtp消息無法發送。

代碼隱藏

using System.Net.Mail; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Demos_email_demo : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    MailMessage myMessage = new MailMessage(); 
    myMessage.Subject = "Test Message"; 
    myMessage.Body = "Hello World"; 
    myMessage.From = new MailAddress("[email protected]", "Will"); 
    myMessage.To.Add(new MailAddress("[email protected]", "Will")); 



    SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com"); 
    mySmtpClient.Send(myMessage); 


} 
} 

的Web.Config

<system.web> 
    <compilation debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5.2" /> 
</system.web> 
<system.net> 
<mailSettings> 
<smtp deliveryMethod="Network" from="Will &lt;[email protected]&gt;"> 
<network host ="smtp.gmail.com" enableSsl="true" port="465" /> 
</smtp> 
</mailSettings> 
</system.net> 
</configuration> 

Screenshot Of Error

+1

嘗試在SMTP設置這樣的新SmtpClient(「smtp.gmail.com」,587)expicitlely設置端口.....你甚至可能需要使用using語句,並設置SSL和諸如此類(var smtp = new SmtpClient())var credential = new NetworkCredential {UserName =「[email protected]」,//替換爲有效值 Password =「qwerty123456」//替換爲有效值 }; smtp.Credentials =憑證; smtp.Host =「smtp.gmail.com」; smtp.Port = 587; smtp.EnableSsl = true; smtp.Send(message);} –

+0

您還需要設置您的Gmail帳戶以允許使用smtp。 https://www.google.com/search?q=set+gmail+account+to+allow+smtp&ie=&oe= –

回答

0

你需要設置的NetworkCredential篩選。

client.Credentials = new NetworkCredential(「username」,「password」);

您的代碼應該是這樣的。

MailMessage myMessage = new MailMessage(); 
    myMessage.Subject = "Test Message"; 
    myMessage.Body = "Hello World"; 
    myMessage.From = new MailAddress("[email protected]", "Will"); 
    myMessage.To.Add(new MailAddress("[email protected]", "Will")); 

    SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com"); 
    mySmtpClient .Credentials=new NetworkCredential("username", "password"); 

    mySmtpClient.Send(myMessage); 
+0

這絕對讓我處於某個地方,但Google現在會阻止正在發送的電子郵件(即使啓用了IMAP) –

+1

允許訪問您的Gmail帳戶中安全性較低的應用。 https://support.google.com/accounts/answer/6010255?hl=zh-CN –

+0

Asp.net現在引發以下錯誤「無法從傳輸連接讀取數據:net_io_connectionclosed。」 –