Friday, July 6, 2007

How to fix issues of sending email in ASP.net 2.0 with localhost Microsoft SMTP service

ASP.NETWith the upgrading to ASP.net 2.0, I decided to change all my sending email codes too. Basically I am going to use system.net.mail instead of system.web.mail or traditional CDONT. I thought it would be an easy task, but I still encountered some issues.

I use localhost as my SMTP host and anonymous connection in the web.config file as below.

<configuration>
<!-- Add the email settings to the <system.net> element -->
<system.net>
<mailSettings>
<smtp>
<network
host="localhost"
port="25"
/>
</smtp>
</mailSettings>
</system.net>

<system.web>
...
</system.web>
</configuration>

And the following is my VB.net code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
Const ToAddress As String = "myname@mycompany.com"
Const UsersEmail As String = "webmaster@mycompany.com"

'(1) Create the MailMessage instance
Dim mm As New MailMessage(UsersEmail, ToAddress)

'(2) Assign the MailMessage's properties
mm.Subject = "Hello, world!"
mm.Body = "This is a message body within ASP.NET."
mm.IsBodyHtml = False

'(3) Create the SmtpClient object
Dim smtp As New SmtpClient

'(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(mm)
End Sub

First, take care of SMTP disabled issue

When I ran the code, and I got the following error message:


An established connection was aborted by the software in your host machine

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine

Since I knew I was testing on my own desktop and there should be some port disabled issue, especially my company is using McAfee Enterprise anti-virus system. I opened McAfee VirusScan Console and found out the outbound email function (or Port 25) has been disabled on my workstation as part of enterprise policy. To go ahead to test the email feature on my machine, I had to uncheck the first checkbox as you can see from the illustrate below.

VirusScan Console

If you have the same issue, either you remove this rule from McAfee, or change your SMTP server's port number to other available numbers.


2), Fix the unable relaying messages issue

Another error message appeared as the following after I removed the SMTP port rule from McAfee.

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for myname@mycompany.com

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Unable to relay for myname@mycompany.com

I do not know what exactly network setup caused this problem (probably my workstation is not in the same subnet with my mail server), but I had the same experience before in traditional asp 3.0 web pages. So I took the same steps to resolve this problem.

  1. Open IIS administrative console by clicking Control Panel --> Administrative Tools.
  2. Expand Default SMTP Virtual Server and right click Domains --> New --> Domain...
  3. On next screen, specify the domain type is Remote.
  4. Type your new domain name. In this case, it should match your mail recipient's domain name, for example mycompany.com and click Finish.
  5. Select the new domain you just created, and right click to open Proprieties window.
  6. Check the first check box to Allow incoming mail to be relayed to this domain.
  7. blog_smtp_domain.jpg
If your machine can know how to connect to your company's mail server, then you are done here. Otherwise, you should put your relaying mail server's IP or server name in Forward all mail to smart host box.
Please remember, when you enter IP address in the forward box, please use [] to enclose the IP address (like in [192.168.2.1] formate).

To make sure your company's mail server (in most case, it is Exchange server) can relay all emails generated from your work stations or web servers, 1) add the name domain in SMTP service as "*.com" or "*.net"; 2) or add your mail server as the Smart host.

blog_smtp_smarthost.jpg

Note: You might need to make such above modifications on your testing working station, and it might not be necessary applicable for your production web servers.

My Next Step

Of course, since there are too many issues to use localhost to send out emails in ASP.net 2.0, it is not recommended to do so. Next task for me is to connect to my exchange server to send emails from ASP.NET. I will post it soon. Thanks for reading.

Technorati : , ,
Del.icio.us : , ,

Sponsor Ad.