我正在尝试使用SmtpClient
和MailMessage
发送电子邮件。我尝试至少发送35次电子邮件,其中8次发送,其余26次显示以下异常:
Failure sending mail.
System
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.208.5.2:25
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, 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)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at \Schedulers\ConsumptionAlertJob.cs:line 90
我的代码如下:
var fromAddress= new MailAddress("xxxxxxx@xxxx.com", "Info");
const string fromPassword = "xxxxxxxx";
const string subject = "test";
string body = "test body";
var smtp = new SmtpClient
{
Host="smtp.xxxxx.com",
Port = 25,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredentail(fromAddress.Address, fromPassword)
};
using (MailMessage message= new MailMessage()
{
Subject=subject,
Body=body
})
{
message.To.Add("xxxxxxxx@gmail.com");
smtp.Send(message);
}
我以为是端口25被阻止的第一件事,但由于它正在发送1/3的电子邮件,并在其余的2/3发出例外,因此可能并非如此。在这方面的任何帮助都将受到高度赞赏。
值得一提的是,当我在控制台应用程序中运行时,所有电子邮件都已发送。仅当我尝试在Web应用程序项目ASP.net MVC中运行它并在Quartz.net中执行作业时,我才会遇到问题。
致谢