Skip to content

Commit 62f69fe

Browse files
Resolves #4 by adding the proper support for the common library
1 parent 0549d27 commit 62f69fe

File tree

5 files changed

+43
-81
lines changed

5 files changed

+43
-81
lines changed

README.md

Lines changed: 2 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

@@ -75,7 +75,7 @@ Additionally you must specify the needed configuration elements within your AppS
7575

7676
### Usage
7777

78-
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.
7979

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class SendGridServiceTests
1919

2020
private readonly Mock<ISendGridMessageBuilder> _sendGridMessageBuilderMock;
2121
private readonly Mock<ISendGridSender> _sendGridSenderMock;
22-
private readonly ISendGridService _service;
22+
private readonly IEmailService _service;
2323

2424
public SendGridServiceTests()
2525
{
@@ -149,7 +149,7 @@ public void SendMessageWithAttachment_ShouldSend_DefaultingFromAddress()
149149
var message = "message";
150150

151151
//Act
152-
_service.SendMessageWithAttachment(to, cc, subject, fileContent, fileName, message);
152+
_service.SendMessageWithAttachment(to, cc, subject, fileContent, fileName, message, null);
153153

154154
//Assets
155155
}
@@ -165,7 +165,7 @@ public void SendMessage_ShouldPassOptionalTemplateName_ToMessageMethods()
165165
var requestedTemplate = "Test";
166166

167167
//Act
168-
_service.SendMessage(to, cc, subject, message, requestedTemplate);
168+
_service.SendMessage(to, cc, subject, message, null, requestedTemplate);
169169

170170
//Assets
171171
}
@@ -183,7 +183,7 @@ public void SendMessageWithAttachment_ShouldPassOptionalTemplateName_ToMessageMe
183183
var requestedTemplate = "Test";
184184

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

188188
//Assets
189189
}

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/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/SendGridService.cs

Lines changed: 33 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,8 @@
33

44
namespace ICG.NetCore.Utilities.Email.SendGrid
55
{
6-
/// <summary>
7-
/// Represents an SendGrid service that can be used to send outbound email messages. Internally the current concrete
8-
/// implementation will utilize the MailKit/MimeKit services per the recommendation of Microsoft.
9-
/// </summary>
10-
/// <remarks>
11-
/// In your project you should use this service only.
12-
/// </remarks>
13-
public interface ISendGridService
14-
{
15-
/// <summary>
16-
/// Returns the configured administrator email for the SendGrid service
17-
/// </summary>
18-
string AdminEmail { get; }
19-
20-
/// <summary>
21-
/// Returns the configured administrator name for the SendGrid service
22-
/// </summary>
23-
string AdminName { get; }
24-
25-
/// <summary>
26-
/// Shortcut for sending an email to the administrator, only requiring the subject and body.
27-
/// </summary>
28-
/// <param name="subject">The message subject</param>
29-
/// <param name="bodyHtml">The message body</param>
30-
bool SendMessageToAdministrator(string subject, string bodyHtml);
31-
32-
/// <summary>
33-
/// Sends a message to the administrator as well as the additional contacts provided.
34-
/// </summary>
35-
/// <param name="ccAddressList">Additional email addresses to add to the CC line</param>
36-
/// <param name="subject">The email subject</param>
37-
/// <param name="bodyHtml">The HTML content of the email</param>
38-
bool SendMessageToAdministrator(IEnumerable<string> ccAddressList, string subject, string bodyHtml);
39-
40-
/// <summary>
41-
/// Sends a message to the specified recipient, with the supplied subject and body
42-
/// </summary>
43-
/// <param name="toAddress">Who is receiving the email</param>
44-
/// <param name="subject">The message subject</param>
45-
/// <param name="bodyHtml">The message body</param>
46-
bool SendMessage(string toAddress, string subject, string bodyHtml);
47-
48-
/// <summary>
49-
/// Sends a message to the specified recipient, and CC's with the supplied subject and body
50-
/// </summary>
51-
/// <param name="toAddress">Who is receiving the email</param>
52-
/// <param name="ccAddressList">Additional CC'ed emails</param>
53-
/// <param name="subject">The message subject</param>
54-
/// <param name="bodyHtml">The message body</param>
55-
/// <param name="templateName">The optional custom template to override with</param>
56-
/// <param name="senderKeyName">The custom key for API usage if needed</param>
57-
bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml, string templateName = "", string senderKeyName = "");
58-
59-
/// <summary>
60-
/// Creates a message with an attachment
61-
/// </summary>
62-
/// <param name="toAddress">The to address for the message</param>
63-
/// <param name="ccAddressList">The address(ses) to add a CC's</param>
64-
/// <param name="subject">The subject of the message</param>
65-
/// <param name="fileContent">Attachment Content</param>
66-
/// <param name="fileName">Attachment file name</param>
67-
/// <param name="bodyHtml">The HTML body contents</param>
68-
/// <param name="templateName">The optional custom template to override with</param>
69-
/// <param name="senderKeyName">The custom key for API usage if needed</param>
70-
/// <returns></returns>
71-
bool SendMessageWithAttachment(string toAddress, IEnumerable<string> ccAddressList, string subject,
72-
byte[] fileContent, string fileName, string bodyHtml, string templateName = "", string senderKeyName = "");
73-
}
74-
756
/// <inheritdoc />
76-
public class SendGridService : ISendGridService
7+
public class SendGridService : IEmailService
778
{
789
private readonly SendGridServiceOptions _serviceOptions;
7910
private readonly ISendGridMessageBuilder _messageBuilder;
@@ -119,8 +50,37 @@ public bool SendMessage(string toAddress, string subject, string bodyHtml)
11950
}
12051

