簡體   English   中英

問題是有關在ASP.NET中發送電子郵件

[英]The problem is about sending email in ASP.NET

protected void SendEmail(object sender, EventArgs e)
    {
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {
            MailAddress fromAddress = new MailAddress("fromemail", "From Me");
            MailAddress toAddress = new MailAddress("toemail", "To You");
            message.From = fromAddress;
            message.To.Add(toAddress);
            message.Subject = "Testing!";
            message.Body = "This is the body of a sample message";
            smtpClient.Host = "smtp.host.com";
            smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
            smtpClient.EnableSsl = true;
            smtpClient.Port = 587;
            smtpClient.Send(message);
            statusLabel.Text = "Email sent.";
        }
        catch (Exception ex)
        {
            statusLabel.Text = "Coudn't send the message!\n"+ex.Message;
        }
    }

錯誤是

“郵件發送失敗。”

我無法發送任何郵件。 問題是什么?

innerexception是

System.Net.WebException:無法連接到遠程服務器---> System.Net.Sockets.SocketException:連接嘗試失敗,因為一段時間后被連接方未正確響應,或者由於連接的主機而建立的連接失敗在System.Net.ServicePoint.ConnectSocketInternal(Boolean)的System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)的System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)上未能響應74.125.39.109:587 connectFailure,套接字s4,套接字s6,套接字和套接字,IP地址和地址,ConnectSocketState狀態,IAsyncResult asyncResult,Int32超時,異常和異常)-內部異常堆棧跟蹤的結尾-在System.Net.ServicePoint.GetConnection(PooledStream PooledStream, System.Net.PooledStream.Activate(對象owningObject,布爾異步,Int32超時,GeneralAsy)處的對象所有者,布爾異步,IPAddress&地址,Socket&abortSocket,Socket&abortSocket6,Int32超時) System.Net.Connection.PooledStream.Activate(對象owningObject,GeneralAsyncDelegate asyncCallback)處的System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)位於System.Net.Mail.SmtpConnection.GetConnection(字符串主機, System.Net.Mail.SmtpTransport.GetConnection的Int32端口)(字符串主機,System.Net.Mail.SmtpClient.GetConnection()的System.Net.Mail.SmtpClient.Send(MailMessage消息)

對於SmtpException ,您始終必須通過Exception.InnerException屬性檢查內部異常。 總是。

刪除try / catch,看看會發生什么:-)

您可以先在本地進行測試:

<mailSettings>
    <smtp deliveryMethod='SpecifiedPickupDirectory'>
        <specifiedPickupDirectory pickupDirectoryLocation="c:\maildrop" />
    </smtp>
</mailSettings>

更多細節:

http://weblogs.asp.net/gunnarpeipman/archive/2010/05/27/asp-net-using-pickup-directory-for-outgoing-e-mails.aspx

從內部異常來看:無法達到74.125.39.109:587。

確保:

  1. 74.125.39.109:587實際上有一個運行的SMTP服務器
  2. 在運行代碼的服務器上,確保可以連接到該IP:Port(沒有防火牆或病毒掃描程序阻止外發smtp連接嘗試)。

一種測試方法是,從同一台服務器打開與74.125.39.109:587的telnet連接。

您已經使用587作為端口....這類似於gmail的smtp服務端口號....如果您使用gmail smtp服務...請嘗試使用此...

主機:smtp.gmail.com端口:587

在網絡憑證中為您提供有效的gmail用戶名及其密碼...

還檢查您的互聯網連接

嘗試取出所有smtp服務器設置,然后將mailSettings放入您的app / web.config中

有時郵件服務器將不允許郵件中繼。 在這種情況下,您將需要設置虛擬smtp服務器。

在IIS 6中中繼

如果使用Windows Server 2008,則需要安裝IIS 6管理控制台以配置中繼。

默認的SMPT端口為25。您正在使用587,可能是問題所在。 還是有使用端口的原因?

我記得我遇到過類似的問題,我找到的解決方案是:

之前:

smtpClient.Send(message);

把這一行:

ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

BTW:

端口應為: smtp.Port = 25;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM