Skip to content

Commit 4b1ff95

Browse files
committed
Updated to .NET6 / Cofoundry v0.11
1 parent d779ee6 commit 4b1ff95

15 files changed

+177
-216
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.cs]
2+
csharp_style_namespace_declarations = file_scoped:warning

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Desktop.ini
9696
# Mac crap
9797
.DS_Store
9898

99-
src/.vs
99+
.vs
100100
.vscode
101101

102102
# Cake Build

src/Cofoundry.Plugins.Mail.SendGrid.sln renamed to Cofoundry.Plugins.Mail.SendGrid.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.32126.315
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cofoundry.Plugins.Mail.SendGrid", "Cofoundry.Plugins.Mail.SendGrid\Cofoundry.Plugins.Mail.SendGrid.csproj", "{28EE7BA3-A12F-4F17-B653-95AB4C9A1D3A}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cofoundry.Plugins.Mail.SendGrid", "src\Cofoundry.Plugins.Mail.SendGrid\Cofoundry.Plugins.Mail.SendGrid.csproj", "{28EE7BA3-A12F-4F17-B653-95AB4C9A1D3A}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SendGridExample", "SendGridExample\SendGridExample.csproj", "{BE4C267F-5545-430C-8EFE-A5203E8F5698}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SendGridExample", "src\SendGridExample\SendGridExample.csproj", "{BE4C267F-5545-430C-8EFE-A5203E8F5698}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{2D2385FA-87FB-4FE1-9C47-F105AD8FCF91}"
1111
EndProject
File renamed without changes.

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version: '{build}'
2-
image: Visual Studio 2019
2+
image: Visual Studio 2022
33
build_script:
44
- cmd: PowerShell .\build.ps1 --PushPackages="true"
55
test: off

src/Cofoundry.Plugins.Mail.SendGrid/Bootstrap/SendGridDependencyRegistration.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
using Cofoundry.Core.Mail;
33
using Cofoundry.Plugins.Mail.SendGrid.Internal;
44

5-
namespace Cofoundry.Plugins.Mail.SendGrid.Registration
5+
namespace Cofoundry.Plugins.Mail.SendGrid.Registration;
6+
7+
public class SendGridDependencyRegistration : IDependencyRegistration
68
{
7-
public class SendGridDependencyRegistration : IDependencyRegistration
9+
public void Register(IContainerRegister container)
810
{
9-
public void Register(IContainerRegister container)
10-
{
11-
if (container.Configuration.GetValue<bool>("Cofoundry:Plugins:SendGrid:Disabled")) return;
11+
if (container.Configuration.GetValue<bool>("Cofoundry:Plugins:SendGrid:Disabled")) return;
1212

13-
var overrideOptions = RegistrationOptions.Override();
13+
var overrideOptions = RegistrationOptions.Override();
1414

15-
container
16-
.Register<IMailDispatchSession, SendGridMailDispatchSession>(overrideOptions)
17-
;
18-
}
15+
container
16+
.Register<IMailDispatchSession, SendGridMailDispatchSession>(overrideOptions)
17+
;
1918
}
2019
}

src/Cofoundry.Plugins.Mail.SendGrid/Cofoundry.Plugins.Mail.SendGrid.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
56

67
<PackageId>Cofoundry.Plugins.Mail.SendGrid</PackageId>
78
<Description>Cofoundry mail service implemention for SendGrid.</Description>
@@ -11,12 +12,12 @@
1112
</PropertyGroup>
1213

1314
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
14-
<DocumentationFile>bin\Release\netcoreapp3.1\Cofoundry.Plugins.Mail.SendGrid.xml</DocumentationFile>
15+
<DocumentationFile>bin\Release\net6.0\Cofoundry.Plugins.Mail.SendGrid.xml</DocumentationFile>
1516
<NoWarn>1701;1702;1705;1591</NoWarn>
1617
</PropertyGroup>
1718

1819
<ItemGroup>
19-
<PackageReference Include="Cofoundry.Core" Version="0.10.0" />
20+
<PackageReference Include="Cofoundry.Core" Version="0.11.1" />
2021
<PackageReference Include="Sendgrid" Version="9.26.0" />
2122
</ItemGroup>
2223

src/Cofoundry.Plugins.Mail.SendGrid/SendGridMailDispatchSession.cs

Lines changed: 97 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,138 +3,134 @@
33
using Cofoundry.Core.Mail.Internal;
44
using SendGrid;
55
using SendGrid.Helpers.Mail;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Threading.Tasks;
96

