简体   繁体   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;
        }
    }

The error is 错误是

"Failure sending mail." “邮件发送失败。”

I can not send any. 我无法发送任何邮件。 What is the problem? 问题是什么?

innerexception is innerexception是

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.39.109:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsy 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超时) ncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) 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消息)

For an SmtpException , you always have to examine the inner exception through the Exception.InnerException property. 对于SmtpException ,您始终必须通过Exception.InnerException属性检查内部异常。 Always. 总是。

Remove the try/catch and see what happens :-) 删除try / catch,看看会发生什么:-)

You can test locally first: 您可以先在本地进行测试:

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

More details: 更多细节:

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

From the inner exception: 74.125.39.109:587 cannot be reached. 从内部异常来看:无法达到74.125.39.109:587。

Make sure: 确保:

  1. 74.125.39.109:587 actually has a SMTP server running 74.125.39.109:587实际上有一个运行的SMTP服务器
  2. From the server running the code, make sure you can connect to that IP:Port (there isn't a firewall or virus scanner blocking the outgoing smtp connection attempt). 在运行代码的服务器上,确保可以连接到该IP:Port(没有防火墙或病毒扫描程序阻止外发smtp连接尝试)。

One way to test, is from that same server, open a telnet connection to 74.125.39.109:587. 一种测试方法是,从同一台服务器打开与74.125.39.109:587的telnet连接。

u have use 587 as the port....this is similar to gmail's smtp service port number....if ur using gmail smtp service...try use this... 您已经使用587作为端口....这类似于gmail的smtp服务端口号....如果您使用gmail smtp服务...请尝试使用此...

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

in network ceredentials give ur valid gmail username and its password... 在网络凭证中为您提供有效的gmail用户名及其密码...

also check ur internet connectivity 还检查您的互联网连接

Try taking out all the smtp server settings and place the mailSettings in your app/web.config 尝试取出所有smtp服务器设置,然后将mailSettings放入您的app / web.config中

Sometimes mail servers will not allow mail relaying. 有时邮件服务器将不允许邮件中继。 In this case, you will need to set up a virtual smtp server. 在这种情况下,您将需要设置虚拟smtp服务器。

Relaying in IIS 6 在IIS 6中中继

If you are using windows server 2008, you will need to install the IIS 6 management console to configure relaying. 如果使用Windows Server 2008,则需要安装IIS 6管理控制台以配置中继。

Default SMPT port is 25. You are using 587, could this be the problem. 默认的SMPT端口为25。您正在使用587,可能是问题所在。 Or is there a reason for using port? 还是有使用端口的原因?

I remember i face similar problem the solution i find is: 我记得我遇到过类似的问题,我找到的解决方案是:

before: 之前:

smtpClient.Send(message);

Put this line: 把这一行:

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

BTW: BTW:

the port should be: smtp.Port = 25; 端口应为: smtp.Port = 25;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM