首页 > 其他分享 >NET6/Framework 封装邮件发送纯文本/HTML/HTML+图片/附件

NET6/Framework 封装邮件发送纯文本/HTML/HTML+图片/附件

时间:2023-09-21 15:34:28浏览次数:36  
标签:dto Framework bodyBuilder HTML NET6 var new public 邮件

Nuget

Runtime: Net6
MailKit  4.2.0
MimeKit  4.2.0

发送纯文本

{
    var bodyBuilder = new BodyBuilder();
    bodyBuilder.TextBody = "这是一封纯文本邮件";
    message.Body = bodyBuilder.ToMessageBody();
}
{
    var textPart = new TextPart("plain")
    {
        Text = "这是一封纯文本邮件"
    };
    message.Body = textPart;
}

发送HTML

{
    var bodyBuilder = new BodyBuilder();
    bodyBuilder.HtmlBody = "<html><body><h1>这是一封包含HTML的邮件</h1></body></html>";
    message.Body = bodyBuilder.ToMessageBody();
}
{
    var multipart = new Multipart("mixed");
    var htmlPart = new TextPart("html")
    {
        Text = "<html><body><h1>这是一封包含HTML的邮件</h1></body></html>"
    };
    multipart.Add(htmlPart);
    message.Body = multipart;
}

HTML+图片

var contentId = MimeUtils.GenerateMessageId();
var str = "<html><body><h1>这是一封HTML包含图片的邮件</h1><img src=\"cid:"+ contentId +"\"></body></html>";
{
    var bodyBuilder = new BodyBuilder();
    var image = bodyBuilder.LinkedResources.Add("图片路径");
    image.ContentId = contentId;
    bodyBuilder.HtmlBody = str;
    message.Body = bodyBuilder.ToMessageBody();
}
{
    var multipart = new Multipart("related");
    var htmlPart = new TextPart("html")
    {
        Text = str
    };
    multipart.Add(htmlPart);
    var image = new MimePart("image", "jpeg")
    {
        Content = new MimeContent(File.OpenRead("图片路径")),
        ContentDisposition = new ContentDisposition(ContentDisposition.Inline),
        ContentTransferEncoding = ContentEncoding.Base64,
        ContentId = contentId
    };
    multipart.Add(image);
    message.Body = multipart;
}

附件

{
    var attachment = new MimePart("application", "octet-stream")
    {
        Content = new MimeContent(File.OpenRead("附件路径")),
        ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
        ContentTransferEncoding = ContentEncoding.Base64,
        FileName = Path.GetFileName("附件路径")
    };
    bodyBuilder.Attachments.Add(attachment);
}
{
    var multipart = new Multipart("mixed");
    var attachment = new MimePart("application", "附件后缀")
    {
        Content = new MimeContent(File.OpenRead("附件路径")),
        ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
        ContentTransferEncoding = ContentEncoding.Base64,
        FileName = Path.GetFileName("附件路径")
    };
    multipart.Add(attachment);
    message.Body = multipart;
}

Demo

