Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 681
Cyberherbalist Avatar asked Jul 23 '12 22:07

Cyberherbalist


People also ask

How do I fix my target machine actively refused?

You can try below solution: You might have a firewall rule in the way, or are trying to run it through a proxy without having the proxy up and running. The easiest way to check would be to disable your firewall or proxy and try again. Disable proxy at web.

Can't connect to target machine actively refused it?

​If no connection could be made because the target machine actively refused it, this indicates that the Source or Destination system blocked the connection. This is likely due to firewall or web server settings. In some cases, an IP restriction may be in place.

What is SmtpClient C#?

SMTP Client in C# and VB.NETThe Simple Mail Transfer Protocol (SMTP) is the only standard protocol for sending mail messages over the Internet. GemBox. Email enables you to work with the SMTP protocol in C# and VB.NET using an SmtpClient class.


1 Answers

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 stablish 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 unsuccesful, 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

like image 119
Andre Calil Avatar answered Oct 04 '22 20:10

Andre Calil