Skip to content

Commit e1a8d66

Browse files
committed
achieve mailkit provier
1 parent 8a7d954 commit e1a8d66

6 files changed

Lines changed: 292 additions & 2 deletions

File tree

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,128 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Threading.Tasks;
45

56
namespace NETCore.MailKit.Core
67
{
78
public class EmailService : IEmailService
89
{
9-
10+
private readonly IMailKitProvider _MailKitProvider;
1011

12+
public EmailService(IMailKitProvider provider)
13+
{
14+
_MailKitProvider = provider;
15+
}
16+
17+
/// <summary>
18+
/// send email with UTF-8
19+
/// </summary>
20+
/// <param name="mailTo">consignee email,multi split with ","</param>
21+
/// <param name="subject">subject</param>
22+
/// <param name="message">email message</param>
23+
/// <param name="isHtml">is set message as html</param>
24+
public void Send(string mailTo, string subject, string message, bool isHtml = false)
25+
{
26+
throw new NotImplementedException();
27+
}
28+
29+
30+
/// <summary>
31+
/// send email
32+
/// </summary>
33+
/// <param name="mailTo">consignee email,multi split with ","</param>
34+
/// <param name="subject">subject</param>
35+
/// <param name="message">email message</param>
36+
/// <param name="encoding">email message encoding</param>
37+
/// <param name="isHtml">is set message as html</param>
38+
public void Send(string mailTo, string subject, string message, Encoding encoding, bool isHtml = false)
39+
{
40+
throw new NotImplementedException();
41+
}
42+
43+
/// <summary>
44+
/// send email with UTF-8
45+
/// </summary>
46+
/// <param name="mailTo">consignee email,multi split with ","</param>
47+
/// <param name="mailCc">send cc,multi split with ","</param>
48+
/// <param name="mailBcc">send bcc,multi split with ","</param>
49+
/// <param name="subject">subject</param>
50+
/// <param name="message">email message</param>
51+
/// <param name="isHtml">is set message as html</param>
52+
public void Send(string mailTo, string mailCc, string mailBcc, string subject, string message, bool isHtml = false)
53+
{
54+
throw new NotImplementedException();
55+
}
56+
57+
/// <summary>
58+
/// send email
59+
/// </summary>
60+
/// <param name="mailTo">consignee email,multi split with ","</param>
61+
/// <param name="mailCc">send cc,multi split with ","</param>
62+
/// <param name="mailBcc">send bcc,multi split with ","</param>
63+
/// <param name="subject">subject</param>
64+
/// <param name="message">email message</param>
65+
/// <param name="encoding">email message encoding</param>
66+
/// <param name="isHtml">is set message as html</param>
67+
public void Send(string mailTo, string mailCc, string mailBcc, string subject, string message, Encoding encoding, bool isHtml = false)
68+
{
69+
throw new NotImplementedException();
70+
}
71+
72+
73+
/// <summary>
74+
/// send email with UTF-8 async
75+
/// </summary>
76+
/// <param name="mailTo">consignee email,multi split with ","</param>
77+
/// <param name="subject">subject</param>
78+
/// <param name="message">email message</param>
79+
/// <param name="isHtml">is set message as html</param>
80+
public Task SendAsync(string mailTo, string subject, string message, bool isHtml = false)
81+
{
82+
throw new NotImplementedException();
83+
}
84+
85+
/// <summary>
86+
/// send email async
87+
/// </summary>
88+
/// <param name="mailTo">consignee email,multi split with ","</param>
89+
/// <param name="subject">subject</param>
90+
/// <param name="message">email message</param>
91+
/// <param name="encoding">email message encoding</param>
92+
/// <param name="isHtml">is set message as html</param>
93+
94+
public Task SendAsync(string mailTo, string subject, string message, Encoding encoding, bool isHtml = false)
95+
{
96+
throw new NotImplementedException();
97+
}
98+
99+
/// <summary>
100+
/// send email with UTF-8 async
101+
/// </summary>
102+
/// <param name="mailTo">consignee email,multi split with ","</param>
103+
/// <param name="mailCc">send cc,multi split with ","</param>
104+
/// <param name="mailBcc">send bcc,multi split with ","</param>
105+
/// <param name="subject">subject</param>
106+
/// <param name="message">email message</param>
107+
/// <param name="isHtml">is set message as html</param>
108+
public Task SendAsync(string mailTo, string mailCc, string mailBcc, string subject, string message, bool isHtml = false)
109+
{
110+
throw new NotImplementedException();
111+
}
112+
113+
/// <summary>
114+
/// send email async
115+
/// </summary>
116+
/// <param name="mailTo">consignee email,multi split with ","</param>
117+
/// <param name="mailCc">send cc,multi split with ","</param>
118+
/// <param name="mailBcc">send bcc,multi split with ","</param>
119+
/// <param name="subject">subject</param>
120+
/// <param name="message">email message</param>
121+
/// <param name="encoding">email message encoding</param>
122+
/// <param name="isHtml">is set message as html</param>
123+
public Task SendAsync(string mailTo, string mailCc, string mailBcc, string subject, string message, Encoding encoding, bool isHtml = false)
124+
{
125+
throw new NotImplementedException();
126+
}
11127
}
12128
}
Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,97 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Threading.Tasks;
45