Models

  1     /// <summary>
  2     /// 自定义配置文件对象
  3     /// </summary>
  4     public class AppSettingDto
  5     {
  6         /// <summary>
  7         /// 邮件配置信息
  8         /// </summary>
  9         public EmailInfo EmailInfo { get; set; }
 10     }
 11 
 12     /// <summary>
 13     /// 邮件配置信息
 14     /// </summary>
 15     public class EmailInfo
 16     {
 17         /// <summary>
 18         /// 邮件:主机
 19         /// </summary>
 20         public string EmailHost { get; set; }
 21 
 22         /// <summary>
 23         /// 邮件:端口
 24         /// </summary>
 25         public int EmailPort { get; set; }
 26 
 27         /// <summary>
 28         /// 发送人账号
 29         /// </summary>
 30         public string EmailFromAccount { get; set; }
 31 
 32         /// <summary>
 33         /// 发送人密码
 34         /// </summary>
 35         public string EmailFromPassword { get; set; }
 36 
 37         /// <summary>
 38         /// 发送人邮件地址
 39         /// </summary>
 40         public string EmailFromAddress { get; set; }
 41     }
 42 
 43     /// <summary>
 44     /// 邮件用户
 45     /// </summary>
 46     public class EmailTargetDto
 47     {
 48         /// <summary>
 49         /// 邮件账号
 50         /// </summary>
 51         public string EmailAccount { get; set; }
 52 
 53         /// <summary>
 54         /// 邮件地址
 55         /// </summary>
 56         public string EmailAddress { get; set; }
 57     }
 58 
 59     /// <summary>
 60     /// 邮件内容类型枚举:纯文本 / 页面:文本 / 页面:项目内相对路径
 61     /// </summary>
 62     public enum EmailTypeEnum
 63     {
 64         /// <summary>
 65         /// 纯文本
 66         /// </summary>
 67         Text = 1,
 68 
 69         /// <summary>
 70         /// 页面:文本
 71         /// </summary>
 72         HtmlText = 10,
 73 
 74         /// <summary>
 75         /// 页面:项目内相对路径
 76         /// </summary>
 77         HtmlPath = 13,
 78     }
 79 
 80     /// <summary>
 81     /// 邮件参数dto
 82     /// </summary>
 83     public class SendEmailDto
 84     {
 85         /// <summary>
 86         /// 主题
 87         /// </summary>
 88         public string Subject { get; set; }
 89 
 90         /// <summary>
 91         /// 邮件集合:接收人
 92         /// </summary>
 93         public List<EmailTargetDto> ToList { get; set; } = new List<EmailTargetDto>();
 94 
 95         /// <summary>
 96         /// 邮件集合:抄送人
 97         /// </summary>
 98         public List<EmailTargetDto> CcList { get; set; } = new List<EmailTargetDto>();
 99 
100         /// <summary>
101         /// 邮件内容类型枚举:纯文本 / 页面:文本 / 页面:项目内相对路径
102         /// </summary>
103         public EmailTypeEnum Type { get; set; }
104 
105         /// <summary>
106         /// 邮件内容
107         /// </summary>
108         public string Content { get; set; }
109 
110         /// <summary>
111         /// 页面:项目内相对路径(ContentType == EmailTypeEnum.HtmlPath)
112         /// </summary>
113         public string HtmlPath { get; set; }
114 
115         /// <summary>
116         /// html内图片路径集合:项目内的相对路径(路径必须和占位符保持一致,且,占位符数量要小于等于路径数量)
117         /// </summary>
118         public List<string> HtmlImgPathList { get; set; }
119 
120         /// <summary>
121         /// 附件地址集合:项目内的相对路径
122         /// </summary>
123         public List<string> AttachmentPathList { get; set; }
124     }
View Code

