33
44namespace 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