ASP.NET 1.x/2.0 使用 System.Web.Mail.SmtpMail 與 MailMessage 發送郵件(.NET 2.0+ 建議改用 System.Net.Mail 命名空間)。
System.Web.Mail 命名空間MailMessage 設定 From、To、Subject、Body、Priority、BodyFormat 等屬性mail.Attachments.Add(new MailAttachment(filePath))SmtpMail.SmtpServer = "smtp.example.com"SmtpMail.Send(mail)Imports System.Web.Mail
Dim mail As New MailMessage()
mail.From = "sender@example.com"
mail.To = "recipient@example.com"
mail.Subject = "測試郵件"
mail.Body = "<b>HTML 內容</b>"
mail.BodyFormat = MailFormat.Html
mail.Priority = MailPriority.High
mail.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.example.com")
mail.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
mail.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendusername", "user")
mail.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendpassword", "pass")
' 附件
mail.Attachments.Add(New MailAttachment(Server.MapPath("~/files/report.pdf")))
SmtpMail.Send(mail)
.NET 2.0 起請改用 System.Net.Mail.MailMessage、SmtpClient,支援 SSL、非同步發送、更豐富的編碼控制。