44using System . Text ;
55using MailKit . Net . Smtp ;
66using MailKit . Security ;
7+ using MailKit . Net . Pop3 ;
8+ using MailKit . Net . Imap ;
79
810namespace 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