我们经常看到在一些博客页面里可以发表评论, 通过电子邮件通知版主(例如咱们的博客园),那么如何用asp.net发送电子邮件呢?我们可以借助.NET提供的强大的类库达到目的。
首先我们要引入 System.Web.Mail 命名空间。
然后创建一个MailMessage类的实例。见如下代码:
'最后使用 SmtpMail 类的静态方法Send发送
首先我们要引入 System.Web.Mail 命名空间。
然后创建一个MailMessage类的实例。见如下代码:
1
'创建一个实例
2
Dim objMM as New MailMessage()
3![](/Images/OutliningIndicators/None.gif)
4
'设置其属性
5
objMM.To = "someone@someaddress.com"
6
objMM.From = "someoneelse@someotheraddress.com"
7
objMM.Cc = "someone2@someaddress.com"
8
objMM.Bcc = "someoneElse@someaddress.com"
9![](/Images/OutliningIndicators/None.gif)
10
'以Text方式发送
11
objMM.BodyFormat = MailFormat.Text
12
'或者以Html方式发送
13
objMM.BodyFormat = MailFormat.Html
14![](/Images/OutliningIndicators/None.gif)
15
'设置邮件的优先级
16
objMM.Priority = MailPriority.Normal
17![](/Images/OutliningIndicators/None.gif)
18
'设置主题
19
objMM.Subject = "Hello there!"
20
'邮件正文
21
objMM.Body = "Hi!" & vbCrLf & vbCrLf & "How are you doing?"
22![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/None.gif)
'最后使用 SmtpMail 类的静态方法Send发送
SmtpMail.Send(objMM)
有的邮件服务器在发送邮件时需要再次认证(一般是为了防治垃圾邮件的蔓延),例如163免费邮箱,那么如果是.net1.1以上的版本,可以加入:
objMM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
objMM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "your_username_here"); //set your username
objMM.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your_password_here"); // set your password
在ASP.NET2.0中,MailMessage类已移到System.Net.Mail命名空间下,其发送email可参见:
http://aspnet.4guysfromrolla.com/articles/072606-1.aspx
http://aspnet.4guysfromrolla.com/articles/080206-1.aspx
Sending HTML Mail with Embedded Image in .NET
另外给大家介绍一个专门讨论System.Web.Mail的网站:
http://www.systemwebmail.com/default.aspx
如果是.NET2.0,可以访问有关讨论System.Net.Mail网址:
![](/images/cnblogs_com/happyhzs2000/qsygsl.jpg)