Skip to content

Commit 551e314

Browse files
Merge pull request #5 from IowaComputerGurus/feature/changes
Creation of initial 2.x release
2 parents 8ebf02b + 62f69fe commit 551e314

File tree

10 files changed

+104
-108
lines changed

10 files changed

+104
-108
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ICG.NetCore.Utilities.Email.SendGrid ![](https://img.shields.io/github/license/iowacomputergurus/netcore.utilities.email.SendGrid.svg)
22
This library provides an easy to use implementation of SendGrid based email delivery. This abstraction with proper interfaces allows email implementation inside of your project with little effort and easy to manage integration, and boasts features such as automatic environment name appending as well as robust email templates.
33

4-
This package depends on the ICG.NetCore.Utilities.Email project for template implementation
4+
This package depends on the ICG.NetCore.Utilities.Email project for template implementation
55

66
## Build Status
77

@@ -48,6 +48,7 @@ Additionally you must specify the needed configuration elements within your AppS
4848
```
4949
"SendGridServiceOptions": {
5050
"AdminEmail": "[email protected]",
51+
"AdminName": "John Smith",
5152
"SendGridApiKey": "YourKey",
5253
"AdditionalApiKeys": { "SpecialSender": "SpecialKey" }
5354
"AlwaysTemplateEmails": true,
@@ -63,6 +64,7 @@ Additionally you must specify the needed configuration elements within your AppS
6364
| Setting | Description |
6465
| --- | --- |
6566
| AdminEmail | This is the email address used as the "from" address and also for any usage of the "SendToAdministrator" option |
67+
| AdminName | If specified this is the name that will be used for the "From" address on all outbound emails |
6668
| SendGridApiKey | The API Key to use for default sending of email addresses |
6769
| AdditionalApiKeys | These are name/value pairs of additional API keys that could be used for sending emails. Totally optional |
6870
| AlwaysTemplateEmails | If selected ALL emails sent will be templated, by default using the "DefaultTemplate" as configured |
@@ -73,7 +75,7 @@ Additionally you must specify the needed configuration elements within your AppS
7375

7476
### Usage
7577

76-
Usage is primarly completed by injecting the ISmtpService interface to your respective project, one injected emails can be sent with a single line of code.
78+
Usage is primarly completed by injecting the IEmailService interface to your respective project, one injected emails can be sent with a single line of code.
7779

7880
```
7981
_sendGridService.SendEmail("[email protected]", "My Subject", "<p>Hello!</p>");

src/NetCore.Utilities.Email.SendGrid.Tests/NetCore.Utilities.Email..SendGrid.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
1111
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
1212
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
14-
<PackageReference Include="Moq" Version="4.16.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
14+
<PackageReference Include="Moq" Version="4.16.1" />
1515
<PackageReference Include="xunit" Version="2.4.1" />
1616
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1717
<PrivateAssets>all</PrivateAssets>

src/NetCore.Utilities.Email.SendGrid.Tests/SmtpServiceTests.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ public class SendGridServiceTests
1111
private readonly SendGridServiceOptions _options = new SendGridServiceOptions()
1212
{
1313
AdminEmail = "[email protected]",
14+
AdminName = "John Smith",
1415
SendGridApiKey = "APIKEY",
1516
AddEnvironmentSuffix = false,
1617
AlwaysTemplateEmails = false
1718
};
1819

1920
private readonly Mock<ISendGridMessageBuilder> _sendGridMessageBuilderMock;
2021
private readonly Mock<ISendGridSender> _sendGridSenderMock;
21-
private readonly ISendGridService _service;
22+
private readonly IEmailService _service;
2223

2324
public SendGridServiceTests()
2425
{
@@ -54,6 +55,32 @@ public void AdminEmail_ShouldReturnNullWhenNoConfiguration()
5455
Assert.Null(result);
5556
}
5657

58+
[Fact]
59+
public void AdminName_ShouldReturnConfigurationName()
60+
{
61+
//Arrange
62+
var expectedName = "John Smith";
63+
64+
//Act
65+
var result = _service.AdminName;
66+
67+
//Assert
68+
Assert.Equal(expectedName, result);
69+
}
70+
71+
[Fact]
72+
public void AdminName_ShouldReturnNullWhenNoConfiguration()
73+
{
74+
//Arrange
75+
var testService = new SendGridService(new OptionsWrapper<SendGridServiceOptions>(null), _sendGridMessageBuilderMock.Object, _sendGridSenderMock.Object);
76+
77+
//Act
78+
var result = testService.AdminName;
79+
80+
//Assert
81+
Assert.Null(result);
82+
}
83+
5784
[Fact]
5885
public void SendToAdministrator_ShouldSend_DefaultingFromAndToAddress()
5986
{
@@ -122,7 +149,7 @@ public void SendMessageWithAttachment_ShouldSend_DefaultingFromAddress()
122149
var message = "message";
123150

124151
//Act
125-
_service.SendMessageWithAttachment(to, cc, subject, fileContent, fileName, message);
152+
_service.SendMessageWithAttachment(to, cc, subject, fileContent, fileName, message, null);
126153

127154
//Assets
128155
}
@@ -138,7 +165,7 @@ public void SendMessage_ShouldPassOptionalTemplateName_ToMessageMethods()
138165
var requestedTemplate = "Test";
139166

140167
//Act
141-
_service.SendMessage(to, cc, subject, message, requestedTemplate);
168+
_service.SendMessage(to, cc, subject, message, null, requestedTemplate);
142169

143170
//Assets
144171
}
@@ -156,7 +183,7 @@ public void SendMessageWithAttachment_ShouldPassOptionalTemplateName_ToMessageMe
156183
var requestedTemplate = "Test";
157184

