2012-12-24 151 views
0

我想通過應用程序在asp.net使用C#發送電子郵件。我搜索了很多,主要是發現下面的代碼使用asp.net C#中發送電子郵件:如何發送電子郵件在asp.net使用C#到任何電子郵件地址使用Gmail地址

 MailMessage objEmail = new MailMessage(); 

     objEmail.From = new MailAddress(txtFrom.Text); 
     objEmail.To.Add(txtTo.Text); 
     objEmail.CC.Add(txtCC.Text); 
     objEmail.Bcc.Add(txtBCC.Text); 
     objEmail.Subject = txtSubject.Text; 

     try 
     { 

      SmtpClient mail = new SmtpClient(); 

      mail.EnableSsl = true; 
      mail.DeliveryMethod = SmtpDeliveryMethod.Network; 
      mail.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text); 
      mail.Timeout = 20000; 

      mail.UseDefaultCredentials = false; 

      mail.Host = "smtp.gmail.com"; 
      mail.Port = 587; 

      mail.Send(objEmail); 

      Response.Write("Your Email has been sent sucessfully - Thank You"); 

     } 
     catch (Exception exc) 
     { 
      Response.Write("Send failure due to : <br />" + exc.ToString()); 
     } 

,而總是我收到以下錯誤:

"System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) 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)"

+0

你需要找到一個SMTP服務器是願意送你。 – SLaks

+0

http://stackoverflow.com/questions/14024715/how-to-send-email-in-asp-net-using-c-sharp-to-any-email-address-using-gmail-addr – 2014-03-29 14:06:44

回答

0

你的代碼看起來很合理,所以你需要弄清楚是什麼阻止它的工作。

你能消除防火牆問題的可能性嗎?大多數中型或大型組織傾向於阻止端口25,465和587 - 他們根本不希望任何未經授權的郵件外出。如果您懷疑這可能是這種情況,您可以嘗試從網絡外部(即您的家用機器),儘管有些ISP也會阻塞端口。

如果防火牆未阻止您的連接,請驗證您的憑據是否有效 - 可以通過Thunderbird,Outlook或同等方式發送。嘗試使用未加密的郵件設置測試 - 根目錄通過垃圾郵件文件夾,將一些垃圾郵件粘貼到垃圾郵件或類似內容中,然後查找開放中繼。另外,還有一些商業電子郵件提供商支持未加密的電子郵件。

+0

我的應用程序正在我的家用機器上運行,我試圖通過禁用Windows防火牆發送eamil,但那不提供解決方案。我通過gmail直接訪問我的gmail帳戶。但是當我嘗試爲我的gmail帳戶配置Outlook時,這並未建立,並且在測試連接時顯示以下錯誤:「登錄到傳入郵件服務器(POP3):Outlook無法連接到您的傳入(POP3)電子郵件服務器如果仍然收到此消息,請與您的服務器管理員或Internet服務提供商(ISP)聯繫。「 –

+0

發送測試電子郵件:無法發送郵件。請確認您的電子郵件地址帳戶屬性服務器響應:530 5.7.0必須首先發出STARTTLS命令l5sm48012964wia.10 –

+0

您是否將Outlook配置爲使用TLS發送?如果是這樣,那麼它會顯示喲你無法創建從你的機器到谷歌的安全連接。可能你的ISP阻止了連接,或者你的機器上有其他東西阻止了安全連接。 – chris

3

您正在嘗試使用Gmail的具有不屬於Gmail的電子郵件地址的SMTP服務器(根據帖子的標題)。您需要更新主機和端口詳細信息以適應您的電子郵件提供商的SMTP詳細信息。

+0

我已修改我的問題是消除歧義。現在請查看帖子,我正在使用Gmail地址發送電子郵件,但得到了帖子中的錯誤。 –

+0

我的帖子中看不到任何錯誤。可能是防火牆/環境問題。 – keyboardP

+0

我試圖通過禁用防火牆來發送電子郵件,但這沒用。任何想法,我失去了什麼關鍵點:( –

0

你可以看看這個..簡單的代碼和它的作品的Gmail帳戶...

try 
    { 

     MailMessage msg = new MailMessage(); 
     msg.From = new MailAddress(TextFRM.Text); 
     msg.To.Add(TextTO.Text); 
     msg.Subject = TextSUB.Text; 
     msg.Body = TextMSG.Text; 


     SmtpClient sc = new SmtpClient("smtp.gmail.com"); 
     sc.Port = 25; 
     sc.Credentials = new NetworkCredential(TextFRM.Text, TextPSD.Text); 
     sc.EnableSsl = true; 
     sc.Send(msg); 
     Response.Write("Mail Send"); 
    } 

    catch (Exception ex) 
    { 

     Response.Write(ex.Message); 

    } 
相關問題