Helper

  1     /// <summary>
  2     /// 邮件帮助类:仅支持纯文本,html,附件
  3     /// </summary>
  4     public class EmailHelper
  5     {
  6         /// <summary>
  7         /// 邮件配置信息
  8         /// </summary>
  9         private EmailInfo _email;
 10 
 11         public EmailHelper(IOptionsSnapshot<AppSettingDto> options)
 12         {
 13             _email = options.Value.EmailInfo;
 14         }
 15 
 16         /// <summary>
 17         /// 发送文本
 18         /// </summary>
 19         /// <param name="content"></param>
 20         public void SendText(SendEmailDto dto)
 21         {
 22             dto.Type = EmailTypeEnum.Text;
 23             SendRun(dto);
 24         }
 25 
 26         /// <summary>
 27         /// 发送Html文本
 28         /// </summary>
 29         /// <param name="content"></param>
 30         public void SendHtmlText(SendEmailDto dto)
 31         {
 32             dto.Type = EmailTypeEnum.HtmlText;
 33             SendRun(dto);
 34         }
 35 
 36         /// <summary>
 37         /// 发送Html相对路径
 38         /// </summary>
 39         /// <param name="content"></param>
 40         public void SendHtmlPath(SendEmailDto dto)
 41         {
 42             dto.Type = EmailTypeEnum.HtmlPath;
 43             SendRun(dto);
 44         }
 45 
 46         /// <summary>
 47         /// 发送邮件
 48         /// </summary>
 49         /// <param name="dto"></param>
 50         private void SendRun(SendEmailDto dto)
 51         {
 52             //  构建邮件:MimeMessage
 53             var message = BuilderEmailMessage(dto);
 54             //  构建邮件:Body
 55             var bodyBuilder = BuilderEmailBody(dto, message);
 56             //  绑定附件
 57             BindingAttachment(dto, bodyBuilder);
 58             message.Body = bodyBuilder.ToMessageBody();
 59             //  发送邮件
 60             SendMessage(message);
 61         }
 62 
 63         /// <summary>
 64         /// 绑定附件
 65         /// </summary>
 66         /// <param name="dto"></param>
 67         /// <param name="bodyBuilder"></param>
 68         private static void BindingAttachment(SendEmailDto dto, BodyBuilder bodyBuilder)
 69         {
 70             var attachmentList = dto?.AttachmentPathList ?? new List<string>();
 71             var baseDirctory = AppDomain.CurrentDomain.BaseDirectory;
 72             foreach (var itemPath in attachmentList)
 73             {
 74                 var attachment = new MimePart("application", "octet-stream")
 75                 {
 76                     Content = new MimeContent(File.OpenRead($@"{baseDirctory}\{itemPath}")),
 77                     ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
 78                     ContentTransferEncoding = ContentEncoding.Base64,
 79                     FileName = Path.GetFileName(itemPath)
 80                 };
 81                 bodyBuilder.Attachments.Add(attachment);
 82             }
 83         }
 84 
 85         /// <summary>
 86         /// 构建邮件:MimeMessage
 87         /// </summary>
 88         /// <param name="dto"></param>
 89         /// <returns></returns>
 90         private MimeMessage BuilderEmailMessage(SendEmailDto dto)
 91         {
 92             var message = new MimeMessage
 93             {
 94                 Subject = dto.Subject,
 95             };
 96             message.From.Add(new MailboxAddress(_email.EmailFromAccount, _email.EmailFromAddress));
 97             if (dto.ToList != null && dto.ToList.Count > 0) dto.ToList.ForEach(x => message.To.Add(new MailboxAddress(x.EmailAccount, x.EmailAddress)));
 98             if (dto.CcList != null && dto.CcList.Count > 0) dto.CcList.ForEach(x => message.Cc.Add(new MailboxAddress(x.EmailAccount, x.EmailAddress)));
 99             return message;
100         }
101 
102         /// <summary>
103         /// 构建邮件:Body
104         /// </summary>
105         /// <param name="dto"></param>
106         /// <param name="message"></param>
107         private BodyBuilder BuilderEmailBody(SendEmailDto dto, MimeMessage message)
108         {
109             var bodyBuilder = new BodyBuilder();
110             switch (dto.Type)
111             {
112                 case EmailTypeEnum.HtmlText:
113                 case EmailTypeEnum.HtmlPath:
114                     // 把 html相对路径 处理成 html文本
115                     var htmlValue = HandleHtmlText(dto);
116                     // 把 html文本 中的img 转换为 base64
117                     htmlValue = HandleHtmlImg(bodyBuilder, htmlValue, dto.HtmlImgPathList);
118                     bodyBuilder.HtmlBody = htmlValue;
119                     break;
120                 case EmailTypeEnum.Text:
121                 default:
122                     bodyBuilder.TextBody = dto?.Content?.Trim(); ;
123                     break;
124             }
125             return bodyBuilder;
126         }
127 
128         /// <summary>
129         /// 把 html文本 中的img 转换为 base64
130         /// </summary>
131         /// <param name="dto"></param>
132         /// <returns></returns>
133         private string HandleHtmlImg(BodyBuilder bodyBuilder, string htmlValue, List<string> htmlImgPathList)
134         {
135             if (htmlImgPathList == null || htmlImgPathList.Count == 0) return htmlValue;
136             var baseDirctory = AppDomain.CurrentDomain.BaseDirectory;
137             //  循环处理img to base64
138             var imgPathList = new List<string>();
139             foreach (var itemPath in htmlImgPathList)
140             {
141                 var image = bodyBuilder.LinkedResources.Add($@"{baseDirctory}\{itemPath}");
142                 image.ContentId = MimeUtils.GenerateMessageId();
143                 string imgPath = $"cid:{image.ContentId}";
144                 imgPathList.Add(imgPath);
145             }
146 
147             //  路径必须和占位符保持一致,且,占位符数量要小于等于路径数量
148             return string.Format(htmlValue, imgPathList.ToArray());
149         }
150 
151         /// <summary>
152         /// 把 html相对路径 处理成 html文本
153         /// </summary>
154         /// <param name="dto"></param>
155         /// <returns></returns>
156         private string HandleHtmlText(SendEmailDto dto)
157         {
158             //  页面:项目内相对路径
159             if (dto.Type == EmailTypeEnum.HtmlPath)
160             {
161                 var baseDirctory = AppDomain.CurrentDomain.BaseDirectory;
162                 var htmlPath = $@"{baseDirctory}\{dto.HtmlPath}";
163                 using (StreamReader SourceReader = File.OpenText(htmlPath))
164                 {
165                     var htmlValue = SourceReader.ReadToEnd();
166                     return htmlValue;
167                 }
168             }
169             return dto.Content;
170         }
171 
172         /// <summary>
173         /// 发送邮件
174         /// </summary>
175         /// <param name="message"></param>
176         private void SendMessage(MimeMessage message)
177         {
178             using (var client = new SmtpClient())
179             {
180                 //  地址端口
181                 client.Connect(_email.EmailHost, _email.EmailPort);
182                 //  账号密码
183                 client.Authenticate(_email.EmailFromAccount, _email.EmailFromPassword);
184 
185                 // 发送邮件
186                 client.Send(message);
187                 client.Disconnect(true);
188             }
189             message.Dispose();
190         }
191     }
View Code

