Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - SmtpClient - Unable to connect to the remote server

Tags:

c#

asp.net

I am working on a project where one of the requirements is to re-write an ASP.NET application. The old ASP.NET application was based on .NET Framework 1.1. The new ASP.NET application is based on .NET Framework 3.5.

One of the functions in the old web application was the ability to send email. The old code used the System.Web.Mail.SmtpMail class, whereas the new web application uses the System.Net.Mail.SmtpClient class.

In testing this on our development servers, everything worked fine. However, we have two beta clients testing out our software, and they both run into problems sending email in the new web application.

The specific exception is as follows:

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

Both the old and new ASP.NET application are on the same server (Windows Server 2003). They both are using the same Exchange mail server. Why is it that the old ASP.NET application can successfully send email, but the new one cannot?

I looked at the old code, and it did not use any form of authentication, it just specified the sender's email address, the recipient's email address, the subject, body, and server, and sent the message. I did the same thing in the new code, with the only difference being that I used the SmtpClient class.

Below is a code snippet from the new ASP.NET application:

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(messageTo);
mail.From = new MailAddress(messageFrom);
mail.Subject = messageSubject;
mail.Body = messageBody;

SmtpClient client = new SmtpClient();
client.Host = smtpServer;
client.Send(mail);
like image 918
Chris Avatar asked Jul 13 '10 14:07

Chris


1 Answers

The "Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it" error usually means that the TCP port you are trying to connect to does not have any service listening on it, i.e. it is closed and a tcp connection could not be made, let alone a SMTP connection.

Nothing will show in your exchange logs as it is failing at a lower level in the network stack and your mail server will not even see the connection attempt.

Can you post the .net1.1 code as well? The most likely problem is that the server is listening on a non standard port and you are not specifying that port in the new code. Please note that you need to set the port by using the Port property, you cannot simply append it to the Host property, e.g.

Wrong:

client.Host = "mail.someco.com:10000";

Correct:

client.Host = "mail.someco.com";
client.Port = 10000;
like image 142
Ben Robinson Avatar answered Sep 16 '22 15:09

Ben Robinson