3

The program runs on multiple computers within the same network. The mail is sent through an internal server. When I try to send an email from SmtpClient, it works on some computers but on others it gives me:

"System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 10.1.0.74:25"

I've tried looking it up and a lot of answers talk about the firewall or the smtp server blocking requests. The problem is it only errors on certain computers, and I'm unsure what the firewall settings are supposed to be.

The code for sending the email is as follows:

public void SendMessage(string subject, string messageBody, string fromAddress, string toAddress)
    {
        MailMessage message = new MailMessage();
        SmtpClient client = new SmtpClient("10.1.0.74", 25);

        // Set the sender's address
        message.From = new MailAddress(fromAddress);

        // Allow multiple "To" addresses to be separated by a semi-colon
        if (toAddress.Trim().Length > 0)
        {
            foreach (string addr in toAddress.Split(';'))
            {
                if (addr.Trim() != "")
                    message.To.Add(new MailAddress(addr));
            }
        }

        // Set the subject and message body text
        message.Subject = subject;
        message.Body = messageBody;
        message.IsBodyHtml = true;

        // Set the SMTP server to be used to send the message
        //client.Host = "smtp.gmail.com";
        System.Net.NetworkCredential a = new System.Net.NetworkCredential("User", "Pass");
        //client.EnableSsl = true;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = a;
        // Send the e-mail message
        client.Send(message);
    }

Edit: Pinged the IP from the computer that's not working, got the following. enter image description here

4

1 回答 1

4

在您的客户端中启用 SSL 并尝试其他端口:

client.EnableSsl = true;
client.Port = 587;
// or 
client.Port = 465;

您还可以使用 cmd 命令检查连接:

telnet smtp.gmail.com 587

成功的响应通常如下所示:

220 mx.google.com ESMTP
于 2013-10-18T09:20:12.013 回答