56
namespace NETCore.MailKit.Core
67
{
78
public interface IEmailService
89
{
9-
10+
/// <summary>
11+
/// send email with UTF-8
12+
/// </summary>
13+
/// <param name="mailTo">consignee email,multi split with ","</param>
14+
/// <param name="subject">subject</param>
15+
/// <param name="message">email message</param>
16+
/// <param name="isHtml">is set message as html</param>
17+
void Send(string mailTo, string subject, string message, bool isHtml = false);
18+
19+
/// <summary>
20+
/// send email
21+
/// </summary>
22+
/// <param name="mailTo">consignee email,multi split with ","</param>
23+
/// <param name="subject">subject</param>
24+
/// <param name="message">email message</param>
25+
/// <param name="encoding">email message encoding</param>
26+
/// <param name="isHtml">is set message as html</param>
27+
void Send(string mailTo, string subject, string message, Encoding encoding, bool isHtml = false);
28+
29+
30+
/// <summary>
31+
/// send email with UTF-8
32+
/// </summary>
33+
/// <param name="mailTo">consignee email,multi split with ","</param>
34+
/// <param name="mailCc">send cc,multi split with ","</param>
35+
/// <param name="mailBcc">send bcc,multi split with ","</param>
36+
/// <param name="subject">subject</param>
37+
/// <param name="message">email message</param>
38+
/// <param name="isHtml">is set message as html</param>
39+
void Send(string mailTo, string mailCc, string mailBcc, string subject, string message, bool isHtml = false);
40+
41+
/// <summary>
42+
/// send email
43+
/// </summary>
44+
/// <param name="mailTo">consignee email,multi split with ","</param>
45+
/// <param name="mailCc">send cc,multi split with ","</param>
46+
/// <param name="mailBcc">send bcc,multi split with ","</param>
47+
/// <param name="subject">subject</param>
48+
/// <param name="message">email message</param>
49+
/// <param name="encoding">email message encoding</param>
50+
/// <param name="isHtml">is set message as html</param>
51+
void Send(string mailTo, string mailCc, string mailBcc, string subject, string message, Encoding encoding, bool isHtml = false);
52+
53+
54+
/// <summary>
55+
/// send email with UTF-8 async
56+
/// </summary>
57+
/// <param name="mailTo">consignee email,multi split with ","</param>
58+
/// <param name="subject">subject</param>
59+
/// <param name="message">email message</param>
60+
/// <param name="isHtml">is set message as html</param>
61+
Task SendAsync(string mailTo, string subject, string message, bool isHtml = false);
62+
63+
/// <summary>
64+
/// send email async
65+
/// </summary>
66+
/// <param name="mailTo">consignee email,multi split with ","</param>
67+
/// <param name="subject">subject</param>
68+
/// <param name="message">email message</param>
69+
/// <param name="encoding">email message encoding</param>
70+
/// <param name="isHtml">is set message as html</param>
71+
Task SendAsync(string mailTo, string subject, string message, Encoding encoding, bool isHtml = false);
72+
73+
74+
/// <summary>
75+
/// send email with UTF-8 async
76+
/// </summary>
77+
/// <param name="mailTo">consignee email,multi split with ","</param>
78+
/// <param name="mailCc">send cc,multi split with ","</param>
79+
/// <param name="mailBcc">send bcc,multi split with ","</param>
80+
/// <param name="subject">subject</param>
81+
/// <param name="message">email message</param>
82+
/// <param name="isHtml">is set message as html</param>
83+
Task SendAsync(string mailTo, string mailCc, string mailBcc, string subject, string message, bool isHtml = false);
84+
85+
/// <summary>
86+
/// send email async
87+
/// </summary>
88+
/// <param name="mailTo">consignee email,multi split with ","</param>
89+
/// <param name="mailCc">send cc,multi split with ","</param>
90+
/// <param name="mailBcc">send bcc,multi split with ","</param>
91+
/// <param name="subject">subject</param>
92+
/// <param name="message">email message</param>
93+
/// <param name="encoding">email message encoding</param>
94+
/// <param name="isHtml">is set message as html</param>
95+
Task SendAsync(string mailTo, string mailCc, string mailBcc, string subject, string message, Encoding encoding, bool isHtml = false);
1096
}
1197
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
using MailKit.Net.Smtp;
12
using System;
23

34
namespace NETCore.MailKit
45
{
56
public interface IMailKitProvider
67
{
8+
/// <summary>
9+
/// smtp client
10+
/// </summary>
11+
SmtpClient Client { get; }
712
}
813
}

‎src/NETCore.MailKit/Infrastructure/Internal/MailKitOptions.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,44 @@ namespace NETCore.MailKit.Infrastructure.Internal
66
{
77
public class MailKitOptions
88
{
9+
public MailKitOptions()
10+
{
911

12+
}
13+
14+
/// <summary>
15+
/// SMTP Server address
16+
/// </summary>
17+
public string Server { get; set; }
18+
19+
/// <summary>
20+
/// SMTP Server Port ,default is 25
21+
/// </summary>
22+
public int Port { get; set; } = 25;
23+
24+
/// <summary>
25+
/// send user name
26+
/// </summary>
27+
public string SenderName { get; set; }
28+
29+
/// <summary>
30+
/// send user email
31+
/// </summary>
32+
public string SenderEmail { get; set; }
33+
34+
/// <summary>
35+
/// send user account,may be equal to senderemail
36+
/// </summary>
37+
public string Account { get; set; }
38+
39+
/// <summary>
40+
/// send user password
41+
/// </summary>
42+
public string Passord { get; set; }
43+
44+
/// <summary>
45+
/// enable ssl
46+
/// </summary>
47+
public bool SSL { get; set; } = false;
1048
}
1149
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
1+
using NETCore.MailKit.Infrastructure.Internal;
12
using System;
23
using System.Collections.Generic;
34
using System.Text;
5+
using MailKit.Net.Smtp;
6+
using MailKit.Security;
47

58
namespace NETCore.MailKit
69
{
710
public class MailKitProvider : IMailKitProvider
811
{
12+
public readonly MailKitOptions _MailKitOptions;
13+
14+
public MailKitProvider(MailKitOptions mailKitOptions)
15+
{
16+
_MailKitOptions = mailKitOptions;
17+
}
18+
19+
public SmtpClient Client
20+
{
21+
get
22+
{
23+
return lazySmtpClient().Value;
24+
}
25+
}
26+
27+
28+
private Lazy<SmtpClient> lazySmtpClient()
29+
{
30+
return new Lazy<SmtpClient>(() =>
31+
{
32+
return InitSmtpClient();
33+
});
34+
}
35+
36+
private SmtpClient InitSmtpClient()
37+
{
38+
var client = new SmtpClient();
39+
if (_MailKitOptions.SSL)
40+
{
41+
client.Connect(_MailKitOptions.Server, _MailKitOptions.Port, SecureSocketOptions.None);
42+
}
43+
else
44+
{
45+
client.Connect(_MailKitOptions.Server, _MailKitOptions.Port, SecureSocketOptions.SslOnConnect);
46+
}
47+
48+
return client;
49+
}
950
}
1051
}

‎src/NETCore.MailKit/NETCore.MailKit.csproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
1010
</ItemGroup>
1111

12+
<ItemGroup>
13+
<Folder Include="Core\Internal\" />
14+
</ItemGroup>
15+
1216
</Project>

0 commit comments

Comments
 (0)