MVC 3 Web Mail

本文详细介绍了如何使用ASP.NET MVC框架实现邮件发送功能,包括配置SMTP服务器、编写视图和控制器代码,并提供了完整的代码示例。通过实践操作,读者可以学会将邮件功能集成到自己的应用中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搞了许久,在网上也看个很多代码!终于给我弄出来了!拿出来跟大家分享一下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApp.Models
{
    public class MailTestModel
    {
        public string MailAddress { get; set; }

        public string MailTitle { get; set; }

        public string MailContent { get; set; }
    }
}

这是model


@model MvcApp.Models.MailTestModel
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
@using (Html.BeginForm())
{
    <label>
        邮件标题:</label>@Html.TextBoxFor(model => model.MailTitle)
    <label>
        要发送的邮件地址:</label> @Html.TextBoxFor(model => model.MailAddress)
    <label>
        邮件内容:</label>@Html.TextBoxFor(model => model.MailContent)
    
    <input type="submit" value="提交" />
}

这个是view视图

 //
        // GET: /WebMailTest/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(MailTestModel test)
        {
            WebMail.SmtpServer = "smtp.163.com";//获取或设置要用于发送电子邮件的 SMTP 中继邮件服务器的名称。
            WebMail.SmtpPort = 25;//发送端口
            WebMail.EnableSsl = true;//是否启用 SSL GMAIL 需要 而其他都不需要 具体看你在邮箱中的配置
            WebMail.UserName = "ludingcheng0923";//账号名
            WebMail.From = "ludingcheng0923@163.com";//邮箱名
            WebMail.Password = "******";//密码
            WebMail.SmtpUseDefaultCredentials = true;//是否使用默认配置

            try
            {
                // Send email 
                WebMail.Send(to: test.MailAddress,
                             subject: test.MailTitle,
                             body: test.MailContent

                    //,cc: "抄送"
                    //   ,filesToAttach: filesPaths
                    //      , isBodyHtml: true,
                    //additionalHeaders:new string[] { "additionalHeaders1", "additionalHeaders2" }
                    );
            }
            catch (Exception e)
            {

                Response.Write(e.ToString());
            }
            return View();
        }

    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值