-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMailKitProvider.cs
More file actions
147 lines (123 loc) · 3.93 KB
/
MailKitProvider.cs
File metadata and controls
147 lines (123 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using NETCore.MailKit.Infrastructure.Internal;
using System;
using System.Collections.Generic;
using System.Text;
using MailKit.Net.Smtp;
using MailKit.Security;
using MailKit.Net.Pop3;
using MailKit.Net.Imap;
namespace NETCore.MailKit
{
public class MailKitProvider:IMailKitProvider
{
public MailKitOptions Options { get; private set; }
public MailKitProvider(MailKitOptions mailKitOptions)
{
Options = mailKitOptions;
}
#region Smtp
public SmtpClient SmtpClient
{
get
{
return lazySmtpClient().Value;
}
}
private Lazy<SmtpClient> lazySmtpClient()
{
return new Lazy<SmtpClient>(() =>
{
return InitSmtpClient();
});
}
private SmtpClient InitSmtpClient()
{
try
{
var client = new SmtpClient();
client.CheckCertificateRevocation = false;
//ÉèÖÃtimeout ±ÜÃⳤʱ¼äµÄµÈ´ý
client.Timeout = 5000;
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
if (!Options.Security)
{
client.Connect(Options.Server, Options.Port, SecureSocketOptions.None);
}
else
{
// fix issue #6
client.Connect(Options.Server, Options.Port, SecureSocketOptions.Auto);
}
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// user login smtp server (fix issue #9)
if (!string.IsNullOrEmpty(Options.Account) && !string.IsNullOrEmpty(Options.Password))
{
client.Authenticate(Options.Account, Options.Password);
}
return client;
}
catch(Exception e)
{
throw e;
}
}
#endregion
#region Pop3
public Pop3Client Pop3Client
{
get
{
return lazyPop3Client().Value;
}
}
private Lazy<Pop3Client> lazyPop3Client()
{
return new Lazy<Pop3Client>(() =>
{
return InitPop3Client();
});
}
private Pop3Client InitPop3Client()
{
var client = new Pop3Client();
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect(Options.Server, Options.Port, Options.Security);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// user login pop3 server
client.Authenticate(Options.Account, Options.Password);
return client;
}
#endregion
#region Imap
public ImapClient ImapClient
{
get
{
return lazyImapClient().Value;
}
}
private Lazy<ImapClient> lazyImapClient()
{
return new Lazy<ImapClient>(() =>
{
return InitImapClient();
});
}
private ImapClient InitImapClient()
{
var client = new ImapClient();
client.Connect(Options.Server, Options.Port, Options.Security);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// user login imap server
client.Authenticate(Options.Account, Options.Password);
return client;
}
#endregion
}
}