[习题]透过 ASP.NET寄信(发信、发Mail),System.Net.Mail.MailDefinition 类别
关于透过 ASP.NET寄信(发信、发Mail),这两篇文章很值得参考:
System.Web.Mail升级至System.Net.Mail的第一个问题
http://www.dotblogs.com.tw/jeff-yeh/archive/2008/04/09/2656.aspx
System.Web.Mail 太阳春、System.Net.Mail 有问题
http://blog.miniasp.com/post/2008/02/05/SystemWebMail-too-weak-and-SystemNetMail-has-problem.aspx
今天我在 MSDN找到 MailDefinition 类别
--允许控制项从文字档或字串建立电子邮件讯息,此类别无法被继承。
http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.maildefinition.aspx
命名空间: System.Web.UI.WebControls
组件: System.Web (在 System.Web.dll 中)
控制项可以使用 MailDefinition 类别,从包含电子邮件讯息主体的 文字档或 字串建立 MailMessage 物件。
MailDefinition 类别可以用于简化建立要由控制项传送之预先定义的电子邮件讯息。
如果您 不 想使用控制项传送电子邮件,请参阅 System.Net.Mail 类别。
由 MailDefinition 类别建立的 MailMessage 物件使用 SmtpClient 类别的 Send 方法传送。
若要能够传送电子邮件,您必须在 Web.config 档中设定 SMTP 电子邮件伺服器。
Web.Config档。 下面「黄色底色」的地方,可以有三种设定。
请看 http://msdn.microsoft.com/zh-tw/library/ms164240.aspx
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network host="localhost" port="25" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
</configuration>
MSDN提供的这个范例不错。(VB、C#都有)
有两个功能:
1. 按一下 [建立电子邮件并仅显示讯息内容],以建立电子邮件讯息并在 Web 网页中显示 MailMessage 物件的属性。
2. 按一下 [建立电子邮件并且传送],以在 Web 网页中显示电子邮件讯息,并使用网际网路电子邮件将讯息传送至收件者。
范例都是来自 微软 MSDN网站 -- http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.maildefinition.aspx
小弟只是为了自己的需要,作了一些注解而已。
'== 重点!! ================
'-- 必须自己宣告 NameSpace!!
Imports System.Net.Mail '-- MailMessage会用到。
Imports System.Reflection
Imports System.Collections.Specialized
'=========================
'== 建立电子邮件并仅显示讯息内容
Protected Sub createEMail_Click(sender As Object, e As System.EventArgs) Handles createEMail.Click
Dim msg As MailMessage = CreateMessage()
'-- System.Net.Mail.MailMessage类别
'-- MailMessage类别的执行个体 (Instance) 是用来建构电子邮件讯息,而这些讯息会使用 SmtpClient类别传送到 SMTP Server进行传递。
'-- 参考来源 http://msdn.microsoft.com/zh-tw/library/system.net.mail.mailmessage(v=VS.100).aspx
PlaceHolder1.Controls.Add(ShowMessage(msg)) '-- 显示 E-Mail讯息内容
End Sub
'== 建立电子邮件并且传送
Protected Sub sendEMail_Click(sender As Object, e As System.EventArgs) Handles sendEMail.Click
Dim msg As MailMessage = CreateMessage()
PlaceHolder1.Controls.Add(ShowMessage(msg))
'--若要能够传送电子邮件,您必须在 Web.config 档中设定 SMTP 电子邮件伺服器。
'--如需详细资讯,请参阅 http://msdn.microsoft.com/zh-tw/library/ms164240.aspx
Try
Dim sc As SmtpClient = New SmtpClient()
sc.Send(msg) '-- 传送 E-Mail
Catch ex As Exception
errorMsg.Text = ex.ToString()
End Try
End Sub
'===========================================================
'== 显示 讯息内容
Function ShowMessage(ByVal msg As MailMessage) As HtmlTable
Dim table As HtmlTable = New HtmlTable
Dim topRow As HtmlTableRow = New HtmlTableRow
Dim fieldHeaderCell As HtmlTableCell = New HtmlTableCell
Dim valueHeaderCell As HtmlTableCell = New HtmlTableCell
fieldHeaderCell.InnerText = "Field"
topRow.Cells.Add(fieldHeaderCell)
valueHeaderCell.InnerText = "Value"
topRow.Cells.Add(valueHeaderCell)
table.Rows.Add(topRow)
For Each p As PropertyInfo In msg.GetType().GetProperties()
Dim row As HtmlTableRow = New HtmlTableRow
Dim labelCell As HtmlTableCell = New HtmlTableCell
Dim valueCell As HtmlTableCell = New HtmlTableCell
If (Not ((p.Name = "Headers") Or _
(p.Name = "Fields") Or _
(p.Name = "Attachments"))) Then
labelCell.InnerText = String.Format("{0}", p.Name)
row.Cells.Add(labelCell)
valueCell.InnerText = String.Format("{0}", p.GetValue(msg, Nothing))
row.Cells.Add(valueCell)
End If
table.Rows.Add(row)
Next
Return table '-- 回传一个 ASP.NET的 HTML表格
End Function
Function CreateMessage() As MailMessage
Dim md As MailDefinition = New MailDefinition
'-- 参考资料: http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.maildefinition.aspx
md.BodyFileName = sourceMailFile.Text '--文字档的档名
md.CC = sourceCC.Text
md.From = sourceFrom.Text
md.Subject = sourceSubject.Text
If sourcePriority.SelectedValue = "Normal" Then
md.Priority = MailPriority.Normal
ElseIf sourcePriority.SelectedValue = "High" Then
md.Priority = MailPriority.High
ElseIf sourcePriority.SelectedValue = "Low" Then
md.Priority = MailPriority.Low
End If
Dim replacements As ListDictionary = New ListDictionary
'-- 事先定义了两个字串<%To%>与<%From%>,代表收件人、发信人。
replacements.Add("<%To%>", sourceTo.Text)
replacements.Add("<%From%>", sourceFrom.Text)
If useFile.Checked Then
Dim fileMsg As MailMessage
fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, Me)
'-- 从 [文字档]建立要透过 SMTP传送的电子邮件讯息。
'-- http://msdn.microsoft.com/zh-tw/library/0002kwb2.aspx
'recipients型别: System.String()
' 讯息收件者的逗号分隔清单。
'replacements型别: System.Collections.IDictionary()
' IDictionary,包含字串及其取代字串的清单。
'owner型别: System.Web.UI.Control()
' 拥有这个 MailDefinition 的 Control。 VB语法为Me,C#语法为 this。
Return fileMsg
Else
Dim textMsg As MailMessage
textMsg = md.CreateMailMessage(sourceTo.Text, replacements, sourceBodyText.Text, Me)
'-- 使用 [文字]建立要透过 SMTP传送的电子邮件讯息。
'-- http://msdn.microsoft.com/zh-tw/library/ms156159.aspx
'recipients型别: System.String()
' 收件者的逗号分隔清单。
'replacements型别: System.Collections.IDictionary()
' IDictionary,包含字串及其取代字串的清单。
'body型别: System.String()
' 电子邮件讯息的文字。
'owner型别: System.Web.UI.Control()
' 拥有这个 MailDefinition 的 Control。 VB语法为Me,C#语法为 this。
Return textMsg '-- 回传值的「型态」System.Net.Mail.MailMessage
End If
End Function
**********************************************************************************
很多人想透过 Web程式来寄发「电子报」、「会员通知信」
建议您看看这篇文章
很多问题,不是IT的问题能解的....... Orz
***********************************************************************************
ASP.NET (Web Form) 远距教学、课程完整内容 影片上网!
欢迎在家自修、学习
***本课程是「事先录影」的影片。缴费后就能观赏!***
缴费后,看完第一天课程 。如果不满意,不需理由,可全额退费!
我将思想传授他人, 他人之所得,亦无损于我之所有;
犹如一人以我的烛火点烛,光亮与他同在,我却不因此身处黑暗。----Thomas Jefferson
写信给我-- mis2000lab (at) yahoo.com.台湾 或是 school (at) mis2000lab.net
................ Google+ https://plus.google.com/100202398389206570368/posts ........
................ YouTube (ASP.NET) 线上教学影片 http://goo.gl/rGLocQ