首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> ASP.NET 基礎

ASP.NET Web Pages Email 郵件發送實戰:WebMail 幫助器完整應用

     編輯:ASP.NET 基礎

ASP.NET Web Pages - WebMail 幫助器實戰:建立支援請求郵件發送系統


WebMail 幫助器封裝 SMTP 協定,提供簡潔 API 實現網頁表單郵件發送。本教學將建立完整的「支援請求」表單流程:輸入頁面 → 伺服器處理 → 郵件發送 → 結果回饋。


前置準備:SMTP 服務與 _AppStart 初始化

在網站根目錄 _AppStart.cshtml 設定 WebMail 屬性 (應用程式啟動自動執行):

@{
 // 現有 WebSecurity 初始化 (若有)
 WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);

 // WebMail SMTP 設定
 WebMail.SmtpServer = "smtp.example.com";
 WebMail.SmtpPort = 587;
 WebMail.EnableSsl = true;
 WebMail.UserName = "support@example.com";
 WebMail.Password = "your-app-password";
 WebMail.From = "noreply@example.com";
}

屬性詳細說明:

  • SmtpServer:SMTP 伺服器主機名 (Gmail: smtp.gmail.com)
  • SmtpPort:587 (TLS/STARTTLS)、465 (SSL)、25 (非加密,不建議)
  • EnableSsl:true 強制加密傳輸,保護帳號密碼與郵件內容
  • UserName/Password**:SMTP 驗證憑證 (Gmail 需應用程式密碼)
  • From**:寄件人顯示 Email,建議與驗證帳號一致

步驟一:建立郵件輸入頁面 (Email_Input.cshtml)

@{
 Layout = "~/_Layout.cshtml";
 Page.Title = "技術支援請求";
}
<!DOCTYPE html>
<html>
<head>
 <title>@Page.Title</title>
</head>
<body>
<h1>Request for Assistance</h1>

<form method="post" action="Email_Send.cshtml">
 <div>
 <label for="customerEmail">Your Email:</label>
 <input type="email" name="customerEmail" id="customerEmail" required />
 </div>
 <div>
 <label for="customerRequest">Problem Details:</label>
 <textarea name="customerRequest" id="customerRequest" cols="50" rows="6" required></textarea>
 </div>
 <p><input type="submit" value="Submit Request" /></p>
</form>
</body>
</html>

關鍵點:

  • action="Email_Send.cshtml":表單提交目標處理頁面
  • method="post":使用 POST 方法傳送資料
  • required:HTML5 瀏覽器端基本驗證
  • type="email":瀏覽器自動驗證 Email 格式

步驟二:建立郵件發送處理頁面 (Email_Send.cshtml)

@{
 Layout = "~/_Layout.cshtml";
 Page.Title = "請求已送出";

 // 讀取表單輸入
 var customerEmail = Request["customerEmail"];
 var customerRequest = Request["customerRequest"];

 // 基本伺服器端驗證
 if (String.IsNullOrEmpty(customerEmail) || String.IsNullOrEmpty(customerRequest)) {
 <p class="error">請填寫所有必填欄位。</p>
 } else {
 try {
 // 發送郵件至支援團隊
 WebMail.Send(
 to: "support-team@example.com",
 subject: "支援請求 - " + customerEmail,
 body: "來自: " + customerEmail + "\n\n問題描述:\n" + customerRequest,
 isBodyHtml: false
 );
 <p class="success">您的支援請求已成功送出,我們將盡快回覆。</p>
 } catch (Exception ex) {
 // 生產環境應記錄日誌而非直接顯示
 <p class="error">郵件發送失敗: @ex.Message</p>
 // Log.Error(ex, "WebMail send failed for {Email}", customerEmail);
 }
 }
}

進階強化:HTML 格式郵件、附件、抄送

WebMail.Send(
 to: "support@example.com",
 subject: "支援請求 - " + customerEmail,
 body: @<div>
 <h2>新支援請求</h2>
 <p><strong>來自:</strong> @customerEmail</p>
 <p><strong>時間:</strong> @DateTime.Now</p>
 <div style="background:#f5f5f5;padding:10px;">@customerRequest</div>
 </div>,
 isBodyHtml: true,
 cc: "manager@example.com",
 filesToAttach: new[] { Server.MapPath("~/App_Data/logs/latest.log") }
);

錯誤處理與日誌記錄最佳實務

@using System.Diagnostics
@functions {
 public static void LogEmailError(Exception ex, string email, string context) {
 var eventLog = new EventLog("Application") { Source = "WebPagesApp" };
 eventLog.WriteEntry(
 $"WebMail Error | Context: {context} | Email: {email} | Error: {ex.Message}\nStack: {ex.StackTrace}",
 EventLogEntryType.Error
 );
 }
}
try {
 WebMail.Send(...);
} catch (SmtpException smtpEx) {
 LogEmailError(smtpEx, customerEmail, "SMTP Protocol Error");
} catch (Exception ex) {
 LogEmailError(ex, customerEmail, "General Error");
}

參考資源

  • 完整 API 參考:WebMail 物件參考手冊
  • SMTP 設定速查:Gmail/Outlook/SendGrid/Mailgun 設定參數
  • 進階主題:非同步發送、佇列機制、退信處理、DMARC 合規

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved