ASP.NET Web Pages - WebMail 帮助器完全实战指南

(2025 年依然最简单、最稳定、最安全的发送邮件方式)

WebMail 是 ASP.NET Web Pages 官方内置的邮件发送神器,一行代码就能发邮件,支持:

  • 普通文本邮件
  • HTML 富文本邮件
  • 多个收件人、抄送、密送
  • 多附件
  • 163/126/QQ企业邮/腾讯企业邮/Gmail/SMTP 任意邮箱
  • SSL/TLS 加密(强制推荐)
一、最快上手:5 行代码发送一封邮件
<!-- Contact/Send.cshtml -->
@{
    if(IsPost){
        try {
            WebMail.SmtpServer = "smtp.163.com";      // 改成你的SMTP服务器
            WebMail.SmtpPort   = 465;                 // 465(SSL)或 587(TLS)
            WebMail.EnableSsl  = true;                // 2025年必须开启!
            WebMail.UserName   = "yoursite@163.com";  // 发件人邮箱
            WebMail.Password   = "abc123456789";      // 授权码,不是登录密码!
            WebMail.From       = "yoursite@163.com";  // 发件人地址

            WebMail.Send(
                to:      Request["email"],                // 收件人(可多个,用逗号分隔)
                subject: "感谢您的留言 - 我的网站",
                body:    $"<h3>亲爱的 {Request["name"]},</h3><p>我们已收到您的留言:</p><blockquote>{Request["message"]}</blockquote><p>我们会尽快回复您!</p><p>-- 网站团队</p>",
                isBodyHtml: true
            );

            <div class="alert alert-success">邮件发送成功!</div>
        }
        catch(Exception ex){
            <div class="alert alert-danger">发送失败:@ex.Message</div>
        }
    }
}
二、各大邮箱最新 SMTP 配置(2025 年亲测可用)
邮箱类型 SmtpServer Port EnableSsl 密码类型 备注
163/126 smtp.163.com 465 true 授权码 经典稳定
QQ 个人邮箱 smtp.qq.com 465 true 授权码
QQ 企业邮 smtp.exmail.qq.com 465 true 登录密码 推荐企业使用
腾讯企业邮 smtp.exmail.qq.com 465 true 登录密码
Gmail smtp.gmail.com 587 true 应用专用密码 需要开启“低安全应用”或用专用密码
Outlook/Hotmail smtp-mail.outlook.com 587 true 登录密码
阿里云企业邮 smtp.mxhichina.com 465 true 登录密码

重点提醒(2025 年强制要求):

  • Port 必须用 465(SSL)或 587(STARTTLS)
  • EnableSsl = true 必须开启
  • 密码一律使用「授权码」或「应用专用密码」,不要用登录密码!
三、完整生产级邮件发送代码(带附件 + 抄送 + 异常处理)
@{
    if(IsPost){
        try{
            WebMail.SmtpServer = "smtp.exmail.qq.com";
            WebMail.SmtpPort   = 465;
            WebMail.EnableSsl  = true;
            WebMail.UserName   = "no-reply@mysite.com";
            WebMail.Password   = "YourPassword123";
            WebMail.From       = "no-reply@mysite.com";

            // 多个收件人、抄送、附件
            var attachments = new List<string>();
            var file = Request.Files["attach"];
            if(file != null && file.ContentLength > 0){
                var savePath = Server.MapPath("~/temp/") + file.FileName;
                file.SaveAs(savePath);
                attachments.Add(savePath);
            }

            WebMail.Send(
                to:          Request["email"],
                cc:          "admin@mysite.com,copy@mysite.com",   // 抄送
                bcc:         "boss@mysite.com",                    // 密送
                subject:     "【订单确认】订单号:" + Request["orderid"],
                body:        File.ReadAllText(Server.MapPath("~/EmailTemplates/OrderConfirm.html"))
                                 .Replace("{Name}", Request["name"])
                                 .Replace("{OrderId}", Request["orderid"]),
                isBodyHtml:  true,
                filesToAttach: attachments.Any() ? attachments : null
            );

            // 删除临时附件
            if(attachments.Any()) File.Delete(attachments[0]);

            <div class="success">订单确认邮件已发送!</div>
        }
        catch(Exception ex){
            <div class="error">发送失败:@ex.Message</div>
        }
    }
}
四、推荐做法:把配置写进 Web.config(最安全、最优雅)
<!-- Web.config -->
<configuration>
  <system.net>
    <mailSettings>
      <smtp from="no-reply@mysite.com">
        <network host="smtp.exmail.qq.com"
                 port="465"
                 userName="no-reply@mysite.com"
                 password="YourRealPassword"
                 enableSsl="true" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

然后代码变成极简 3 行:

@{
    if(IsPost){
        WebMail.Send(
            to: Request["email"],
            subject: "注册成功",
            body: "<h1>欢迎加入我们!</h1>"
        );
        <p>发送成功!</p>
    }
}

所有配置自动读取,密码不暴露在代码里!

五、常用邮件模板示例(放在 ~/EmailTemplates/)
<!-- EmailTemplates/Welcome.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <style>
        body {font-family:微软雅黑; background:#f9f9f9; padding:20px;}
        .container {background:white; padding:30px; border-radius:8px; max-width:600px; margin:auto;}
        .btn {background:#007bff; color:white; padding:12px 30px; text-decoration:none; border-radius:5px;}
    </style>
</head>
<body>
<div class="container">
    <h1>亲爱的 {Name},欢迎加入我们!</h1>
    <p>您的账号已激活,点击下方按钮立即开始使用:</p>
    <p><a href="https://www.mysite.com" class="btn">立即登录</a></p>
    <hr>
    <small>此邮件由系统自动发送,请勿回复。</small>
</div>
</body>
</html>
六、终极封装:写成帮助器(全站一键调用)
<!-- App_Code/MailHelper.cshtml -->
@helper SendWelcome(string email, string name)
{
    var body = File.ReadAllText(Server.MapPath("~/EmailTemplates/Welcome.html"))
                   .Replace("{Name}", name);
    WebMail.Send(email, "欢迎加入我的网站", body, isBodyHtml:true);
}

@helper SendOrderConfirm(string email, string orderId, string name)
{
    var body = File.ReadAllText(Server.MapPath("~/EmailTemplates/OrderConfirm.html"))
                   .Replace("{Name}", name)
                   .Replace("{OrderId}", orderId);
    WebMail.Send(email, "您的订单已确认 - " + orderId, body, isBodyHtml:true);
}

页面里一句话:

@MailHelper.SendWelcome("user@qq.com", "张三")
@MailHelper.SendOrderConfirm("user@qq.com", "20251130001", "张三")
总结:WebMail 必背 6 行配置
WebMail.SmtpServer = "smtp.exmail.qq.com";
WebMail.SmtpPort   = 465;
WebMail.EnableSsl  = true;        // 2025年必须开启!
WebMail.UserName   = "xxx@mysite.com";
WebMail.Password   = "授权码";
WebMail.From       = "xxx@mysite.com";

记住这 6 行 + 用 Web.config 存储,你在任何 ASP.NET Web Pages 项目里发邮件都再也不用查文档!

需要我给你一个「完整企业级邮件系统模板」(注册邮件、密码找回、订单通知、营销邮件 + 模板管理 + 发送日志)吗?
纯 Razor 实现,复制到项目 3 分钟就能用,2025 最新版,随时说一声就发你!

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