2017-04-23 55 views
0

我想通過我的網絡發送電子郵件,我尋求幫助沒有運氣。 仍然收到錯誤:smtpexception是由用戶代碼未處理用smtpexception發送郵件時發生錯誤

protected void Button1_Click(object sender, EventArgs e) 
    { 

     MailMessage msg = new MailMessage(); 
     msg.From = new MailAddress("[email protected]"); 
     msg.To.Add(TextBox3.Text); 
     msg.Subject = TextBox4.Text; 
     msg.Body = TextBox5.Text; 
     msg.IsBodyHtml = true; 

     SmtpClient smtp = new SmtpClient(); 
     smtp.Host = "smtp.gmail.com"; 

     System.Net.NetworkCredential netCred = new System.Net.NetworkCredential(); 
     netCred.UserName = "[email protected]"; 
     netCred.Password = "11111111"; 

     smtp.UseDefaultCredentials = true; 
     smtp.Credentials = netCred; 
     smtp.Port = 465; 
     smtp.EnableSsl = true; 
     smtp.Send(msg); 
     try 
     { 
      smtp.Send(msg); 
      Label1.Text = "Your E-Mail Sent Great Job!!!!"; 
     } 
     catch (Exception ex) 
     { 
      //Handle your exception here 
      Label1.Text = ex.Message + " " + "Oeps, something when wrong when we tried to send the email"; 
      return; 
     }   
    } 

當我運行它顯示我此錯誤突出smtp.Send(消息);:

SmtpException是由用戶代碼和故障未處理髮送郵件。

System.Net.Mail.SmtpException:發送郵件失敗。 ---> System.IO.IOException:無法從傳輸連接讀取數據:net_io_connectionclosed。 System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte []緩衝區,Int32偏移量,Int32讀取,布爾readLine)在System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader調用方,布爾oneLine)在System.Net.Mail.SmtpReplyReaderFactory System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)上System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)System.Net.Mail.SmtpClient.GetConnection()System.Net上的.ReadLine(SmtpReplyReader調用方) .Mail.SmtpClient.Send(MailMessage消息)---內部異常堆棧跟蹤結束---在System.Net.Mail.SmtpClient.Send(MailMessage消息)IntroAsp.net.EmailMsg.Button1_Click(Object sender,EventArgs e在c)中:\用戶\雷格夫\文件\的Visual Studio 2013 \項目\ IntroAsp.net \ IntroAsp.net \ EmailMsg.aspx.cs:線46

enter image description here

+0

應該有更多的異常細節比,可以添加它嗎? – Crowcoder

+0

你有沒有檢查過這個SmtpException和它的消息? –

+1

確保gmail設置爲允許「不太安全的應用程序」 –

回答

0

可能的解決方案:

1)Make smtpClient.UseDefaultCredentials = false;

2)憑證「用戶名」和「密碼」對發送電子郵件有效。

3)檢查端口465沒有被防火牆阻止。

4)而不是使用端口號465使用端口587

希望的,這可能會幫助你。

謝謝。

相關問題