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

由于.Net Core中不再支持HttpPostedFileBase类,因此文件的上传可以使用IFormFile代替。

具体方法如下:

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

    public class EmailModel
    {
        public string To { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public IFormFile Attachment { get; set; }
    }

此处的附件AttachMent类型就是IFormFile。

然后创建Controller,

    public class SendEmailController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Index(EmailModel model)
        {
            using (MailMessage mm = new MailMessage("XXX@gmail.com", model.To))
            {
                mm.Subject = model.Subject;
                mm.Body = model.Body;
                if (model.Attachment.Length > 0)
                {
                    string fileName = Path.GetFileName(model.Attachment.FileName);
                    mm.Attachments.Add(new Attachment(model.Attachment.OpenReadStream(), fileName));
                }
                mm.IsBodyHtml = false;
                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential NetworkCred = new NetworkCredential("XXX@gmail.com", "p@ssw3rd");
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    smtp.Port = 587;
                    smtp.Send(mm);
                    ViewBag.Message = "Email sent.";
                }
            }

            return View();
        }
    }

在ASP.NET Core中不会自动创建对应的View文件夹,所以需要手动创建文件夹“Views/SendEmail”,并新建.cshtml文件如下,

@model CoreEmail.Models.EmailModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }

        table th, table td {
            padding: 5px;
        }
    </style>
</head>
<body>
    <div>
        @using (Html.BeginForm("Index", "SendEmail", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td style="width: 80px">
                        To:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.To)
                    </td>
                </tr>
                <tr>
                    <td>
                        Subject:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Subject)
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        Body:
                    </td>
                    <td>
                        @Html.TextAreaFor(model => model.Body, new { rows = "3", cols = "20" })
                    </td>
                </tr>
                <tr>
                    <td>
                        File Attachment:
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.Attachment, new { type = "file" })
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="Send" />
                    </td>
                </tr>
            </table>
            <br />
            <span style="color:green">@ViewBag.Message</span>
        }
    </div>
</body>
</html>

默认Route下通过https://localhost:#port/SendEmail访问。

posted @ 2020-06-11 17:29  Kyle0418  阅读(509)  评论(0编辑  收藏  举报