158185
//Act
159-
_service.SendMessageWithAttachment(to, cc, subject, fileContent, fileName, message, requestedTemplate);
186+
_service.SendMessageWithAttachment(to, cc, subject, fileContent, fileName, message, null, requestedTemplate);
160187

161188
//Assets
162189
}

src/NetCore.Utilities.Email.SendGrid.Tests/StartupExtensiosTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void ServiceCollection_ShouldRegisterSendGridService()
5353
var services = collection.BuildServiceProvider();
5454

5555
//Act
56-
var result = services.GetService<ISendGridService>();
56+
var result = services.GetService<IEmailService>();
5757

5858
//Assert
5959
Assert.NotNull(result);

src/NetCore.Utilities.Email.SendGrid.Tests/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"SendGridServiceOptions": {
33
"AdminEmail": "[email protected]",
4+
"AdminName": "John Smith",
45
"SendGridApiKey": "TestKey",
56
"AdditionalApiKeys": { "SpecialSender": "SpecialKey" },
67
"AlwaysTemplateEmails": true,

src/NetCore.Utilities.Email.SendGrid/DependencyResolution/StartupExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ICG.NetCore.Utilities.Email.SendGrid;
1+
using ICG.NetCore.Utilities.Email;
2+
using ICG.NetCore.Utilities.Email.SendGrid;
23
using Microsoft.Extensions.Configuration;
34

45
namespace Microsoft.Extensions.DependencyInjection
@@ -19,7 +20,7 @@ public static void UseIcgNetCoreUtilitiesEmailSendGrid(this IServiceCollection s
1920
services.UseIcgNetCoreUtilitiesEmail(configuration);
2021

2122
//Bind additional services
22-
services.AddTransient<ISendGridService, SendGridService>();
23+
services.AddTransient<IEmailService, SendGridService>();
2324
services.AddTransient<ISendGridMessageBuilder, SendGridMessageBuilder>();
2425
services.AddTransient<ISendGridSender, SendGridSender>();
2526
services.Configure<SendGridServiceOptions>(configuration.GetSection(nameof(SendGridServiceOptions)));

src/NetCore.Utilities.Email.SendGrid/NetCore.Utilities.Email.SendGrid.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@
3333
</PropertyGroup>
3434

3535
<ItemGroup>
36-
<PackageReference Include="icg.netcore.utilities.email" Version="3.0.4" />
36+
<PackageReference Include="icg.netcore.utilities.email" Version="4.0.4" />
3737
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
38-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
38+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
3939
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
4040
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
4141
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
4242
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
4343
<PrivateAssets>all</PrivateAssets>
4444
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4545
</PackageReference>
46-
<PackageReference Include="SendGrid" Version="9.24.0" />
46+
<PackageReference Include="SendGrid" Version="9.24.3" />
4747
</ItemGroup>
4848

4949
</Project>

src/NetCore.Utilities.Email.SendGrid/SendGridMessageBuilder.cs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,33 @@ namespace ICG.NetCore.Utilities.Email.SendGrid
1313
/// </summary>
1414
public interface ISendGridMessageBuilder
1515
{
16-
/// <summary>
17-
/// Creates a simple message for sending
18-
/// </summary>
19-
/// <param name="from">Who the message is from</param>
20-
/// <param name="to">Who the message is to</param>
21-
/// <param name="subject">The subject of the message</param>
22-
/// <param name="bodyHtml">The Email's HTML content</param>
23-
/// <returns></returns>
24-
SendGridMessage CreateMessage(string from, string to, string subject, string bodyHtml);
25-
2616
/// <summary>
2717
/// Creates a simple message for sending with a custom template
2818
/// </summary>
2919
/// <param name="from">Who the message is from</param>
20+
/// <param name="fromName">The name of the sender</param>
3021
/// <param name="to">Who the message is to</param>
3122
/// <param name="cc">An optional listing of CC addresses</param>
3223
/// <param name="subject">The subject of the message</param>
3324
/// <param name="bodyHtml">The Email's HTML content</param>
3425
/// <param name="templateName">The name of the template to use</param>
3526
/// <returns></returns>
36-
SendGridMessage CreateMessage(string from, string to, IEnumerable<string> cc, string subject, string bodyHtml,
27+
SendGridMessage CreateMessage(string from, string fromName, string to, IEnumerable<string> cc, string subject, string bodyHtml,
3728
string templateName = "");
3829

3930
/// <summary>
4031
/// Creates a simple message for sending with a custom template and attachment
4132
/// </summary>
4233
/// <param name="from">Who the message is from</param>
34+
/// <param name="fromName">The name of the sender</param>
4335
/// <param name="to">Who the message is to</param>
4436
/// <param name="cc">An optional listing of CC addresses</param>
4537
/// <param name="fileContent">The content of the attachment in bytes</param>
4638
/// <param name="fileName">The desired name for the file attachment</param>
4739
/// <param name="subject">The subject of the message</param>
4840
/// <param name="bodyHtml">The Email's HTML content</param>
4941
/// <param name="templateName">The name of the template to use</param>
50-
SendGridMessage CreateMessageWithAttachment(string from, string to, IEnumerable<string> cc,
42+
SendGridMessage CreateMessageWithAttachment(string from, string fromName, string to, IEnumerable<string> cc,
5143
byte[] fileContent, string fileName, string subject, string bodyHtml, string templateName = "");
5244
}
5345

@@ -65,7 +57,7 @@ public class SendGridMessageBuilder : ISendGridMessageBuilder
6557
/// <param name="hostingEnvironment"></param>
6658
/// <param name="emailTemplateFactory"></param>
6759
/// <param name="options"></param>
68-
/// <param name="loggerFactory"></param>
60+
/// <param name="logger"></param>
6961
public SendGridMessageBuilder(IHostingEnvironment hostingEnvironment, IEmailTemplateFactory emailTemplateFactory,
7062
IOptions<SendGridServiceOptions> options, ILogger<SendGridMessageBuilder> logger)
7163
{
@@ -74,15 +66,9 @@ public SendGridMessageBuilder(IHostingEnvironment hostingEnvironment, IEmailTemp
7466
_serviceOptions = options.Value;
7567
_logger = logger;
7668
}
77-
69+
7870
/// <inheritdoc />
79-
public SendGridMessage CreateMessage(string from, string to, string subject, string bodyHtml)
80-
{
81-
return CreateMessage(from, to, null, subject, bodyHtml);
82-
}
83-
84-
/// <inheritdoc />
85-
public SendGridMessage CreateMessage(string from, string to, IEnumerable<string> cc, string subject, string bodyHtml, string templateName = "")
71+
public SendGridMessage CreateMessage(string from, string fromName, string to, IEnumerable<string> cc, string subject, string bodyHtml, string templateName = "")
8672
{
8773
//Validate inputs
8874
if (string.IsNullOrEmpty(from))
@@ -96,6 +82,8 @@ public SendGridMessage CreateMessage(string from, string to, IEnumerable<string>
9682

9783
//Get addresses
9884
var fromAddress = new EmailAddress(from);
85+
if (!string.IsNullOrEmpty(fromName))
86+
fromAddress.Name = fromName;
9987
var recipients = new List<EmailAddress> {new EmailAddress(to)};
10088
if (cc != null)
10189
{
@@ -134,11 +122,12 @@ public SendGridMessage CreateMessage(string from, string to, IEnumerable<string>
134122
bodyHtml);
135123
}
136124

137-
public SendGridMessage CreateMessageWithAttachment(string from, string to, IEnumerable<string> cc,
125+
/// <inheritdoc />
126+
public SendGridMessage CreateMessageWithAttachment(string from, string fromName, string to, IEnumerable<string> cc,
138127
byte[] fileContent, string fileName, string subject, string bodyHtml, string templateName = "")
139128
{
140129
//Build the basic message
141-
var toSend = CreateMessage(from, to, cc, subject, bodyHtml, templateName);
130+
var toSend = CreateMessage(from, fromName, to, cc, subject, bodyHtml, templateName);
142131

143132
//Attach file
144133
toSend.Attachments = new List<Attachment>

0 commit comments

Comments
 (0)