本文介绍了发送邮件在服务器上工作,但不在我的本地机器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(我已经停留在xxx不发布真实姓名)。当我上传到服务器时,这段代码可以正常工作,并且将完美发送电子邮件。

i have the following code below (i have stuck in xxx to not publish the real name). this code works fine when i upload to the server and will send out emails perfectly.

        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("[email protected]", "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());
        }

但是当我在视觉工作室的本机上进行本地测试时,我收到错误:

but when i test locally on my machine in visual studio, i get an error:

[SocketException(0x274d):不能连接,因为目标机器主动拒绝它 66.226.21.251:25]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)+239
System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)+35
System.Net.ServicePoint.ConnectSocketInternal( Boolean connectFailure,Socket s4,Socket s6,Socket& socket,IPAddress& address,ConnectSocketState状态,IAsyncResult asyncResult,Int32超时,异常和异常)+224

[SocketException (0x274d): No connection could be made because the target machine actively refused it 66.226.21.251:25] System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +239 System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +35 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +224

[WebException: 无法连接到远程服务器]
System.Net.ServicePoint.GetConnection(PooledStream PooledStream,Object owner,Boolean async,IPAddress& address,Socket& abortSocket,Socket& abortSocket6,Int32 timeout )+5420699
系统m.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(String host,Int32 port)+160
System.Net.Mail.SmtpTransport.GetConnection (String host,Int32 port)+159

[WebException: Unable to connect to the remote server] 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(String host, Int32 port) +160 System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +159

任何建议,为什么这不工作

any suggestions of why this wouldn't work

推荐答案

我要做的第一件事就是看错误信息是否实际拒绝连接。我可以从这里连接到该IP地址的端口25,所以可能是您的错误信息是误导的。这些都是导致无法连接的可能原因:

The first thing I'd do to run this down is to see whether the target machine is, as the error message says, actually refusing the connections. I can connect to port 25 on that IP address from here, so it's possible that your error message is misleading. These are all possible causes of a failure to connect:


  • 您的代码中有一些问题

  • 您正在使用的图书馆中有一些问题

  • 您有一个本地防火墙,防止出现异常程序的出站邮件

  • 您的ISP不允许出站SMTP连接除了自己的服务器

  • 某些中间网络正在阻止事件

  • 邮件服务器的ISP正在阻止来自您的网络的SMTP连接

  • 邮件服务器真的拒绝你,也许是因为你在一个阻止列表

  • there's some problem in your code
  • there's some problem in the library you're using
  • you have a local firewall preventing outbound mail from strange programs
  • your ISP does not permit outbound SMTP connections except to their own servers
  • some intermediate network is blocking things
  • the mail server's ISP is blocking SMTP connections from your network
  • the mail server really is rejecting you, perhaps because you're on a blocklist

要获得更多的细节,下载并安装。这就像网络的调试器。在您的系统和目标网络之间启动捕获,然后运行您的代码。解释捕获的数据包需要一些经验,所以如果你被卡住,请随时在这里发布更多的数据。

To get more detail, download and install Wireshark. It's like a debugger for networks. Start a capture between your system and the target network and then run your code. Interpreting the captured packets takes some experience, so if you get stuck feel free to post more data here.

这篇关于发送邮件在服务器上工作,但不在我的本地机器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:11