Run

  1     /// <summary>
  2     /// 单元测试
  3     /// </summary>
  4     public class UnitTest
  5     {
  6         private EmailInfo _email;
  7         private EmailHelper _emailUnit;
  8 
  9         /// <summary>
 10         /// 
 11         /// </summary>
 12         /// <param name="emailUnit"></param>
 13         /// <param name="email"></param>
 14         public UnitTest(EmailHelper emailUnit, IOptionsSnapshot<AppSettingDto> options)
 15         {
 16             _emailUnit = emailUnit;
 17             _email = options.Value.EmailInfo;
 18         }
 19 
 20         /// <summary>
 21         /// 发送文本
 22         /// </summary>
 23         [Fact]
 24         public void SendTextDto()
 25         {
 26             var dto = new SendEmailDto
 27             {
 28                 Subject = $"测试Net6邮件[Text] - {DateTime.Now:yyyy-MM-dd HH:mm:ss}",
 29                 ToList = new List<EmailTargetDto>
 30                 {
 31                     new EmailTargetDto
 32                     {
 33                         EmailAccount = _email.EmailFromAccount,
 34                         EmailAddress = _email.EmailFromAddress,
 35                     }
 36                 },
 37                 Content = "这是一封测试邮件"
 38             };
 39             _emailUnit.SendText(dto);
 40         }
 41 
 42         /// <summary>
 43         /// 发送Html文本
 44         /// </summary>
 45         [Fact]
 46         public void SendHtmlTextDto()
 47         {
 48             var builder = new StringBuilder();
 49             builder.AppendLine("<html>");
 50             builder.AppendLine("    <head></head>");
 51             builder.AppendLine("    <body style='text-align: center; width: 630px !important; height:100px !important; border: 1px solid red;'>");
 52             builder.AppendLine("        <div style='width: 630px; height: 22px;'>");
 53             builder.AppendLine("            <img width='630' height='22' style='width: 630px; height: 22px;' src='{0}' />");
 54             builder.AppendLine("        </div>");
 55             builder.AppendLine("        <div style='color: red; width: 630px; height: 22px;border: 1px solid black;'>这是一封测试邮件</div>");
 56             builder.AppendLine("        <div style='width: 630px; height: 22px;'>");
 57             builder.AppendLine("            <img src='{1}' />");// html显示图片
 58             builder.AppendLine("        </div>");
 59             builder.AppendLine("    </body>");
 60             builder.AppendLine("</html>");
 61             var dto = new SendEmailDto
 62             {
 63                 Subject = $"测试Net6邮件[HtmlText] - {DateTime.Now:yyyy-MM-dd HH:mm:ss}",
 64                 ToList = new List<EmailTargetDto>
 65                 {
 66                     new EmailTargetDto
 67                     {
 68                         EmailAccount = _email.EmailFromAccount,
 69                         EmailAddress = _email.EmailFromAddress,
 70                     }
 71                 },
 72                 Content = builder.ToString(),
 73                 HtmlImgPathList = new List<string>
 74                 {
 75                     @"wwwroot\Email\EmailTop.png",
 76                     @"wwwroot\Email\Koala.jpg",
 77                 }
 78 
 79             };
 80             _emailUnit.SendHtmlText(dto);
 81         }
 82 
 83         /// <summary>
 84         /// 发送Html相对路径
 85         /// </summary>
 86         [Fact]
 87         public void SendHtmlPathDto()
 88         {
 89             var dto = new SendEmailDto
 90             {
 91                 Subject = $"测试Net6邮件[HtmlPath] - {DateTime.Now:yyyy-MM-dd HH:mm:ss}",
 92                 ToList = new List<EmailTargetDto>
 93                 {
 94                     new EmailTargetDto
 95                     {
 96                         EmailAccount = _email.EmailFromAccount,
 97                         EmailAddress = _email.EmailFromAddress,
 98                     }
 99                 },
100                 HtmlPath = @"wwwroot\Email\Email.html",
101                 HtmlImgPathList = new List<string>
102                 {
103                     @"wwwroot\Email\EmailTop.png",
104                     @"wwwroot\Email\Koala.jpg",
105                 },
106                 AttachmentPathList = new List<string>
107                 {
108                     @"wwwroot\Email\EmailTop.png",
109                     @"wwwroot\Email\Koala.jpg",
110                 },
111             };
112             _emailUnit.SendHtmlPath(dto);
113         }
114     }
View Code

 

 

 

