With 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.
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.
- Open IIS administrative console by clicking Control Panel --> Administrative Tools.
- Expand Default SMTP Virtual Server and right click Domains --> New --> Domain...
- On next screen, specify the domain type is Remote.
- Type your new domain name. In this case, it should match your mail recipient's domain name, for example mycompany.com and click Finish.
- Select the new domain you just created, and right click to open Proprieties window.
- Check the first check box to Allow incoming mail to be relayed to this domain.
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.
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.