Skip to content

Commit 648f818

Browse files
committed
* Upgraded to Cofoundry v0.10 pre-release
* Added example project * Fixed bug with running in LocalDrop mode
1 parent 00660ba commit 648f818

20 files changed

+352
-64
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,7 @@ src/.vs
101101

102102
# Cake Build
103103
/tools
104-
/artifacts
104+
/artifacts
105+
106+
appsettings.local.json
107+
APP_Data/

src/Cofoundry.Plugins.Mail.SendGrid.sln

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26430.13
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.32126.315
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "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", "Cofoundry.Plugins.Mail.SendGrid\Cofoundry.Plugins.Mail.SendGrid.csproj", "{28EE7BA3-A12F-4F17-B653-95AB4C9A1D3A}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SendGridExample", "SendGridExample\SendGridExample.csproj", "{BE4C267F-5545-430C-8EFE-A5203E8F5698}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{2D2385FA-87FB-4FE1-9C47-F105AD8FCF91}"
711
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,8 +19,18 @@ Global
1519
{28EE7BA3-A12F-4F17-B653-95AB4C9A1D3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
1620
{28EE7BA3-A12F-4F17-B653-95AB4C9A1D3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
1721
{28EE7BA3-A12F-4F17-B653-95AB4C9A1D3A}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{BE4C267F-5545-430C-8EFE-A5203E8F5698}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{BE4C267F-5545-430C-8EFE-A5203E8F5698}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{BE4C267F-5545-430C-8EFE-A5203E8F5698}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{BE4C267F-5545-430C-8EFE-A5203E8F5698}.Release|Any CPU.Build.0 = Release|Any CPU
1826
EndGlobalSection
1927
GlobalSection(SolutionProperties) = preSolution
2028
HideSolutionNode = FALSE
2129
EndGlobalSection
30+
GlobalSection(NestedProjects) = preSolution
31+
{BE4C267F-5545-430C-8EFE-A5203E8F5698} = {2D2385FA-87FB-4FE1-9C47-F105AD8FCF91}
32+
EndGlobalSection
33+
GlobalSection(ExtensibilityGlobals) = postSolution
34+
SolutionGuid = {74105766-BAD0-4183-AF4E-744F172B7750}
35+
EndGlobalSection
2236
EndGlobal
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using Cofoundry.Core.DependencyInjection;
22
using Cofoundry.Core.Mail;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
3+
using Cofoundry.Plugins.Mail.SendGrid.Internal;
64

7-
namespace Cofoundry.Plugins.Mail.SendGrid
5+
namespace Cofoundry.Plugins.Mail.SendGrid.Registration
86
{
97
public class SendGridDependencyRegistration : IDependencyRegistration
108
{
@@ -15,8 +13,8 @@ public void Register(IContainerRegister container)
1513
var overrideOptions = RegistrationOptions.Override();
1614

1715
container
18-
.Register<IMailDispatchService, SendGridMailDispatchService>(overrideOptions)
16+
.Register<IMailDispatchSession, SendGridMailDispatchSession>(overrideOptions)
1917
;
2018
}
2119
}
22-
}
20+
}

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

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

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
using SendGrid.Helpers.Mail;
66
using System;
77
using System.Collections.Generic;
8-
using System.Text;
98
using System.Threading.Tasks;
109

