ASP.NET MVC实现邮件发送(包含附件)

首先创建一个Model类来存放数据,

    public class MailModel
    {
        [Required(ErrorMessage = "Please enter the receiver")]
        public string To { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
    }

然后创建Controller,

    public class SendMailerController : Controller
    {
        // GET: SendMailer
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Send Mail with Gmail
        /// </summary>
        /// <param name="objModelMail">MailModel Object, keeps all properties</param>
        /// <param name="fileUploader">Selected file data, example-filename,content,content type(file type- .txt,.png etc.),length etc.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(Attach_a_file_to_Email.Models.MailModel objModelMail, HttpPostedFileBase fileUploader)
        {
            if (ModelState.IsValid)
            {
                string from = "XXX@gmail.com";
                using (MailMessage mail = new MailMessage(from, objModelMail.To))
                {
                    mail.Subject = objModelMail.Subject;
                    mail.Body = objModelMail.Body;
                    if (fileUploader != null)
                    {
                        string fileName = Path.GetFileName(fileUploader.FileName);
                        mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                    }
                    mail.IsBodyHtml = false;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential networkCredential = new NetworkCredential(from, "p@ssw3rd");
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = networkCredential;
                    smtp.Port = 587;
                    smtp.Send(mail);
                    ViewBag.Message = "Sent";
                    return View("Index", objModelMail);
                }
            }
            else
            {
                return View();
            }
        }
    }

在对应的View中新建Index.cshtml,

@model Attach_a_file_to_Email.Models.MailModel
@{
    ViewBag.Title = "Index";
}

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        if ('@ViewBag.Message' == 'Sent') {
            alert('Mail has been sent successfully');
        }
    });
</script>

<h2>Index</h2>
<fieldset>
    <legend>
        Send Email
    </legend>

    @using (@Html.BeginForm("Index", "SendMailer", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
    {
        <input type="submit" value="Send" />
        <table>
            <tr>
                <td>
                    To:
                </td>
                <td>
                    @Html.TextBoxFor(m => m.To)
                    @Html.ValidationMessageFor(m => m.To)
                </td>
            </tr>
            <tr>
                <td>
                    Subject:
                </td>
                <td>
                    @Html.TextBoxFor(m => m.Subject)
                </td>
            </tr>
            <tr>
                <td>
                    Attachment
                </td>
                <td>
                    <input type="file" name="fileUploader" /> @*和HttpPostedFileBase一致*@
                </td>
            </tr>
            <tr>
                <td>
                    Body:
                </td>
                <td>
                    @Html.TextAreaFor(m => m.Body)
                </td>
            </tr>
        </table>
    }
</fieldset>
原文地址:https://www.cnblogs.com/jizhiqiliao/p/13093216.html