zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    tuple 元组及字典dict
    day 49 css属性补充浮动 属性定位 抽屉作业
    day48 选择器(基本、层级 、属性) css属性
    day47 列表 表单 css初识
    day 46 http和html
    day 45索引
    day 44 练习题讲解 多表查询
    day 40 多表查询 子查询
    day39 表之间的关联关系、 补充 表操作总结 where 、group by、
    day38 数据类型 约束条件
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/13093216.html
Copyright © 2011-2022 走看看