开发者

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

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 applicati开发者_如何学编程on.

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);


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;


If you are using any antivirus software check it's log to see whether it is because of the antivirus. I faced same problem when McAffee was blocking my mails (there is a security policy - Prevent mass mailing worms from sending mails). Edit this policy and add your application to the exception list. In my case this sorted the problem. Please check if it works for you.


You are sure the SmtpServer property of the System.Web.Mail.SmtpMail is set in the old code? It was possible to use it without setting this property and then it used the local server's SMTP service. System.Web.Mail.SmtpMail.SmtpServer


Have u check the web.config file for smtp host configuration? You should copy the config section over to your app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