12152
/// <inheritdoc />
122-
public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml, string templateName = "", string senderKeyName = "")
53+
public bool SendMessage(string toAddress, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens)
54+
{
55+
return SendMessage(toAddress, null, subject, bodyHtml, null, "");
56+
}
57+
58+
/// <inheritdoc />
59+
public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml)
60+
{
61+
return SendMessage(toAddress, ccAddressList, subject, bodyHtml, null, "");
62+
}
63+
64+
/// <inheritdoc />
65+
public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens)
12366
{
67+
return SendMessage(toAddress, ccAddressList, subject, bodyHtml, tokens, "");
68+
}
69+
70+
71+
/// <inheritdoc />
72+
public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml,
73+
List<KeyValuePair<string, string>> tokens,
74+
string templateName, string senderKeyName = "")
75+
{
76+
if (tokens != null)
77+
{
78+
foreach (var item in tokens)
79+
{
80+
bodyHtml = bodyHtml.Replace(item.Key, item.Value);
81+
}
82+
}
83+
12484
//Get the message to send
12585
var toSend = _messageBuilder.CreateMessage(_serviceOptions.AdminEmail, _serviceOptions.AdminName, toAddress, ccAddressList, subject,
12686
bodyHtml, templateName);
@@ -135,7 +95,8 @@ public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, str
13595
}
13696

13797
/// <inheritdoc />
138-
public bool SendMessageWithAttachment(string toAddress, IEnumerable<string> ccAddressList, string subject, byte[] fileContent, string fileName, string bodyHtml, string templateName = "", string senderKeyName = "")
98+
public bool SendMessageWithAttachment(string toAddress, IEnumerable<string> ccAddressList, string subject,
99+
byte[] fileContent, string fileName, string bodyHtml, List<KeyValuePair<string, string>> tokens, string templateName = "", string senderKeyName = "")
139100
{
140101
//Get the message to send
141102
var toSend = _messageBuilder.CreateMessageWithAttachment(_serviceOptions.AdminEmail, _serviceOptions.AdminName, toAddress,

0 commit comments

Comments
 (0)