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>
  • 相关阅读:
    20169217 2016-2017-2 《网络攻防实践》第六周学习总结
    20169217 2016-2017-2 《网络攻防实践》第五周学习总结
    20169217 2016-2017-2 《网络攻防实践》第四周学习总结
    20169210 2016-2017-2《网络攻防实践》课程总结
    工具介绍
    20169210 2016-2017-2《网络攻防实践》第十四周免杀技术
    20169210 2016-2017-2《网络攻防实践》第十三周攻击MS08-067漏洞
    20169210 2016-2017-2《网络攻防实践》第十二周SQL注入
    20169210 2016-2017-2《网络攻防实践》第十一周总结
    20169210 2016-2017-2《网络攻防实践》第十周总结
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/13093216.html
Copyright © 2011-2022 走看看