Skip to content

Commit 0549d27

Browse files
Resolves #2 adding new support for optional configuration of Admin Name property.
1 parent ed4c6ef commit 0549d27

File tree

8 files changed

+63
-31
lines changed

8 files changed

+63
-31
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 |

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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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
@@ -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
{

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/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: 10 additions & 22 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

@@ -74,15 +66,9 @@ public SendGridMessageBuilder(IHostingEnvironment hostingEnvironment, IEmailTemp
7466
_serviceOptions = options.Value;
7567
_logger = logger;
7668
}
77-
78-
/// <inheritdoc />
79-
public SendGridMessage CreateMessage(string from, string to, string subject, string bodyHtml)
80-
{
81-
return CreateMessage(from, to, null, subject, bodyHtml);
82-
}
83-
69+
8470
/// <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
{
@@ -135,11 +123,11 @@ public SendGridMessage CreateMessage(string from, string to, IEnumerable<string>
135123
}
136124

137125
/// <inheritdoc />
138-
public SendGridMessage CreateMessageWithAttachment(string from, string to, IEnumerable<string> cc,
126+
public SendGridMessage CreateMessageWithAttachment(string from, string fromName, string to, IEnumerable<string> cc,
139127
byte[] fileContent, string fileName, string subject, string bodyHtml, string templateName = "")
140128
{
141129
//Build the basic message
142-
var toSend = CreateMessage(from, to, cc, subject, bodyHtml, templateName);
130+
var toSend = CreateMessage(from, fromName, to, cc, subject, bodyHtml, templateName);
143131

144132
//Attach file
145133
toSend.Attachments = new List<Attachment>

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public interface ISendGridService
1717
/// </summary>
1818
string AdminEmail { get; }
1919

20+
/// <summary>
21+
/// Returns the configured administrator name for the SendGrid service
22+
/// </summary>
23+
string AdminName { get; }
24+
2025
/// <summary>
2126
/// Shortcut for sending an email to the administrator, only requiring the subject and body.
2227
/// </summary>
@@ -76,6 +81,9 @@ public class SendGridService : ISendGridService
7681

7782
/// <inheritdoc />
7883
public string AdminEmail => _serviceOptions?.AdminEmail;
84+
85+
/// <inheritdoc />
86+
public string AdminName => _serviceOptions?.AdminName;
7987

8088
/// <summary>
8189
/// DI Capable Constructor for SendGrid message delivery using MimeKit/MailKit
@@ -114,7 +122,7 @@ public bool SendMessage(string toAddress, string subject, string bodyHtml)
114122
public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml, string templateName = "", string senderKeyName = "")
115123
{
116124
//Get the message to send
117-
var toSend = _messageBuilder.CreateMessage(_serviceOptions.AdminEmail, toAddress, ccAddressList, subject,
125+
var toSend = _messageBuilder.CreateMessage(_serviceOptions.AdminEmail, _serviceOptions.AdminName, toAddress, ccAddressList, subject,
118126
bodyHtml, templateName);
119127

120128
//Determine the key to use
@@ -130,7 +138,7 @@ public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, str
130138
public bool SendMessageWithAttachment(string toAddress, IEnumerable<string> ccAddressList, string subject, byte[] fileContent, string fileName, string bodyHtml, string templateName = "", string senderKeyName = "")
131139
{
132140
//Get the message to send
133-
var toSend = _messageBuilder.CreateMessageWithAttachment(_serviceOptions.AdminEmail, toAddress,
141+
var toSend = _messageBuilder.CreateMessageWithAttachment(_serviceOptions.AdminEmail, _serviceOptions.AdminName, toAddress,
134142
ccAddressList, fileContent, fileName, subject, bodyHtml, templateName);
135143

136144
//Determine the key to use

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ public class SendGridServiceOptions
1515
public string AdminEmail { get; set; }
1616

1717
/// <summary>
18-
/// The SendGrid API key that needs to
18+
/// An optional name for the administrative user
19+
/// </summary>
20+
[Display(Name = "Admin Name")]
21+
public string AdminName { get; set; }
22+
23+
/// <summary>
24+
/// The SendGrid API key that needs to
1925
/// </summary>
2026
public string SendGridApiKey { get; set; }
2127

2228
/// <summary>
23-
/// Optional additional API Keys for sending outbound emails
29+
/// Optional additional API Keys for sending outbound emails
2430
/// </summary>
2531
public Dictionary<string, string> AdditionalApiKeys { get; set; }
2632

0 commit comments

Comments
 (0)