搞了许久,在网上也看个很多代码!终于给我弄出来了!拿出来跟大家分享一下
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();
}
}