Skip to content

Commit

Permalink
Merge pull request #15 from IowaComputerGurus/feature/reply-to
Browse files Browse the repository at this point in the history
Implemented initial support for ReplyTo address building.
  • Loading branch information
mitchelsellers authored Aug 30, 2022
2 parents de0ca9f + 8c55d50 commit 0caf092
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
77 changes: 76 additions & 1 deletion src/NetCore.Utilities.Email.SendGrid.Tests/SmtpServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Options;
using Moq;
using SendGrid.Helpers.Mail;
using Xunit;

namespace ICG.NetCore.Utilities.Email.SendGrid.Tests
Expand Down Expand Up @@ -187,5 +189,78 @@ public void SendMessageWithAttachment_ShouldPassOptionalTemplateName_ToMessageMe

//Assets
}

[Fact]
public void SendWithReplyTo_ShouldThrowArgumentException_WhenReplyToMissing()
{
//Arrange
var to = "tester@test.com";
var subject = "test";
var message = "message";

//Act/Assert
Assert.Throws<ArgumentNullException>(() => _service.SendWithReplyTo("", "", to, subject, message));
}

[Fact]
public void SendWithReplyTo_WithoutCCRecipients_ShouldSend_DefaultingFromAddress()
{
//Arrange
var replyTo = "sender@sendy.com";
var to = "tester@test.com";
var subject = "test";
var message = "message";
var returnMessage = new SendGridMessage();
_sendGridMessageBuilderMock
.Setup(s => s.CreateMessage(_options.AdminEmail, _options.AdminName, to, null, subject, message,
"")).Returns(returnMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, "", to, subject, message);

//Verify
}

[Fact]
public void SendWithReplyTo_WithCCRecipients_ShouldSend_DefaultingFromAddress()
{
//Arrange
var replyTo = "sender@sendy.com";
var to = "tester@test.com";
var cc = new List<string> { "Person1@test.com" };
var subject = "test";
var message = "message";
var returnMessage = new SendGridMessage();
_sendGridMessageBuilderMock
.Setup(s => s.CreateMessage(_options.AdminEmail, _options.AdminName, to, cc, subject, message, "")).Returns(returnMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, "", to, cc, subject, message);

//Verify
_sendGridMessageBuilderMock.Verify();
}

[Fact]
public void SendWithReplyTo_ShouldPassOptionalTemplateName_ToMessageMethods()
{
//Arrange
var replyTo = "sender@sendy.com";
var to = "tester@test.com";
var cc = new List<string> { "Person1@test.com" };
var subject = "test";
var message = "message";
var requestedTemplate = "Test";
var returnMessage = new SendGridMessage();
_sendGridMessageBuilderMock
.Setup(s => s.CreateMessage(_options.AdminEmail, _options.AdminName, to, cc, subject, message,
requestedTemplate)).Returns(returnMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, "", to, cc, subject, message, null, requestedTemplate);

//Assets
_sendGridMessageBuilderMock.Verify();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="icg.netcore.utilities.email" Version="6.0.0" />
<PackageReference Include="icg.netcore.utilities.email" Version="6.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
Expand All @@ -44,7 +44,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SendGrid" Version="9.28.0" />
<PackageReference Include="SendGrid" Version="9.28.1" />
</ItemGroup>

</Project>
68 changes: 67 additions & 1 deletion src/NetCore.Utilities.Email.SendGrid/SendGridService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net.Mail;
using Microsoft.Extensions.Options;
using SendGrid.Helpers.Mail;

namespace ICG.NetCore.Utilities.Email.SendGrid
{
Expand Down Expand Up @@ -94,6 +97,69 @@ public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, str
return _sender.SendMessage(apiKey, toSend).GetAwaiter().GetResult();
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, string subject, string bodyHtml)
{
//Call full overload
return SendWithReplyTo(replyToAddress, replyToName, toAddress, null, subject, bodyHtml);
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens)
{
return SendWithReplyTo(replyToAddress, replyToName, toAddress, null, subject, bodyHtml, null, "");
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml)
{
return SendWithReplyTo(replyToAddress, replyToName, toAddress, ccAddressList, subject, bodyHtml, null, "");
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens)
{
return SendWithReplyTo(replyToAddress, replyToName, toAddress, ccAddressList, subject, bodyHtml, tokens, "");
}


/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml,
List<KeyValuePair<string, string>> tokens,
string templateName, string senderKeyName = "")
{
if (string.IsNullOrEmpty(replyToAddress))
throw new ArgumentNullException(nameof(replyToAddress));

if (tokens != null)
{
foreach (var item in tokens)
{
bodyHtml = bodyHtml.Replace(item.Key, item.Value);
}
}

//Get the message to send
var toSend = _messageBuilder.CreateMessage(_serviceOptions.AdminEmail, _serviceOptions.AdminName, toAddress, ccAddressList, subject,
bodyHtml, templateName);

if (!string.IsNullOrEmpty(replyToAddress))
{
var replyTo = new EmailAddress(replyToAddress);
if (!string.IsNullOrEmpty(replyToName))
replyTo.Name = replyToName;
toSend.ReplyTo = replyTo;
}

//Determine the key to use
var apiKey = _serviceOptions.SendGridApiKey;
if (!string.IsNullOrEmpty(senderKeyName))
apiKey = _serviceOptions.AdditionalApiKeys[senderKeyName];

//Send
return _sender.SendMessage(apiKey, toSend).GetAwaiter().GetResult();
}

/// <inheritdoc />
public bool SendMessageWithAttachment(string toAddress, IEnumerable<string> ccAddressList, string subject,
byte[] fileContent, string fileName, string bodyHtml, List<KeyValuePair<string, string>> tokens, string templateName = "", string senderKeyName = "")
Expand Down

0 comments on commit 0caf092

Please sign in to comment.