【发布时间】:2013-12-26 04:55:25
【问题描述】:
当数据库中的某些值超过其阈值时,我正在尝试发送 SMTP 电子邮件。
我已经在 Windows 防火墙中允许了端口 25,587 和 465,并在防病毒软件中禁用了阻止群发邮件的选项。我正在使用的代码如下所示
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add("to@domain.com");
// From
MailAddress mailAddress = new MailAddress("from@domain.com");
mailMsg.From = mailAddress;
// Subject and Body
mailMsg.Subject = "MCAS Alert";
mailMsg.Body = "Parameter out of range";
SmtpClient smtpClient = new SmtpClient("smtp.servername.com", 25);
smtpClient.UseDefaultCredentials = false;
smtpClient.Timeout = 30000;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("username", "passwrod");
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
//ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtpClient.Send(mailMsg);
堆栈跟踪
[SocketException (0x271d): An attempt was made to access a socket in a way forbidden by its access permissions xx.xx.xx.xx:25]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +208
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +464
[WebException: Unable to connect to the remote server]
System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) +6486360
System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) +307
System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +19
System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +324
System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +141
System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) +170
System.Net.Mail.SmtpClient.GetConnection() +44
System.Net.Mail.SmtpClient.Send(MailMessage message) +1554
[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +1906
Admin_Alert.SMTPAuth() in c:\Users\spandya\Documents\Visual Studio 2012\WebSites\WebSite3\Admin\Alert.aspx.cs:61
Admin_Alert.Page_Load(Object sender, EventArgs e) in c:\Users\spandya\Documents\Visual Studio 2012\WebSites\WebSite3\Admin\Alert.aspx.cs:22
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
我在这里还缺少什么?这些特定的端口地址有防火墙入站规则。
【问题讨论】:
-
错误来自发送方还是接收方?
-
检查堆栈跟踪的编辑
-
您是否尝试过从相关机器 Telnet 到此端口以确保路由打开?类似
telnet smtp.mydomain.com 25 -
主要是
SocketException (0x271d)被抛出用于端口块,所以您可以检查该服务器上的netstat -anb | find "25"或远程检查telnet smtp.servername.com 25以检查该端口是否在您的smtp 服务器上打开? -
我尝试了telnet,但它说无法连接到端口25上的主机
标签: c# asp.net sockets email smtp