.NET Framework 內建 System.Web.Mail 命名空間 (注意:.NET 2.0+ 建議改用 System.Net.Mail),讓郵件發送變得極簡單。本文以 .NET 1.1 經典 API 為例,示範完整發送流程。
SmtpMail.SmtpServer),提供靜態 Send 方法。SmtpMail.Send("from@test.com", "to@test.com", "主旨", "純文字內容");
適用:簡單通知、無附件、無 HTML 格式需求。
MailMessage msg = new MailMessage();
msg.From = "sender@company.com";
msg.To = "receiver@company.com";
msg.Subject = "測試 HTML 郵件";
msg.BodyFormat = MailFormat.Html;
msg.Body = "<h1>你好</h1><p>這是<b>HTML</b>郵件</p>";
msg.Priority = MailPriority.High;
msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // 基本認證
msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendusername", "smtp_user");
msg.Fields.Add("https://schemas.microsoft.com/cdo/configuration/sendpassword", "smtp_pwd");
SmtpMail.SmtpServer = "smtp.company.com:25";
SmtpMail.Send(msg);
| 屬性 | 說明 | 範例值 |
|---|---|---|
| BodyFormat | 郵件格式 | MailFormat.Html / MailFormat.Text |
| Priority | 優先級 | High / Normal / Low |
| Attachments | 附件集合 | msg.Attachments.Add(new MailAttachment("file.pdf")) |
| Fields | CDO 設定 (認證、SSL、Port 等) | 見上方程式碼 |
System.Net.Mail.SmtpClient,API 更完整、支援 SSL)。幾行代碼即可整合郵件通知、密碼重設、訂單確認等核心功能,是 ASP.NET 應用不可或缺的基礎設施。