11-
namespace Cofoundry.Plugins.Mail.SendGrid
10+
namespace Cofoundry.Plugins.Mail.SendGrid.Internal
1211
{
1312
public class SendGridMailDispatchSession : IMailDispatchSession
1413
{
@@ -40,12 +39,19 @@ IPathResolver pathResolver
4039
public void Add(MailMessage mailMessage)
4140
{
4241
var messageToSend = FormatMessage(mailMessage);
42+
43+
if (_mailSettings.SendMode == MailSendMode.LocalDrop)
44+
{
45+
_debugMailDispatchSession.Add(mailMessage);
46+
return;
47+
}
48+
4349
_mailQueue.Enqueue(messageToSend);
4450
}
4551

4652
public async Task FlushAsync()
4753
{
48-
if (_debugMailDispatchSession != null)
54+
if (_mailSettings.SendMode == MailSendMode.LocalDrop)
4955
{
5056
await _debugMailDispatchSession.FlushAsync();
5157
return;
@@ -63,10 +69,7 @@ public async Task FlushAsync()
6369

6470
public void Dispose()
6571
{
66-
if (_debugMailDispatchSession != null)
67-
{
68-
_debugMailDispatchSession.Dispose();
69-
}
72+
_debugMailDispatchSession?.Dispose();
7073
}
7174

7275
private SendGridMessage FormatMessage(MailMessage message)
@@ -112,7 +115,7 @@ private EmailAddress GetMailToAddress(MailMessage message)
112115

113116
private EmailAddress CreateMailAddress(string email, string displayName)
114117
{
115-
// In other libraries we catch validation exceptions here, but SendGrid does not throw any so it is ommited
118+
// In other libraries we catch validation exceptions here, but SendGrid does not throw any so it is omitted
116119
if (string.IsNullOrEmpty(displayName))
117120
{
118121
return new EmailAddress(email);
@@ -134,4 +137,4 @@ private void SetMessageBody(SendGridMessage message, string bodyHtml, string bod
134137
message.PlainTextContent = bodyText;
135138
}
136139
}
137-
}
140+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using Cofoundry.Core.Configuration;
2-
using System;
3-
using System.Collections.Generic;
42
using System.ComponentModel.DataAnnotations;
5-
using System.Text;
63

74
namespace Cofoundry.Plugins.Mail.SendGrid
85
{
@@ -20,4 +17,4 @@ public class SendGridSettings : PluginConfigurationSettingsBase
2017
[Required]
2118
public string ApiKey { get; set; }
2219
}
23-
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Cofoundry.Core.Mail;
2+
3+
namespace SendGridExample
4+
{
5+
/// <summary>
6+
/// Each email consists of a template class and a set of
7+
/// template files. The template class is used as the view
8+
/// model and so you can include any custom propeties that
9+
/// want to make available in the template views.
10+
/// </summary>
11+
public class ExampleMailTemplate : IMailTemplate
12+
{
13+
/// <summary>
14+
/// Full path to the view file. This should not include the type part
15+
/// or file extension (i.e. '_html.cshml' or '_text.cshml') because this
16+
/// is automatically added.
17+
///
18+
/// You can place your templates wherever you like e.g. in the Cofoudry
19+
/// folder as we do here, or in the Views folder.
20+
/// </summary>
21+
public string ViewFile => "~/Cofoundry/MailTemplates/ExampleMailTemplate";
22+
23+
/// <summary>
24+
/// Used as the subject line in the email.
25+
/// </summary>
26+
public string Subject => "Example Mail";
27+
28+
/// <summary>
29+
/// Here we add the contact request as a custom property so we can
30+
/// write it out in our view.
31+
/// </summary>
32+
public string Message { get; set; }
33+
}
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@model SendGridExample.ExampleMailTemplate
2+
@*
3+
You can define either an html or plain text template, or both.
4+
The html template should have "_html" appended to the file name.
5+
*@
6+
@{
7+
Layout = null;
8+
}
9+
<h1>@Model.Subject</h1>
10+
<p>
11+
Message:
12+
</p>
13+
<p>
14+
@Model.Message
15+
</p>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@model SendGridExample.ExampleMailTemplate
2+
@*
3+
You can define either an html or plain text template, or both.
4+
The plain text template should have "_text" appended to the file name.
5+
If both templates are defined then a multi-part email is sent, where the
6+
plain text template is displayed when HTML is not permitted.
7+
*@
8+
@{
9+
Layout = null;
10+
}
11+
@Model.Subject
12+
13+
Message:
14+
15+
-------------------------------------------------
16+
@Model.Message
17+
--------------------------------------------------
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@page
2+
@model IndexModel
3+
4+
<div class="row justify-content-center">
5+
<form method="post" class="col col-8">
6+
<h1 class="mb-4">Send a message</h1>
7+
8+
@if (Model.Result != null)
9+
{
10+
<div class="alert alert-success" role="alert">
11+
@Model.Result
12+
</div>
13+
}
14+
15+
<div class="mb-3">
16+
<label asp-for="EmailAddress" class="form-label"></label>
17+
<input asp-for="EmailAddress" class="form-control">
18+
<span asp-validation-for="EmailAddress" class="text-danger"></span>
19+
</div>
20+
<div class="mb-3">
21+
<label asp-for="DisplayName" class="form-label"></label>
22+
<input asp-for="DisplayName" class="form-control">
23+
<span asp-validation-for="DisplayName" class="text-danger"></span>
24+
</div>
25+
<div class="mb-3">
26+
<label asp-for="Message" class="form-label"></label>
27+
<textarea asp-for="Message" class="form-control"></textarea>
28+
<span asp-validation-for="EmailAddress" class="text-danger"></span>
29+
</div>
30+
31+
<div asp-validation-summary="ModelOnly" class="mb-3 text-danger"></div>
32+
33+
<button type="submit" class="btn btn-primary">Send</button>
34+
</form>
35+
</div>

0 commit comments

Comments
 (0)