Search code examples
c#.netsmtpclient

Using SmtpClient, and getting "the target machine actively refused it"


I am trying to use System.Net.Mail for an application to send an email, but get this exception:

System.Net.Mail.SmtpException: Failure sending mail. ---> 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 198.238.39.170:25

The code I am using is:

string mailserver = "Scanmail.ofm.wa.lcl";
MailMessage msg = new MailMessage("[email protected]", "[email protected]", "Very subjective", "A message body!");
SmtpClient client = new SmtpClient(mailserver);
client.Send(msg);

Obviously the email addresses above are fictional, but in the actual code it uses real email addresses in the system. The mail server address is accurate.

I am tempted to think that I need to put some kind of security credentials in there, but not sure where - although @Andre_Calil's advice suggests this is not the problem, and that possibly the mail server is configured to prevent my development machine from connecting. So how is this to be overcome?


Solution

  • So, as we were talking, your server is probably configured to deny relay from every machine, which is a recommended security setting.

    From your development machine, open a prompt (command) and type telnet SMTP_SERVER_IP_ADDRESS 25. This command will try to establish a basic socket connection on port 25, which is the default SMTP port. If it's successful, you'll still have to discover whether the server requires authentication.

    However, if it's unsuccessful, you'll have to put your code on hold until you can get the help of a sysadmin.

    One other thing to try is to repeat this same test from a app server, because the SMTP server may be configured to allow app_server and deny everybody_else.

    Regards