栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C# MailKit邮件发送,HTML模板

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# MailKit邮件发送,HTML模板

文章目录
  • 前言
  • 一、后端代码
  • 二、HTML模板


前言

工作笔记:VS 2019 MailKit 发送邮件,
NuGet下载:MailKit,Mimekit
引用:
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;


一、后端代码
		public string sendMail(SendMailConfig config, SendMailEntity entity)
		{
		     string receiver = entity.Receiver;
		     if (null == receiver || receiver.Trim().Equals(""))
		     {
		         throw new Exception("收件人不能为空");
		     }
		     string[] cc = (null == entity.CopyFor || entity.CopyFor.Equals("")) ? null : entity.CopyFor.Split(';');
		     string[] attach = (null == entity.Attach || entity.Attach.Equals("")) ? null : entity.Attach.Split(',');
		
		     sendMail(config.username, config.password, config.from, receiver.Split(';'), cc, null, entity.Title, entity.Content, attach, config);
		
		     return "ok";
		}

		/// 
        /// 邮件发送
        /// 
        /// 用户名
        /// 密码
        /// 发件邮箱
        /// 收件邮箱
        /// 抄送
        /// 密送
        /// 标题
        /// 内容
        /// 附件
        /// 配置
		public void sendMail(string username, string password, string from, string[] to, string[] cc, string[] bcc, string subject, string content, string[] attachfilenames, SendMailConfig config)
        {
            using (var client = new SmtpClient())
            {
                var message = new MimeMessage();

                #region from to cc bcc
                if (string.IsNullOrWhiteSpace(from))
                {
                    message.From.Add(new MailboxAddress(from, from));
                }
                else
                {
                    message.From.Add(new MailboxAddress(config.fromdisplayname, config.from));
                }
                for (int i = 0; i < to.Length; i++)
                {
                    if (!string.ReferenceEquals(to[i], null) && !to[i].Trim().Equals(""))
                    {
                        message.To.Add(new MailboxAddress(to[i], to[i]));
                    }
                }
                if (cc != null && !cc.Equals(""))
                {
                    for (int i = 0; i < cc.Length; i++)
                    {
                        if (!string.ReferenceEquals(cc[i], null) && !cc[i].Trim().Equals(""))
                        {
                            message.Cc.Add(new MailboxAddress(cc[i], cc[i]));
                        }
                    }
                }
                if (bcc != null)
                {
                    for (int i = 0; i < bcc.Length; i++)
                    {
                        if (!string.ReferenceEquals(bcc[i], null) && !bcc[i].Trim().Equals(""))
                        {
                            message.Bcc.Add(new MailboxAddress(bcc[i], bcc[i]));
                        }
                    }
                }
                #endregion
                
                message.Subject = subject;
                BodyBuilder _body = new BodyBuilder
                {
                    HtmlBody = content,
                };
                if (attachfilenames != null && attachfilenames.Length > 0)
                {
                    for (int i = 0; i < attachfilenames.Length; i++)
                    {
                        if (!string.ReferenceEquals(attachfilenames[i], null))
                        {
                            string[] n = attachfilenames[i].Split('@');
                            if (n.Length >= 2)
                            {
                                _body.Attachments.Add(n[0], obs.downFile(n[1]));
                            }
                        }
                    }
                }

                message.Body = _body.ToMessageBody();
                // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                if (config.smtp.starttlsEnable && !config.smtp.sslEnable) //config.getSmtp.sslEnable 端口不再是默认端口 25 而是465
                {
                    client.Connect(config.smtp.host, config.smtp.port, SecureSocketOptions.StartTls);
                }
                else
                {
                    client.Connect(config.smtp.host, config.smtp.port, config.smtp.sslEnable);
                }
                
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate(username, password);
                client.Send(message);
                client.Disconnect(true);
            }
        }
        
        /// 
        /// 获取HTML模板 我的模板是配置在目录下的,通过路径拿到文件内的内容
        /// 
        /// 
        /// 
		public string BuildByFile(string filePath)
        {
            StreamReader reader = null;
            string template = string.Empty;
            reader = new StreamReader(filePath);
            template = reader.ReadToEnd();
            reader.Close();
            return template;
        }
二、HTML模板
	
{-- 最上边紫色的分割线 --}} --> {-- 页头的LOGO --}} -->
 

HI:

请复制下方链接至浏览器打开:

https://www.baidu.com/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023684.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号