我在下面有以下代码(我一直在 xxx 中不公布真实姓名)。当我上传到服务器时,这段代码可以正常工作,并且会完美地发送电子邮件。
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromEmailAddress_);
msg.To.Add(new MailAddress(toEmailAddress_));
msg.Subject = subject_;
msg.Body = body_;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("xxx@xxxx.org", "password");
try
{
SmtpClient c = new SmtpClient("mail.xxx.org");
c.UseDefaultCredentials = false;
c.Credentials = basicAuthenticationInfo;
c.Send(msg);
}
catch (Exception ex)
{
throw ex;
Console.Write(ex.Message.ToString());
}
但是当我在 Visual Studio 的机器上进行本地测试时,出现错误:
[SocketException (0x274d):无法建立连接,因为目标机器主动拒绝它66.226.21.251:25] System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +239 System.Net.Sockets.Socket。 InternalConnect(端点 remoteEP)+35 System.Net.ServicePoint.ConnectSocketInternal(布尔连接失败,套接字 s4,套接字 s6,套接字和套接字,IPAddress 和地址,ConnectSocketState 状态,IAsyncResult asyncResult,Int32 超时,异常和异常)+224
[WebException:无法连接到远程服务器] System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) +5420699 System.Net.PooledStream.Activate( Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) +202 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +21 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +332 System .Net.Mail.SmtpConnection.GetConnection(字符串主机,Int32 端口)+160 System.Net.Mail.SmtpTransport.GetConnection(字符串主机,Int32 端口)+159
关于为什么这不起作用的任何建议