Skip to content

Commit 0caf092

Browse files
Merge pull request #15 from IowaComputerGurus/feature/reply-to
Implemented initial support for ReplyTo address building.
2 parents de0ca9f + 8c55d50 commit 0caf092

File tree

4 files changed

+149
-8
lines changed

4 files changed

+149
-8
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
1515
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
1616
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
17-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
18-
<PackageReference Include="Moq" Version="4.16.1" />
19-
<PackageReference Include="xunit" Version="2.4.1" />
20-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
17+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
18+
<PackageReference Include="Moq" Version="4.18.2" />
19+
<PackageReference Include="xunit" Version="2.4.2" />
20+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
2121
<PrivateAssets>all</PrivateAssets>
2222
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2323
</PackageReference>

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using Microsoft.Extensions.Options;
45
using Moq;
6+
using SendGrid.Helpers.Mail;
57
using Xunit;
68

79
namespace ICG.NetCore.Utilities.Email.SendGrid.Tests
@@ -187,5 +189,78 @@ public void SendMessageWithAttachment_ShouldPassOptionalTemplateName_ToMessageMe
187189

188190
//Assets
189191
}
192+
193+
[Fact]
194+
public void SendWithReplyTo_ShouldThrowArgumentException_WhenReplyToMissing()
195+
{
196+
//Arrange
197+
var to = "[email protected]";
198+
var subject = "test";
199+
var message = "message";
200+
201+
//Act/Assert
202+
Assert.Throws<ArgumentNullException>(() => _service.SendWithReplyTo("", "", to, subject, message));
203+
}
204+
205+
[Fact]
206+
public void SendWithReplyTo_WithoutCCRecipients_ShouldSend_DefaultingFromAddress()
207+
{
208+
//Arrange
209+
var replyTo = "[email protected]";
210+
var to = "[email protected]";
211+
var subject = "test";
212+
var message = "message";
213+
var returnMessage = new SendGridMessage();
214+
_sendGridMessageBuilderMock
215+
.Setup(s => s.CreateMessage(_options.AdminEmail, _options.AdminName, to, null, subject, message,
216+
"")).Returns(returnMessage).Verifiable();
217+
218+
//Act
219+
_service.SendWithReplyTo(replyTo, "", to, subject, message);
220+
221+
//Verify
222+
}
223+
224+
[Fact]
225+
public void SendWithReplyTo_WithCCRecipients_ShouldSend_DefaultingFromAddress()
226+
{
227+
//Arrange
228+
var replyTo = "[email protected]";
229+
var to = "[email protected]";
230+
var cc = new List<string> { "[email protected]" };
231+
var subject = "test";
232+
var message = "message";
233+
var returnMessage = new SendGridMessage();
234+
_sendGridMessageBuilderMock
235+
.Setup(s => s.CreateMessage(_options.AdminEmail, _options.AdminName, to, cc, subject, message, "")).Returns(returnMessage).Verifiable();
236+
237+
//Act
238+
_service.SendWithReplyTo(replyTo, "", to, cc, subject, message);
239+
240+
//Verify
241+
_sendGridMessageBuilderMock.Verify();
242+
}
243+
244+
[Fact]
245+
public void SendWithReplyTo_ShouldPassOptionalTemplateName_ToMessageMethods()
246+
{
247+
//Arrange
248+
var replyTo = "[email protected]";
249+
var to = "[email protected]";
250+
var cc = new List<string> { "[email protected]" };
251+
var subject = "test";
252+
var message = "message";
253+
var requestedTemplate = "Test";
254+
var returnMessage = new SendGridMessage();
255+
_sendGridMessageBuilderMock
256+
.Setup(s => s.CreateMessage(_options.AdminEmail, _options.AdminName, to, cc, subject, message,
257+
requestedTemplate)).Returns(returnMessage).Verifiable();
258+
259+
//Act
260+
_service.SendWithReplyTo(replyTo, "", to, cc, subject, message, null, requestedTemplate);
261+
262+
//Assets
263+
_sendGridMessageBuilderMock.Verify();
264+
}
190265
}
191266
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</ItemGroup>
3535

3636
<ItemGroup>
37-
<PackageReference Include="icg.netcore.utilities.email" Version="6.0.0" />
37+
<PackageReference Include="icg.netcore.utilities.email" Version="6.1.0" />
3838
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
3939
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
4040
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
@@ -44,7 +44,7 @@
4444
<PrivateAssets>all</PrivateAssets>
4545
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4646
</PackageReference>
47-
<PackageReference Include="SendGrid" Version="9.28.0" />
47+
<PackageReference Include="SendGrid" Version="9.28.1" />
4848
</ItemGroup>
4949

5050
</Project>

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Mail;
24
using Microsoft.Extensions.Options;
5+
using SendGrid.Helpers.Mail;
36

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

100+
/// <inheritdoc />
101+
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, string subject, string bodyHtml)
102+
{
103+
//Call full overload
104+
return SendWithReplyTo(replyToAddress, replyToName, toAddress, null, subject, bodyHtml);
105+
}
106+
107+
/// <inheritdoc />
108+
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens)
109+
{
110+
return SendWithReplyTo(replyToAddress, replyToName, toAddress, null, subject, bodyHtml, null, "");
111+
}
112+
113+
/// <inheritdoc />
114+
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml)
115+
{
116+
return SendWithReplyTo(replyToAddress, replyToName, toAddress, ccAddressList, subject, bodyHtml, null, "");
117+
}
118+
119+
/// <inheritdoc />
120+
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens)
121+
{
122+
return SendWithReplyTo(replyToAddress, replyToName, toAddress, ccAddressList, subject, bodyHtml, tokens, "");
123+
}
124+
125+
126+
/// <inheritdoc />
127+
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml,
128+
List<KeyValuePair<string, string>> tokens,
129+
string templateName, string senderKeyName = "")
130+
{
131+
if (string.IsNullOrEmpty(replyToAddress))
132+
throw new ArgumentNullException(nameof(replyToAddress));
133+
134+
if (tokens != null)
135+
{
136+
foreach (var item in tokens)
137+
{
138+
bodyHtml = bodyHtml.Replace(item.Key, item.Value);
139+
}
140+
}
141+
142+
//Get the message to send
143+
var toSend = _messageBuilder.CreateMessage(_serviceOptions.AdminEmail, _serviceOptions.AdminName, toAddress, ccAddressList, subject,
144+
bodyHtml, templateName);
145+
146+
if (!string.IsNullOrEmpty(replyToAddress))
147+
{
148+
var replyTo = new EmailAddress(replyToAddress);
149+
if (!string.IsNullOrEmpty(replyToName))
150+
replyTo.Name = replyToName;
151+
toSend.ReplyTo = replyTo;
152+
}
153+
154+
//Determine the key to use
155+
var apiKey = _serviceOptions.SendGridApiKey;
156+
if (!string.IsNullOrEmpty(senderKeyName))
157+
apiKey = _serviceOptions.AdditionalApiKeys[senderKeyName];
158+
159+
//Send
160+
return _sender.SendMessage(apiKey, toSend).GetAwaiter().GetResult();
161+
}
162+
97163
/// <inheritdoc />
98164
public bool SendMessageWithAttachment(string toAddress, IEnumerable<string> ccAddressList, string subject,
99165
byte[] fileContent, string fileName, string bodyHtml, List<KeyValuePair<string, string>> tokens, string templateName = "", string senderKeyName = "")

0 commit comments

Comments
 (0)