Skip to content

Commit 3969b0e

Browse files
committed
[#494] [add] impl
1 parent 808ba38 commit 3969b0e

File tree

12 files changed

+388
-369
lines changed

12 files changed

+388
-369
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"seealso",
1919
"serializer",
2020
"SMTP",
21+
"snupkg",
2122
"typeparam",
2223
"Сonveyor"
2324
]

src/Simplify.Mail.IntegrationTests/App.config

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Simplify.Mail.IntegrationTests/MailSenderTests.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
using Microsoft.Extensions.Configuration;
23
using NUnit.Framework;
34
using Simplify.DI;
45
using Simplify.DI.Provider.DryIoc;
@@ -10,27 +11,41 @@ namespace Simplify.Mail.IntegrationTests;
1011
[Category("Integration")]
1112
public class MailSenderTests
1213
{
13-
[Test]
14-
public void SendSimpleTestEmail()
14+
private IConfiguration _configuration;
15+
private IMailSender _mailSender;
16+
17+
[SetUp]
18+
public void Setup()
1519
{
16-
MailSender.Default.Send("[email protected]", "[email protected]", "Hello subject", "Hello World!!!");
20+
_configuration = new ConfigurationBuilder()
21+
.AddJsonFile("appsettings.json")
22+
.Build();
23+
24+
_mailSender = new MailSender(_configuration);
1725
}
1826

19-
[Test]
20-
public Task SendSimpleAsyncTestEmail()
27+
[TearDown]
28+
public void TearDown()
2129
{
22-
return MailSender.Default.SendAsync("[email protected]",
23-
"[email protected]", "Hello subject", "Hello World!!!");
30+
_mailSender.Dispose();
2431
}
2532

33+
[Test]
34+
public void SendSimpleTestEmail() =>
35+
_mailSender.Send("[email protected]", "[email protected]", "Hello subject", "Hello World!!!");
36+
37+
[Test]
38+
public async Task SendSimpleAsyncTestEmail() =>
39+
await _mailSender.SendAsync("[email protected]", "[email protected]", "Hello subject", "Hello World!!!");
40+
2641
[Test]
2742
public async Task SendAsync_TwoDuplicates_OneSend()
2843
{
2944
// Arrange
3045

3146
var container = new DryIocDIProvider();
3247

33-
container.Register<IMailSender>(r => new MailSender());
48+
container.Register<IMailSender>(r => new MailSender(_configuration));
3449
container.Verify();
3550

3651
const string from = "[email protected]";

src/Simplify.Mail.IntegrationTests/Simplify.Mail.IntegrationTests.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net48</TargetFrameworks>
3+
<TargetFramework>net9.0</TargetFramework>
44
<LangVersion>latest</LangVersion>
55

66
<Authors>Alexander Krylkov</Authors>
@@ -12,9 +12,15 @@
1212
<ProjectReference Include="..\Simplify.Mail\Simplify.Mail.csproj" />
1313
</ItemGroup>
1414
<ItemGroup>
15+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.*" />
1516
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.*" />
1617
<PackageReference Include="NUnit" Version="4.4.*" />
1718
<PackageReference Include="NUnit3TestAdapter" Version="5.2.*" />
1819
<PackageReference Include="Simplify.DI" Version="4.2.*" />
1920
</ItemGroup>
21+
<ItemGroup>
22+
<None Update="appsettings.json">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
2026
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"MailSenderSettings": {
3+
"SmtpServerAddress": "localhost",
4+
"SmtpUserName": "test",
5+
"SmtpUserPassword": "test"
6+
}
7+
}

src/Simplify.Mail/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## [2.0] - Unreleased
4+
5+
### Breaking change
6+
7+
- Switched from System.Net.Mail to MimeKit
8+
9+
### Removed
10+
11+
- System.Configuration.ConfigurationManager base configuration support
12+
313
## [1.6.0] - 2025-06-15
414

515
### Removed

src/Simplify.Mail/IMailSender.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
using System.Collections.Generic;
2-
using System.Net.Mail;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
34
using System.Threading.Tasks;
5+
using MailKit.Net.Smtp;
6+
using MimeKit;
47
using Simplify.Mail.Settings;
58

69
namespace Simplify.Mail;
710

811
/// <summary>
912
/// Represent E-mail sending interface.
1013
/// </summary>
11-
public interface IMailSender
14+
public interface IMailSender : IDisposable
1215
{
1316
/// <summary>
1417
/// MailSender settings.
@@ -25,31 +28,33 @@ public interface IMailSender
2528
/// Send single e-mail.
2629
/// </summary>
2730
/// <param name="client">Smtp client.</param>
28-
/// <param name="mailMessage">The mail message.</param>
31+
/// <param name="mimeMessage">The MIME message.</param>
2932
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
30-
void Send(SmtpClient client, MailMessage mailMessage, string bodyForAntiSpam = null);
33+
void Send(SmtpClient client, MimeMessage mimeMessage, string bodyForAntiSpam = null);
3134

3235
/// <summary>
3336
/// Send single e-mail asynchronously.
3437
/// </summary>
3538
/// <param name="client">Smtp client.</param>
36-
/// <param name="mailMessage">The mail message.</param>
39+
/// <param name="mimeMessage">The MIME message.</param>
3740
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
38-
Task SendAsync(SmtpClient client, MailMessage mailMessage, string bodyForAntiSpam = null);
41+
/// <param name="cancellationToken">The cancellation token.</param>
42+
Task SendAsync(SmtpClient client, MimeMessage mimeMessage, string bodyForAntiSpam = null, CancellationToken cancellationToken = default);
3943

4044
/// <summary>
4145
/// Send single e-mail.
4246
/// </summary>
43-
/// <param name="mailMessage">The mail message.</param>
47+
/// <param name="mimeMessage">The MIME message.</param>
4448
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
45-
void Send(MailMessage mailMessage, string bodyForAntiSpam = null);
49+
void Send(MimeMessage mimeMessage, string bodyForAntiSpam = null);
4650

4751
/// <summary>
4852
/// Send single e-mail asynchronously.
4953
/// </summary>
50-
/// <param name="mailMessage">The mail message.</param>
54+
/// <param name="mimeMessage">The MIME message.</param>
5155
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
52-
Task SendAsync(MailMessage mailMessage, string bodyForAntiSpam = null);
56+
/// <param name="cancellationToken">The cancellation token.</param>
57+
Task SendAsync(MimeMessage mimeMessage, string bodyForAntiSpam = null, CancellationToken cancellationToken = default);
5358

5459
/// <summary>
5560
/// Send single e-mail.
@@ -65,7 +70,7 @@ public interface IMailSender
6570
/// Process status, <see langword="true" /> if message is processed to sent successfully
6671
/// </returns>
6772
void Send(SmtpClient client, string from, string to, string subject, string body, string bodyForAntiSpam = null,
68-
params Attachment[] attachments);
73+
params MimeEntity[] attachments);
6974

7075
/// <summary>
7176
/// Send single e-mail asynchronously.
@@ -77,11 +82,12 @@ void Send(SmtpClient client, string from, string to, string subject, string body
7782
/// <param name="body">e-mail body.</param>
7883
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
7984
/// <param name="attachments">The attachments to an e-mail.</param>
85+
/// <param name="cancellationToken">The cancellation token.</param>
8086
/// <returns>
8187
/// Process status, <see langword="true" /> if message is processed to sent successfully
8288
/// </returns>
8389
Task SendAsync(SmtpClient client, string from, string to, string subject, string body, string bodyForAntiSpam = null,
84-
params Attachment[] attachments);
90+
MimeEntity[] attachments = null, CancellationToken cancellationToken = default);
8591

8692
/// <summary>
8793
/// Send single e-mail.
@@ -94,7 +100,7 @@ Task SendAsync(SmtpClient client, string from, string to, string subject, string
94100
/// <param name="attachments">The attachments to an e-mail.</param>
95101
/// <returns>Process status, <see langword="true"/> if message is processed to sent successfully</returns>
96102
void Send(string from, string to, string subject, string body, string bodyForAntiSpam = null,
97-
params Attachment[] attachments);
103+
params MimeEntity[] attachments);
98104

99105
/// <summary>
100106
/// Send single e-mail asynchronously.
@@ -105,9 +111,10 @@ void Send(string from, string to, string subject, string body, string bodyForAnt
105111
/// <param name="body">e-mail body.</param>
106112
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
107113
/// <param name="attachments">The attachments to an e-mail.</param>
114+
/// <param name="cancellationToken">The cancellation token.</param>
108115
/// <returns>Process status, <see langword="true"/> if message is processed to sent successfully</returns>
109116
Task SendAsync(string from, string to, string subject, string body, string bodyForAntiSpam = null,
110-
params Attachment[] attachments);
117+
MimeEntity[] attachments = null, CancellationToken cancellationToken = default);
111118

112119
/// <summary>
113120
/// Send e-mail to multiple recipients in one e-mail.
@@ -121,7 +128,7 @@ Task SendAsync(string from, string to, string subject, string body, string bodyF
121128
/// <param name="attachments">The attachments to an e-mail.</param>
122129
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
123130
void Send(SmtpClient client, string fromMailAddress, IList<string> addresses, string subject, string body,
124-
string bodyForAntiSpam = null, params Attachment[] attachments);
131+
string bodyForAntiSpam = null, params MimeEntity[] attachments);
125132

126133
/// <summary>
127134
/// Send e-mail to multiple recipients in one e-mail asynchronously.
@@ -133,9 +140,10 @@ void Send(SmtpClient client, string fromMailAddress, IList<string> addresses, st
133140
/// <param name="body">e-mail body.</param>
134141
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
135142
/// <param name="attachments">The attachments to an e-mail.</param>
143+
/// <param name="cancellationToken">The cancellation token.</param>
136144
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
137145
Task SendAsync(SmtpClient client, string fromMailAddress, IList<string> addresses, string subject, string body,
138-
string bodyForAntiSpam = null, params Attachment[] attachments);
146+
string bodyForAntiSpam = null, MimeEntity[] attachments = null, CancellationToken cancellationToken = default);
139147

140148
/// <summary>
141149
/// Send e-mail to multiple recipients in one e-mail.
@@ -148,7 +156,7 @@ Task SendAsync(SmtpClient client, string fromMailAddress, IList<string> addresse
148156
/// <param name="attachments">The attachments to an e-mail.</param>
149157
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
150158
void Send(string fromMailAddress, IList<string> addresses, string subject, string body,
151-
string bodyForAntiSpam = null, params Attachment[] attachments);
159+
string bodyForAntiSpam = null, params MimeEntity[] attachments);
152160

153161
/// <summary>
154162
/// Send e-mail to multiple recipients in one e-mail asynchronously.
@@ -159,9 +167,10 @@ void Send(string fromMailAddress, IList<string> addresses, string subject, strin
159167
/// <param name="body">e-mail body.</param>
160168
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
161169
/// <param name="attachments">The attachments to an e-mail.</param>
170+
/// <param name="cancellationToken">The cancellation token.</param>
162171
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
163172
Task SendAsync(string fromMailAddress, IList<string> addresses, string subject, string body,
164-
string bodyForAntiSpam = null, params Attachment[] attachments);
173+
string bodyForAntiSpam = null, MimeEntity[] attachments = null, CancellationToken cancellationToken = default);
165174

166175
/// <summary>
167176
/// Send e-mail to multiple recipients and carbon copy recipients in one e-mail.
@@ -176,7 +185,7 @@ Task SendAsync(string fromMailAddress, IList<string> addresses, string subject,
176185
/// <param name="attachments">The attachments to an e-mail.</param>
177186
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
178187
void Send(SmtpClient client, string fromMailAddress, IList<string> addresses, IList<string> ccAddresses,
179-
string subject, string body, string bodyForAntiSpam = null, params Attachment[] attachments);
188+
string subject, string body, string bodyForAntiSpam = null, params MimeEntity[] attachments);
180189

181190
/// <summary>
182191
/// Send e-mail to multiple recipients and carbon copy recipients in one e-mail asynchronously.
@@ -189,9 +198,10 @@ void Send(SmtpClient client, string fromMailAddress, IList<string> addresses, IL
189198
/// <param name="body">e-mail body.</param>
190199
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
191200
/// <param name="attachments">The attachments to an e-mail.</param>
201+
/// <param name="cancellationToken">The cancellation token.</param>
192202
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
193203
Task SendAsync(SmtpClient client, string fromMailAddress, IList<string> addresses, IList<string> ccAddresses,
194-
string subject, string body, string bodyForAntiSpam = null, params Attachment[] attachments);
204+
string subject, string body, string bodyForAntiSpam = null, MimeEntity[] attachments = null, CancellationToken cancellationToken = default);
195205

196206
/// <summary>
197207
/// Send e-mail to multiple recipients and carbon copy recipients in one e-mail.
@@ -204,7 +214,8 @@ Task SendAsync(SmtpClient client, string fromMailAddress, IList<string> addresse
204214
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
205215
/// <param name="attachments">The attachments to an e-mail.</param>
206216
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
207-
void Send(string fromMailAddress, IList<string> addresses, IList<string> ccAddresses, string subject, string body, string bodyForAntiSpam = null, params Attachment[] attachments);
217+
void Send(string fromMailAddress, IList<string> addresses, IList<string> ccAddresses, string subject, string body,
218+
string bodyForAntiSpam = null, params MimeEntity[] attachments);
208219

209220
/// <summary>
210221
/// Send e-mail to multiple recipients and carbon copy recipients in one e-mail asynchronously.
@@ -216,9 +227,10 @@ Task SendAsync(SmtpClient client, string fromMailAddress, IList<string> addresse
216227
/// <param name="body">e-mail body.</param>
217228
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
218229
/// <param name="attachments">The attachments to an e-mail.</param>
230+
/// <param name="cancellationToken">The cancellation token.</param>
219231
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
220232
Task SendAsync(string fromMailAddress, IList<string> addresses, IList<string> ccAddresses, string subject, string body,
221-
string bodyForAntiSpam = null, params Attachment[] attachments);
233+
string bodyForAntiSpam = null, MimeEntity[] attachments = null, CancellationToken cancellationToken = default);
222234

223235
/// <summary>
224236
/// Send e-mail to multiple recipients separately
@@ -232,7 +244,7 @@ Task SendAsync(string fromMailAddress, IList<string> addresses, IList<string> cc
232244
/// <param name="attachments">The attachments to an e-mail.</param>
233245
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
234246
void SendSeparately(SmtpClient client, string fromMailAddress, IList<string> addresses, string subject, string body,
235-
string bodyForAntiSpam = null, params Attachment[] attachments);
247+
string bodyForAntiSpam = null, params MimeEntity[] attachments);
236248

237249
/// <summary>
238250
/// Send e-mail to multiple recipients separately
@@ -244,9 +256,10 @@ void SendSeparately(SmtpClient client, string fromMailAddress, IList<string> add
244256
/// <param name="body">e-mail body.</param>
245257
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
246258
/// <param name="attachments">The attachments to an e-mail.</param>
259+
/// <param name="cancellationToken">The cancellation token.</param>
247260
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
248261
Task SendSeparatelyAsync(SmtpClient client, string fromMailAddress, IList<string> addresses, string subject, string body,
249-
string bodyForAntiSpam = null, params Attachment[] attachments);
262+
string bodyForAntiSpam = null, MimeEntity[] attachments = null, CancellationToken cancellationToken = default);
250263

251264
/// <summary>
252265
/// Send e-mail to multiple recipients separately
@@ -259,7 +272,7 @@ Task SendSeparatelyAsync(SmtpClient client, string fromMailAddress, IList<string
259272
/// <param name="attachments">The attachments to an e-mail.</param>
260273
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
261274
void SendSeparately(string fromMailAddress, IList<string> addresses, string subject, string body,
262-
string bodyForAntiSpam = null, params Attachment[] attachments);
275+
string bodyForAntiSpam = null, params MimeEntity[] attachments);
263276

264277
/// <summary>
265278
/// Send e-mail to multiple recipients separately
@@ -270,7 +283,8 @@ void SendSeparately(string fromMailAddress, IList<string> addresses, string subj
270283
/// <param name="body">e-mail body.</param>
271284
/// <param name="bodyForAntiSpam">Part of an e-mail body just for anti-spam checking.</param>
272285
/// <param name="attachments">The attachments to an e-mail.</param>
286+
/// <param name="cancellationToken">The cancellation token.</param>
273287
/// <returns>Process status, <see langword="true"/> if all messages are processed to sent successfully</returns>
274288
Task SendSeparatelyAsync(string fromMailAddress, IList<string> addresses, string subject, string body,
275-
string bodyForAntiSpam = null, params Attachment[] attachments);
276-
}
289+
string bodyForAntiSpam = null, MimeEntity[] attachments = null, CancellationToken cancellationToken = default);
290+
}

0 commit comments

Comments
 (0)