Fork me on GitHub

知途灬林

导航

QQ邮箱发送邮件

 
public class SendMail
    {
        private MailMessage mailMessage;
        private SmtpClient smtpClient;
        private string password;//发件人密码  
        /// <summary>  
        /// 带参数的构造函数,初始化发送邮件的条件
        /// </summary>  
        /// <param name="To">收件人地址</param>  
        /// <param name="From">发件人地址</param>  
        /// <param name="Body">邮件正文</param>  
        /// <param name="Title">邮件的主题</param>  
        /// <param name="Password">发件人密码</param>  
        public SendMail(string[] To, string From, string Body, string Title, string Password)
        {
            mailMessage = new MailMessage();
            foreach (var item in To)
            {
                mailMessage.To.Add(item);
            }
            mailMessage.From = new System.Net.Mail.MailAddress(From);
            mailMessage.Subject = Title;
            mailMessage.Body = Body;
            mailMessage.IsBodyHtml = true;
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
            mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
            this.password = Password;
        }
        /// <summary>  
        /// 添加附件  
        /// </summary>  
        public void Attachments(string Path)
        {
            string[] path = Path.Split(',');
            Attachment data;
            ContentDisposition disposition;
            for (int i = 0; i < path.Length; i++)
            {
                data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件  
                disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期  
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期  
                disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期  
                mailMessage.Attachments.Add(data);//添加到附件中  
            }
        }
        /// <summary>  
        /// 异步发送邮件  
        /// </summary>  
        /// <param name="CompletedMethod"></param>  
        public void SendAsync(SendCompletedEventHandler CompletedMethod)
        {
            if (mailMessage != null)
            {
                smtpClient = new SmtpClient();
                smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据  
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpClient.Host = "smtp." + mailMessage.From.Host;
                smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件  
                smtpClient.SendAsync(mailMessage, mailMessage.Body);
            }
        }
        /// <summary>  
        /// 发送邮件  
        /// </summary>  
        public void Send()
        {
            if (mailMessage != null)
            {
                using (smtpClient = new SmtpClient())
                {
                    smtpClient.Timeout = 9999999;
                    smtpClient.EnableSsl = true;
                    smtpClient.UseDefaultCredentials = false;
                    smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据  
                    smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    smtpClient.Host = "smtp." + mailMessage.From.Host;
                    smtpClient.Send(mailMessage);
                    mailMessage.Attachments.Dispose();
                }
            }
        }
    }

    <script src="Scripts/jquery-3.4.1.min.js"></script>
    <script src="Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script>
        function SendMails() {

            var obj = {
                RecMails:$("#txt_RevMail").val(),
                Title:$("#txt_Title").val(),
                BodyContent:$("#ta_Content").val(),
            };
            $.ajax({
                url: "http://localhost:57820/api/Mail/SendMailMethod",
                dataType: "json",
                type: "post",
                data: obj,
                success: function (data) {
                    if (data > 0) {
                        alert("发送成功!");
                    }
                }
            })
        }
    </script>
</head>
<body>
    <div align="center">
        <table class="table table-bordered" style="width:50%;">
            <tr>
                <th>标题:</th>
                <td><input id="txt_Title" type="text" /></td>
            </tr>
            <tr>
                <th>收件人:</th>
                <td><input id="txt_RevMail" type="text" /></td>
            </tr>
            <tr>
                <th>正文:</th>
                <td><textarea id="ta_Content" rows="4" cols="40"></textarea></td>
            </tr>
            <tr>
                <td colspan="2"><input id="btn_Send" type="button" class="btn btn-primary" value="发送邮件" onclick="SendMails()" /></td>
            </tr>
        </table>
    </div>
</body>
</html>

 

 
1
public class MailController : ApiController 2 { 3 [HttpPost] 4 public int SendMailMethod(EmailModel model) 5 { 6 //List<string> list = new List<string>() { 7 // "691805617@qq.com", 8 // "31900477@qq.com" 9 //}; 10 try 11 { 12 string[] arr = null; 13 if(model.RecMails.Contains(",")) 14 { 15 arr = model.RecMails.Split(','); 16 }else 17 { 18 arr = new string[] { model.RecMails }; 19 } 20 21 SendMail mail = new SendMail(arr, "发件人的QQ邮箱", model.BodyContent, model.Title, "授权码"); 22 mail.Send(); 23 return 1; 24 } 25 catch (Exception ex) 26 { 27 return 0; 28 } 29 } 30 }

 

posted on 2020-11-18 20:26  小豆子你还好吗  阅读(108)  评论(0编辑  收藏  举报