MailKit---发送邮件

   一封最复杂的电子邮件的基本情况为:含有邮件正文和邮件附件,邮件正文可以同时使用HTML格式和普通文本格式表示,并且HTML格式的正文中又引用了其他的内嵌资源。对于这种最复杂的电子邮件,可以采用如图所示的MIME消息结构进行描述。

  一封MIME邮件中的MIME消息可以有三种组合关系:混合、关联、选择,它们对应MIME类型如下:

  1、multipart/mixed

  表示消息体中的内容是混和组合类型,内容可以是文本、声音和附件等不同邮件内容的混和体,例如上图中的整封邮件的MIME类型就必须定义为multipart/mixed。

  2、multipart/related

  表示消息体中的内容是关联(依赖)组合类型,例如图5中的邮件正文要使用HTML代码引用内嵌的图片资源,它们组合成的MIME消息的MIME类型就应该定义为multipart/related,表示其中某些资源(HTML代码)要引用(依赖)另外的资源(图像数据),引用资源与被引用的资源必须组合成multipart/related类型的MIME组合消息。

  3、multipart/alternative

  表示消息体中的内容是选择组合类型,例如一封邮件的邮件正文同时采用HTML格式和普通文本格式进行表达时,就可以将它们嵌套在一个multipart/alternative类型的MIME组合消息中。这种做法的好处在于如果邮件阅读程序不支持HTML格式时,可以采用其中的文本格式进行替代。

  下面就演示一下mailkit发送一封文本、html、内嵌图片,附件的邮件。mailkit提供了两种方式供我们选择:

  方式一:

 1 var message = new MimeMessage();
 2             message.To.Add(new MailboxAddress("[email protected]", "[email protected]"));//收件人邮箱
 3             message.From.Add(new MailboxAddress("xxxx@163", "xxxx@163.com"));//发件人邮件
 4             message.Subject = "邮件测试主题2";
 5 
 6             var builder = new BodyBuilder();
 7             var image = builder.LinkedResources.Add(path);
 8             //组合消息中的内嵌资源指定一个唯一标识号
 9             image.ContentId = MimeUtils.GenerateMessageId();
10 
11             var plain = new TextPart("plain")
12             {
13                 Text = @"Hey AnLong[cid:" + image.ContentId + "]"
14             };
15             var html = new TextPart("html")
16             {
17                 Text = @"<p>Hey AnLong<br>
18                         <p>Will you be my +1<br>
19                         <center><img src=""cid:" + image.ContentId + @"""></center>"
20             };
21             
22             // create an image attachment for the file located at path
23             var attachment = new MimePart("image", "png")
24             {
25                 ContentObject = new ContentObject(File.OpenRead(path), ContentEncoding.Default),
26                 //指定邮件阅读程序处理数据内容的方式,有inline和attachment两种标准方式,inline表示直接处理,而attachment表示当做附件处理。
27                 ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
28                 //指定MIME消息体中的内容所采用的邮件编码方式
29                 ContentTransferEncoding = ContentEncoding.Base64,
30                 //表示MIME消息体的内容为邮件附件,附件名
31                 FileName = Path.GetFileName(path)
32             };
33 
34             //表示消息体中的内容是选择组合类型
35             var alternative = new Multipart("alternative");
36             alternative.Add(plain);
37             alternative.Add(html);
38 
39             //表示消息体中的内容是关联(依赖)组合类型
40             var relatavenative = new Multipart("related");
41             relatavenative.Add(alternative);
42             relatavenative.Add(image);
43 
44             // now create the multipart/mixed container to hold the message text and the
45             // image attachment
46             var multipart = new Multipart("mixed");
47             multipart.Add(relatavenative);
48             multipart.Add(attachment);
49             message.Body = multipart;
50 
51             
52 
53             using (var client = new MailKit.Net.Smtp.SmtpClient())
54             {
55                 client.Connect("smtp.163.com", 465, true);
56 
57                 // Note: since we don't have an OAuth2 token, disable
58                 // the XOAUTH2 authentication mechanism.
59                 client.AuthenticationMechanisms.Remove("XOAUTH2");
60 
61                 // Note: only needed if the SMTP server requires authentication
62                 client.Authenticate("[email protected]", "123456");
63 
64                 client.Send(message);
65                 client.Disconnect(true);
66             }

方法二:

 1 var message = new MimeMessage();
 2             message.To.Add(new MailboxAddress("[email protected]", "[email protected]"));//收件人
 3             message.From.Add(new MailboxAddress("xxxx", "xxxx@163.com"));//发件人
 4             message.Subject = "邮件测试主题2";
 5 
 6             var builder = new BodyBuilder();
 7             var image = builder.LinkedResources.Add(path);
 8             image.ContentId = MimeUtils.GenerateMessageId();
 9 
10             builder.TextBody = string.Format(@"[cid:{0}] Hey AnLong", image.ContentId);
11             builder.HtmlBody = string.Format(@"<p>Hey anlong</p><p>Will you be my +1</p><center><img src=""cid:{0}""></center>", image.ContentId);
12 
13             // We may also want to attach a calendar event for Monica's party...
14             builder.Attachments.Add(@"C:\Users\Joey\Documents\party.ics");
15 
16             message.Body = builder.ToMessageBody();
17 
18             
19 
20             using (var client = new MailKit.Net.Smtp.SmtpClient())
21             {
22                 client.Connect("smtp.163.com", 465, true);
23 
24                 // Note: since we don't have an OAuth2 token, disable
25                 // the XOAUTH2 authentication mechanism.
26                 client.AuthenticationMechanisms.Remove("XOAUTH2");
27 
28                 // Note: only needed if the SMTP server requires authentication
29                 client.Authenticate("[email protected]", "123456");
30 
31                 client.Send(message);
32                 client.Disconnect(true);
33             }

通过上述方式组合成一封完整的邮件,然后发送即可。

猜你喜欢

转载自www.cnblogs.com/zuimengaitianya/p/9227541.html
今日推荐