Skip to content

Commit 18b40b4

Browse files
committed
add pop3Client and imapClient
1 parent ab8dc6a commit 18b40b4

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

‎src/NETCore.MailKit/Core/EmailService.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private void SendEmail(string mailTo, string mailCc, string mailBcc, string subj
209209
//set email body
210210
mimeMessage.Body = body;
211211

212-
using (var client = _MailKitProvider.Client)
212+
using (var client = _MailKitProvider.SmtpClient)
213213
{
214214
client.Send(mimeMessage);
215215
}

‎src/NETCore.MailKit/IMailKitProvider.cs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using MailKit.Net.Imap;
2+
using MailKit.Net.Pop3;
13
using MailKit.Net.Smtp;
24
using NETCore.MailKit.Infrastructure.Internal;
35
using System;
@@ -14,6 +16,16 @@ public interface IMailKitProvider
1416
/// <summary>
1517
/// smtp client
1618
/// </summary>
17-
SmtpClient Client { get; }
19+
SmtpClient SmtpClient { get; }
20+
21+
/// <summary>
22+
/// pop3 client
23+
/// </summary>
24+
Pop3Client Pop3Client { get; }
25+
26+
/// <summary>
27+
/// imap client
28+
/// </summary>
29+
ImapClient ImapClient { get; }
1830
}
1931
}

‎src/NETCore.MailKit/MailKitProvider.cs‎

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Text;
55
using MailKit.Net.Smtp;
66
using MailKit.Security;
7+
using MailKit.Net.Pop3;
8+
using MailKit.Net.Imap;
79

810
namespace NETCore.MailKit
911
{
@@ -16,14 +18,15 @@ public MailKitProvider(MailKitOptions mailKitOptions)
1618
Options = mailKitOptions;
1719
}
1820

19-
public SmtpClient Client
21+
#region Smtp
22+
23+
public SmtpClient SmtpClient
2024
{
2125
get
2226
{
2327
return lazySmtpClient().Value;
2428
}
2529
}
26-
2730
private Lazy<SmtpClient> lazySmtpClient()
2831
{
2932
return new Lazy<SmtpClient>(() =>
@@ -35,6 +38,8 @@ private Lazy<SmtpClient> lazySmtpClient()
3538
private SmtpClient InitSmtpClient()
3639
{
3740
var client = new SmtpClient();
41+
42+
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
3843
if (!Options.SSL)
3944
{
4045
client.Connect(Options.Server, Options.Port, SecureSocketOptions.None);
@@ -52,5 +57,74 @@ private SmtpClient InitSmtpClient()
5257

5358
return client;
5459
}
60+
61+
#endregion
62+
63+
#region Pop3
64+
public Pop3Client Pop3Client
65+
{
66+
get
67+
{
68+
return lazyPop3Client().Value;
69+
}
70+
}
71+
private Lazy<Pop3Client> lazyPop3Client()
72+
{
73+
return new Lazy<Pop3Client>(() =>
74+
{
75+
return InitPop3Client();
76+
});
77+
}
78+
79+
private Pop3Client InitPop3Client()
80+
{
81+
var client = new Pop3Client();
82+
83+
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
84+
client.Connect(Options.Server, Options.Port, Options.SSL);
85+
86+
// Note: since we don't have an OAuth2 token, disable
87+
// the XOAUTH2 authentication mechanism.
88+
client.AuthenticationMechanisms.Remove("XOAUTH2");
89+
90+
// user login pop3 server
91+
client.Authenticate(Options.Account, Options.Password);
92+
return client;
93+
}
94+
95+
#endregion
96+
97+
#region Imap
98+
public ImapClient ImapClient
99+
{
100+
get
101+
{
102+
return lazyImapClient().Value;
103+
}
104+
}
105+
private Lazy<ImapClient> lazyImapClient()
106+
{
107+
return new Lazy<ImapClient>(() =>
108+
{
109+
return InitImapClient();
110+
});
111+
}
112+
113+
private ImapClient InitImapClient()
114+
{
115+
var client = new ImapClient();
116+
117+
client.Connect(Options.Server, Options.Port, Options.SSL);
118+
119+
// Note: since we don't have an OAuth2 token, disable
120+
// the XOAUTH2 authentication mechanism.
121+
client.AuthenticationMechanisms.Remove("XOAUTH2");
122+
// user login imap server
123+
client.Authenticate(Options.Account, Options.Password);
124+
125+
return client;
126+
}
127+
#endregion
128+
55129
}
56130
}

0 commit comments

Comments
 (0)