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.comDescription: 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.

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.
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 : ASP.NET, IIS, SMTP
Del.icio.us : ASP.NET, IIS, SMTP


你好,
我现在遇到发送邮件方面的困难,希望能得到你的帮助.
我刚开始接受一个网站,是以基本html网页为主,除了一个contact form.原先的设计是把form作为邮件发送到一个网站管理员的信箱.不知什么人设计的vb.net代码,据说以前正常工作.现在的问题是,对于某些邮箱,这个代码一切正常,发送form后可以收到邮件,但对于某些邮箱,就会出现runtime error网页.(令人头疼的是,这些不能正常工作的收件箱正是这个网站公司的邮箱!) 代码不复杂,大概如下:
……
Dim str_body As New StringBuilder
Dim msg As New MailMessage
msg.From = New MailAddress(Request.Form(”email”))
msg.To.Add(New MailAddress(”info@website.com”)) ‘
[
Reply ]
msg.Subject = “Web Contact Form”
str_body.Append(”name:” & Request.Form(”name”) & “,” )
str_body.Append(”company:” & Request.Form(”company”) & “,” )
str_body.Append(”email” & Request.Form(”email”))
msg.Body = str_body.ToString
msg.IsBodyHtml = True
Dim smtp As New SmtpClient(”127.0.0.1″, “25″)
smtp.Send(msg)
……
1 如果我用类似”xxx@website.com”的邮箱,代码出错;
2 如果我用其他邮箱,比如我已经试过的”xxx@gmail.com”, 或另一个公司的邮箱,一切正常,可以顺利收到form的邮件.
我对.net不太熟悉,以前是用asp的.但现在面临着这个较急的问题,只好在网上请求帮助了. 请帮我判断:可能是什么原因? 如果从以上情况无法确定,那么我怎么去查明原因,检查,或测试? 请尽可能多给我提供一些建议!
谢谢!
[
Reply ]
I did not get a chance to test your ASP.net yet. But I doubt that your problem might not be about your ASP.net code since your can send out forms to some email addresses. Please double check your SMTP service set up and firewall. To test, you can add the domain name of your problem email address as the alien domain name in your SMTP service. Let me know the testing result.
[
Reply ]