在開發系統時,常要用系統通知寄件來通知給使用都訊息,如有人回應了你的訊息或系統通知開會時間等等…
在Web.config加入
Imports System.Net.Mail
Public Sub SendMailList(ByVal usermail As String, ByVal txtbody As String, Optional ByVal PTitle As String = Nothing, Optional ByVal SendUser As String = Nothing)
'寄件人Mail地址
Dim MailFrom As String = System.Configuration.ConfigurationManager.AppSettings("MailFrom")
'寄件抬頭
Dim MailFromTitle As String = System.Configuration.ConfigurationManager.AppSettings("MailFromTitle")
'寄出的主機
Dim Host As String = System.Configuration.ConfigurationManager.AppSettings("Host")
'寄出使用的帳號
Dim Username As String = System.Configuration.ConfigurationManager.AppSettings("Username")
'寄出所用帳號的密碼
Dim Password As String = System.Configuration.ConfigurationManager.AppSettings("Password")
'建立信件
Dim mail As MailMessage = New MailMessage()
Dim mailtitle As String = ""
'建立寄出設定
Dim SmtpMail As New SmtpClient()
'信件加入寄件人和抬頭
mail.To.Add(New MailAddress(usermail))
'信件加入收件人Mail地址
mail.From = New MailAddress(MailFrom, MailFromTitle)
'寄出通知設定
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.None
'信件的重要性
mail.Priority = MailPriority.High
'主旨編碼設定
mail.SubjectEncoding = System.Text.Encoding.UTF8
'內容編碼設定
mail.BodyEncoding = System.Text.Encoding.UTF8
'主旨
mail.Subject = "系統通知" & PTitle + SendUser
'是否使用Html編寫內容
mail.IsBodyHtml = True
'內容
mail.Body = mailtitle + "<P>" & txtbody & "</P>"
'是否使用憑證
SmtpMail.UseDefaultCredentials = False
'驗證帳號密碼
SmtpMail.Credentials = New Net.NetworkCredential(Username, Password)
'傳送方式
SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network
'寄出主機
SmtpMail.Host = Host
'寄出
SmtpMail.Send(mail)
End Sub