10-
namespace Cofoundry.Plugins.Mail.SendGrid.Internal
7+
namespace Cofoundry.Plugins.Mail.SendGrid.Internal;
8+
9+
public class SendGridMailDispatchSession : IMailDispatchSession
1110
{
12-
public class SendGridMailDispatchSession : IMailDispatchSession
11+
private readonly Queue<SendGridMessage> _mailQueue = new Queue<SendGridMessage>();
12+
private readonly Core.Mail.MailSettings _mailSettings;
13+
private readonly SendGridSettings _sendGridSettings;
14+
private readonly SendGridClient _sendGridClient;
15+
private readonly DebugMailDispatchSession _debugMailDispatchSession;
16+
17+
public SendGridMailDispatchSession(
18+
Core.Mail.MailSettings mailSettings,
19+
SendGridSettings sendGridSettings,
20+
IPathResolver pathResolver
21+
)
1322
{
14-
private readonly Queue<SendGridMessage> _mailQueue = new Queue<SendGridMessage>();
15-
private readonly Core.Mail.MailSettings _mailSettings;
16-
private readonly SendGridSettings _sendGridSettings;
17-
private readonly SendGridClient _sendGridClient;
18-
private readonly DebugMailDispatchSession _debugMailDispatchSession;
19-
20-
public SendGridMailDispatchSession(
21-
Core.Mail.MailSettings mailSettings,
22-
SendGridSettings sendGridSettings,
23-
IPathResolver pathResolver
24-
)
25-
{
26-
_mailSettings = mailSettings;
27-
_sendGridSettings = sendGridSettings;
23+
_mailSettings = mailSettings;
24+
_sendGridSettings = sendGridSettings;
2825

29-
if (_mailSettings.SendMode == MailSendMode.LocalDrop)
30-
{
31-
_debugMailDispatchSession = new DebugMailDispatchSession(mailSettings, pathResolver);
32-
}
33-
else
34-
{
35-
_sendGridClient = new SendGridClient(_sendGridSettings.ApiKey);
36-
}
26+
if (_mailSettings.SendMode == MailSendMode.LocalDrop)
27+
{
28+
_debugMailDispatchSession = new DebugMailDispatchSession(mailSettings, pathResolver);
3729
}
38-
39-
public void Add(MailMessage mailMessage)
30+
else
4031
{
41-
var messageToSend = FormatMessage(mailMessage);
32+
_sendGridClient = new SendGridClient(_sendGridSettings.ApiKey);
33+
}
34+
}
4235

43-
if (_mailSettings.SendMode == MailSendMode.LocalDrop)
44-
{
45-
_debugMailDispatchSession.Add(mailMessage);
46-
return;
47-
}
36+
public void Add(MailMessage mailMessage)
37+
{
38+
var messageToSend = FormatMessage(mailMessage);
4839

49-
_mailQueue.Enqueue(messageToSend);
40+
if (_mailSettings.SendMode == MailSendMode.LocalDrop)
41+
{
42+
_debugMailDispatchSession.Add(mailMessage);
43+
return;
5044
}
5145

52-
public async Task FlushAsync()
46+
_mailQueue.Enqueue(messageToSend);
47+
}
48+
49+
public async Task FlushAsync()
50+
{
51+
if (_mailSettings.SendMode == MailSendMode.LocalDrop)
5352
{
54-
if (_mailSettings.SendMode == MailSendMode.LocalDrop)
55-
{
56-
await _debugMailDispatchSession.FlushAsync();
57-
return;
58-
}
53+
await _debugMailDispatchSession.FlushAsync();
54+
return;
55+
}
5956

60-
while (_mailQueue.Count > 0)
57+
while (_mailQueue.Count > 0)
58+
{
59+
var mailItem = _mailQueue.Dequeue();
60+
if (mailItem != null && _mailSettings.SendMode != MailSendMode.DoNotSend)
6161
{
62-
var mailItem = _mailQueue.Dequeue();
63-
if (mailItem != null && _mailSettings.SendMode != MailSendMode.DoNotSend)
64-
{
65-
await _sendGridClient.SendEmailAsync(mailItem);
66-
}
62+
await _sendGridClient.SendEmailAsync(mailItem);
6763
}
6864
}
65+
}
6966

70-
public void Dispose()
71-
{
72-
_debugMailDispatchSession?.Dispose();
73-
}
67+
public void Dispose()
68+
{
69+
_debugMailDispatchSession?.Dispose();
70+
}
7471

75-
private SendGridMessage FormatMessage(MailMessage message)
76-
{
77-
if (message == null) throw new ArgumentNullException(nameof(message));
72+
private SendGridMessage FormatMessage(MailMessage message)
73+
{
74+
if (message == null) throw new ArgumentNullException(nameof(message));
7875

79-
var messageToSend = new SendGridMessage();
76+
var messageToSend = new SendGridMessage();
8077

81-
var toAddress = GetMailToAddress(message);
82-
messageToSend.AddTo(toAddress);
83-
messageToSend.Subject = message.Subject;
84-
if (message.From != null)
85-
{
86-
messageToSend.SetFrom(CreateMailAddress(message.From.Address, message.From.DisplayName));
87-
}
88-
else
89-
{
90-
messageToSend.SetFrom(CreateMailAddress(_mailSettings.DefaultFromAddress, _mailSettings.DefaultFromAddressDisplayName));
91-
}
78+
var toAddress = GetMailToAddress(message);
79+
messageToSend.AddTo(toAddress);
80+
messageToSend.Subject = message.Subject;
81+
if (message.From != null)
82+
{
83+
messageToSend.SetFrom(CreateMailAddress(message.From.Address, message.From.DisplayName));
84+
}
85+
else
86+
{
87+
messageToSend.SetFrom(CreateMailAddress(_mailSettings.DefaultFromAddress, _mailSettings.DefaultFromAddressDisplayName));
88+
}
9289

93-
SetMessageBody(messageToSend, message.HtmlBody, message.TextBody);
90+
SetMessageBody(messageToSend, message.HtmlBody, message.TextBody);
9491

95-
return messageToSend;
96-
}
92+
return messageToSend;
93+
}
9794

98-
private EmailAddress GetMailToAddress(MailMessage message)
95+
private EmailAddress GetMailToAddress(MailMessage message)
96+
{
97+
EmailAddress toAddress;
98+
if (_mailSettings.SendMode == MailSendMode.SendToDebugAddress)
9999
{
100-
EmailAddress toAddress;
101-
if (_mailSettings.SendMode == MailSendMode.SendToDebugAddress)
100+
if (string.IsNullOrEmpty(_mailSettings.DebugEmailAddress))
102101
{
103-
if (string.IsNullOrEmpty(_mailSettings.DebugEmailAddress))
104-
{
105-
throw new Exception("MailSendMode.SendToDebugAddress requested but Cofoundry:Mail:DebugEmailAddress setting is not defined.");
106-
}
107-
toAddress = CreateMailAddress(_mailSettings.DebugEmailAddress, message.To.DisplayName);
102+
throw new Exception("MailSendMode.SendToDebugAddress requested but Cofoundry:Mail:DebugEmailAddress setting is not defined.");
108103
}
109-
else
110-
{
111-
toAddress = new EmailAddress(message.To.Address, message.To.DisplayName);
112-
}
113-
return toAddress;
104+
toAddress = CreateMailAddress(_mailSettings.DebugEmailAddress, message.To.DisplayName);
114105
}
115-
116-
private EmailAddress CreateMailAddress(string email, string displayName)
106+
else
117107
{
118-
// In other libraries we catch validation exceptions here, but SendGrid does not throw any so it is omitted
119-
if (string.IsNullOrEmpty(displayName))
120-
{
121-
return new EmailAddress(email);
122-
}
123-
124-
return new EmailAddress(email, displayName);
108+
toAddress = new EmailAddress(message.To.Address, message.To.DisplayName);
125109
}
110+
return toAddress;
111+
}
126112

127-
private void SetMessageBody(SendGridMessage message, string bodyHtml, string bodyText)
113+
private EmailAddress CreateMailAddress(string email, string displayName)
114+
{
115+
// In other libraries we catch validation exceptions here, but SendGrid does not throw any so it is omitted
116+
if (string.IsNullOrEmpty(displayName))
128117
{
129-
var hasHtmlBody = !string.IsNullOrWhiteSpace(bodyHtml);
130-
var hasTextBody = !string.IsNullOrWhiteSpace(bodyText);
131-
if (!hasHtmlBody && !hasTextBody)
132-
{
133-
throw new ArgumentException("An email must have either a html or text body");
134-
}
118+
return new EmailAddress(email);
119+
}
120+
121+
return new EmailAddress(email, displayName);
122+
}
135123

136-
message.HtmlContent = bodyHtml;
137-
message.PlainTextContent = bodyText;
124+
private void SetMessageBody(SendGridMessage message, string bodyHtml, string bodyText)
125+
{
126+
var hasHtmlBody = !string.IsNullOrWhiteSpace(bodyHtml);
127+
var hasTextBody = !string.IsNullOrWhiteSpace(bodyText);
128+
if (!hasHtmlBody && !hasTextBody)
129+
{
130+
throw new ArgumentException("An email must have either a html or text body");
138131
}
132+
133+
message.HtmlContent = bodyHtml;
134+
message.PlainTextContent = bodyText;
139135
}
140136
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using Cofoundry.Core.Configuration;
22
using System.ComponentModel.DataAnnotations;
33

4-
namespace Cofoundry.Plugins.Mail.SendGrid
4+
namespace Cofoundry.Plugins.Mail.SendGrid;
5+
6+
public class SendGridSettings : PluginConfigurationSettingsBase
57
{
6-
public class SendGridSettings : PluginConfigurationSettingsBase
7-
{
8-
/// <summary>
9-
/// Indicates whether the plugin should be disabled, which means services
10-
/// will not be bootstrapped. Defaults to false.
11-
/// </summary>
12-
public bool Disabled { get; set; }
8+
/// <summary>
9+
/// Indicates whether the plugin should be disabled, which means services
10+
/// will not be bootstrapped. Defaults to false.
11+
/// </summary>
12+
public bool Disabled { get; set; }
1313

14-
/// <summary>
15-
/// The api key use when authenticating with the SendGrid api.
16-
/// </summary>
17-
[Required]
18-
public string ApiKey { get; set; }
19-
}
14+
/// <summary>
15+
/// The api key use when authenticating with the SendGrid api.
16+
/// </summary>
17+
[Required]
18+
public string ApiKey { get; set; }
2019
}

0 commit comments

Comments
 (0)