【问题标题】:The problem is about sending email in ASP.NET问题是关于在 ASP.NET 中发送电子邮件
【发布时间】:2011-06-15 11:24:19
【问题描述】:
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;
        }
    }

错误是

“发送邮件失败。”

我不能发送任何东西。 有什么问题?

内部异常是

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败因为连接的主机未能在 System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) 在 System.Net.ServicePoint 的 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 响应 74.125.39.109:587。 ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- 内部异常堆栈跟踪结束---在 System.Net.ServicePoint.GetConnection( PooledStream PooledStream,对象所有者,布尔异步,IPAddress 和地址,套接字和 abortSocket,套接字和 abortSocket6,Int32 超时)在 System.Net.PooledStream.Activate(对象拥有对象,布尔异步,Int32 超时,常规AsyncDelegate asyncCallback) 在 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) 在 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) 在 System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 端口)在 System.Net.Mail.SmtpTransport.GetConnection(字符串主机,Int32 端口)在 System.Net.Mail.SmtpClient.GetConnection() 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)

【问题讨论】:

  • 那么你得到了什么异常(如果有的话)?
  • 请包含整个 ex.ToString() 的输出。实际问题可能在内部异常中描述,需要诊断整个异常输出。

标签: c# asp.net


【解决方案1】:

对于SmtpException,您总是必须通过Exception.InnerException 属性检查内部异常。总是。

【讨论】:

  • 这应该是评论,你没有回答他的问题。
  • 作为答案或评论发布的规则是什么?
  • 一个实际上并不能直接帮助用户的衬里应该是评论。
  • Uwe - 如您所见,它们也可以在 cmets 中格式化:)
  • 不是快捷语法。只需使用[text](url) 语法。
【解决方案2】:

删除 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

【讨论】:

  • 我按照你说的做了。但是当我把它改成网络时它不起作用
【解决方案3】:

从内部异常:74.125.39.109:587 无法到达。

确保:

  1. 74.125.39.109:587 实际上有一个 SMTP 服务器正在运行
  2. 从运行代码的服务器,确保您可以连接到该 IP:Port(没有防火墙或病毒扫描程序阻止传出 smtp 连接尝试)。

一种测试方法是从同一台服务器打开到 74.125.39.109:587 的 telnet 连接。

【讨论】:

  • 谢谢。是的,它正在工作。我可以发送电子邮件。防病毒程序阻止了我的套接字。
【解决方案4】:

尝试删除所有 smtp 服务器设置并将mailSettings 放在您的 app/web.config 中

有时邮件服务器不允许邮件中继。在这种情况下,您需要设置一个虚拟 smtp 服务器。

Relaying in IIS 6

如果您使用的是 windows server 2008,则需要安装 IIS 6 管理控制台来配置中继。

【讨论】:

    【解决方案5】:

    默认 SMPT 端口是 25。您使用的是 587,这可能是问题所在。还是有使用端口的原因?

    【讨论】:

    • 因为我用gmail账号发邮件,所以我用587
    【解决方案6】:

    你使用 587 作为端口....这类似于 gmail 的 smtp 服务端口号....如果你使用 gmail smtp 服务...尝试使用这个...

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

    在网络证书中给你有效的 gmail 用户名和密码...

    同时检查您的互联网连接

    【讨论】:

      【解决方案7】:

      我记得我遇到了类似的问题,我找到的解决方案是:

      之前:

      smtpClient.Send(message);

      把这一行:

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

      顺便说一句:

      端口应该是: smtp.Port = 25;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 2013-04-05
        • 2015-10-16
        • 1970-01-01
        相关资源
        最近更新 更多