标签:dto,Framework,bodyBuilder,HTML,NET6,var,new,public,邮件
From: https://www.cnblogs.com/CRobot/p/17720064.html

相关文章

  • firefox浏览shtml时直接显示源代码问题
    firefox浏览shtml时直接显示源代码问题今天浏览网页时,打开一个网页http://photo.17ok.com/,发现直接显示了源代码,随之把页面补全,.php.html.htm都试过但都提示servernotfound,最后输入shtml,又一次出来了源代码。那么确定此页面属于.shtml页面。用IE浏览器打开,正常,无奈了,初步......
  • HTML
    HTML笔记1、前端:主要职责,负责网页。2、通过网页展示数据。3、网页的组成:(1)网页的结构内容-HTML(2)网页的样式-CSS(3)网页的行为-JavaScript4、HTML(1)简介:超文本标记语言(英语:HyperTextMarkupLanguage,简称:HTML)是一种用于创建网页的标准标记语言。开发工具:VSCode、HBuilderX、HBuilderHTML......
  • html小结002
    html基本标签brhr文字标签strong/b加粗ins/u下划线del/s删除线em/i斜体img图片标签src=""图片路径相对/绝对a超链接1.跳转2.#回到顶部3.锚点4.下载文件(不用)只能压缩文件格式列表标签......
  • html与css小知识
    html入门学习参考资料:https://www.cnblogs.com/gh110/p/15153664.html参考视频:https://www.bilibili.com/video/BV1x4411V75C扩展:qq邮箱扩展(点击联系我)可以打开qq推广弄iframe可以作为a标签的目标,然后一键点击跳转!点文字就可以锁定文字对应的框表单......
  • springframework中关于bean的装配方式、初始化、实例化流程
    ●bean的配置方式?○xml文件配置○bean的配置范围(scope范围)○bean的延迟加载(applicationContext是立即加载,如果需要延迟加载设置lazy-init为true,在getBean的时候才进行加载,加载实例化完了之后回存储到单例池中去,后续使用该bean直接从单例池中获取即可,本质上该bean还是单例的)......
  • 使用Java去除html标签的几种方法
    用Java解析html,删除所有的HTML标签,只保留纯文字内容,有以下几种方法:1.使用正则表达式html内容从程序角度来讲,就是一个字符串,我们可以对其按照纯文本处理的方式来处理。我们在做文本处理的时候,第一个想到的就是正则表达式。从一个字符串中删除HTML,对于正则来说,还是比较简单的......
  • Spring Framework RCE CVE-2022-22965 漏洞分析
    摘要本文会从几个角度分析漏洞CVE-2022-22965,首先会从payload的构造。每次我都喜欢先分析漏洞的payload,不得不承认实力没达到可以直接分析漏洞地步。所以会先看看payload的构造过程看看,每次学习和分析漏洞的payload能学到很多有趣的角度和想法。从payload的构造分析,分析payload的......
  • destoon上做纯js实现html指定页面导出word
    因为最近做了范文网站需要,所以要下载为word文档,如果php进行处理,很吃后台服务器,所以想用前端进行实现。查询github发现,确实有这方面的插件。js导出word文档所需要的两个插件:FileSaver.jsjquery.wordexport.js首先引入:<!--生成word!--><scriptsrc="https://cdn.bootcss......
  • 解决Visual Studio 2022中无法编译 .NET Framework 4.5/4.5.1项目(Visual Studio 2022
    最新【一键处理】方法:https://github.com/MrXhh/VSTools/releases1)下载VS2022Net4NotCompileFix2)右键管理员执行3)重启VS https://github.com/MrXhh/VSTools/releases......
  • HTML笔记
    一、HTML基础1、网页文件、以.html或者.htm为后缀名的文件。2、网站网页的集合。3、HTML:超文本标记语言。纯文本(字符、数字、字母等)超文本:超越文本的限制、显示视频、音频、图片、动画等。超链接跳转功能。标记:打标记符号(字母、数字等组合),浏览器解析标记。4、工具的使用(1)插入HTML......