|
|
@@ -1,9 +1,13 @@
|
|
1
|
|
-using MailKit.Net.Smtp;
|
|
|
1
|
+using MailKit;
|
|
|
2
|
+using MailKit.Net.Imap;
|
|
|
3
|
+using MailKit.Net.Smtp;
|
|
|
4
|
+using MailKit.Search;
|
|
2
|
5
|
using MimeKit;
|
|
3
|
6
|
using System;
|
|
4
|
7
|
using System.Collections.Generic;
|
|
5
|
8
|
using System.IO;
|
|
6
|
9
|
using System.Linq;
|
|
|
10
|
+using System.Text;
|
|
7
|
11
|
using System.Threading.Tasks;
|
|
8
|
12
|
|
|
9
|
13
|
namespace Utility.Mail
|
|
|
@@ -20,7 +24,7 @@ namespace Utility.Mail
|
|
20
|
24
|
{
|
|
21
|
25
|
var smtpClient = new SmtpClient();
|
|
22
|
26
|
smtpClient.Timeout = 10 * 1000; //设置超时时间
|
|
23
|
|
- smtpClient.Connect(config.Host, config.Port, MailKit.Security.SecureSocketOptions.None);//连接到远程smtp服务器
|
|
|
27
|
+ smtpClient.Connect(config.SMPTHost, config.Port, MailKit.Security.SecureSocketOptions.None);//连接到远程smtp服务器
|
|
24
|
28
|
smtpClient.Authenticate(config.Address, config.Password);
|
|
25
|
29
|
await smtpClient.SendAsync(mailMessage);//发送邮件
|
|
26
|
30
|
smtpClient.Disconnect(true);
|
|
|
@@ -90,5 +94,107 @@ namespace Utility.Mail
|
|
90
|
94
|
return await SendMail(mailMessage, config);
|
|
91
|
95
|
|
|
92
|
96
|
}
|
|
|
97
|
+
|
|
|
98
|
+ /// <summary>
|
|
|
99
|
+ /// 接收邮件
|
|
|
100
|
+ /// </summary>
|
|
|
101
|
+ /// <param name="config"></param>
|
|
|
102
|
+ /// <param name="webRootPath"></param>
|
|
|
103
|
+ /// <returns></returns>
|
|
|
104
|
+ public async static Task<List<MailOutput>> GetMail(MailConfig config, string webRootPath)
|
|
|
105
|
+ {
|
|
|
106
|
+ List<MailOutput> list = new List<MailOutput>();
|
|
|
107
|
+ using (var client = new ImapClient())
|
|
|
108
|
+ {
|
|
|
109
|
+ client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
|
|
110
|
+
|
|
|
111
|
+ await client.ConnectAsync(config.IMAPHost, config.IMAPPort, true);
|
|
|
112
|
+
|
|
|
113
|
+ await client.AuthenticateAsync(config.IMAPAddress, config.IMAPPassword);
|
|
|
114
|
+
|
|
|
115
|
+ var inbox = client.Inbox;
|
|
|
116
|
+ await inbox.OpenAsync(FolderAccess.ReadOnly);
|
|
|
117
|
+
|
|
|
118
|
+ var unids = await inbox.SearchAsync(SearchQuery.NotSeen);
|
|
|
119
|
+
|
|
|
120
|
+
|
|
|
121
|
+
|
|
|
122
|
+ foreach (var summary in await inbox.FetchAsync(unids, MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope | MessageSummaryItems.BodyStructure))
|
|
|
123
|
+ {
|
|
|
124
|
+ var directory = Path.Combine(webRootPath, summary.UniqueId.ToString());
|
|
|
125
|
+
|
|
|
126
|
+ if (!Directory.Exists(directory))
|
|
|
127
|
+ {
|
|
|
128
|
+ Directory.CreateDirectory(directory);
|
|
|
129
|
+ }
|
|
|
130
|
+
|
|
|
131
|
+ //邮件列表
|
|
|
132
|
+ list.Add(new MailOutput
|
|
|
133
|
+ {
|
|
|
134
|
+ Id = summary.UniqueId.ToString(),
|
|
|
135
|
+ Date = summary.Date.DateTime,
|
|
|
136
|
+ Subject = summary.Envelope.Subject
|
|
|
137
|
+ });
|
|
|
138
|
+
|
|
|
139
|
+ //if (summary.TextBody != null)
|
|
|
140
|
+ //{
|
|
|
141
|
+ // // this will download *just* the text/plain part
|
|
|
142
|
+ // var text = await inbox.GetBodyPartAsync(summary.UniqueId, summary.TextBody);
|
|
|
143
|
+ // var part = (MimePart)text;
|
|
|
144
|
+ // var fileName = "TextBody.txt";
|
|
|
145
|
+ // var path = Path.Combine(directory, fileName);
|
|
|
146
|
+ // using (var stream = System.IO.File.Create(path))
|
|
|
147
|
+ // {
|
|
|
148
|
+ // await part.Content.DecodeToAsync(stream);
|
|
|
149
|
+ // }
|
|
|
150
|
+ //}
|
|
|
151
|
+
|
|
|
152
|
+ if (summary.HtmlBody != null)
|
|
|
153
|
+ {
|
|
|
154
|
+ // this will download *just* the text/html part
|
|
|
155
|
+ var html = await inbox.GetBodyPartAsync(summary.UniqueId, summary.HtmlBody);
|
|
|
156
|
+ var part = (MimePart)html;
|
|
|
157
|
+ var fileName = "HtmlBody.txt";
|
|
|
158
|
+ var path = Path.Combine(directory, fileName);
|
|
|
159
|
+ using (var stream = System.IO.File.Create(path))
|
|
|
160
|
+ {
|
|
|
161
|
+ await part.Content.DecodeToAsync(stream);
|
|
|
162
|
+ }
|
|
|
163
|
+ }
|
|
|
164
|
+
|
|
|
165
|
+
|
|
|
166
|
+ if (summary.Body is BodyPartMultipart)
|
|
|
167
|
+ {
|
|
|
168
|
+ foreach (var attachment in summary.Attachments)
|
|
|
169
|
+ {
|
|
|
170
|
+ // this will download *just* the attachment
|
|
|
171
|
+ var entity = await inbox.GetBodyPartAsync(summary.UniqueId, attachment);
|
|
|
172
|
+
|
|
|
173
|
+ if (entity is MessagePart)
|
|
|
174
|
+ {
|
|
|
175
|
+ var rfc822 = (MessagePart)entity;
|
|
|
176
|
+ var path = Path.Combine(directory, attachment.PartSpecifier + ".eml");
|
|
|
177
|
+ rfc822.Message.WriteTo(path);
|
|
|
178
|
+ }
|
|
|
179
|
+ else
|
|
|
180
|
+ {
|
|
|
181
|
+ var part = (MimePart)entity;
|
|
|
182
|
+ var fileName = part.FileName;
|
|
|
183
|
+ var path = Path.Combine(directory, fileName);
|
|
|
184
|
+ using (var stream = System.IO.File.Create(path))
|
|
|
185
|
+ {
|
|
|
186
|
+ await part.Content.DecodeToAsync(stream);
|
|
|
187
|
+ }
|
|
|
188
|
+ }
|
|
|
189
|
+ }
|
|
|
190
|
+ }
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ await client.DisconnectAsync(true);
|
|
|
194
|
+ }
|
|
|
195
|
+
|
|
|
196
|
+ return list;
|
|
|
197
|
+
|
|
|
198
|
+ }
|
|
93
|
199
|
}
|
|
94
|
|
-}
|
|
|
200
|
+}
|