@@ -22,23 +22,23 @@ public interface ISendGridService
2222 /// </summary>
2323 /// <param name="subject">The message subject</param>
2424 /// <param name="bodyHtml">The message body</param>
25- void SendMessageToAdministrator ( string subject , string bodyHtml ) ;
25+ bool SendMessageToAdministrator ( string subject , string bodyHtml ) ;
2626
2727 /// <summary>
2828 /// Sends a message to the administrator as well as the additional contacts provided.
2929 /// </summary>
3030 /// <param name="ccAddressList">Additional email addresses to add to the CC line</param>
3131 /// <param name="subject">The email subject</param>
3232 /// <param name="bodyHtml">The HTML content of the email</param>
33- void SendMessageToAdministrator ( IEnumerable < string > ccAddressList , string subject , string bodyHtml ) ;
33+ bool SendMessageToAdministrator ( IEnumerable < string > ccAddressList , string subject , string bodyHtml ) ;
3434
3535 /// <summary>
3636 /// Sends a message to the specified recipient, with the supplied subject and body
3737 /// </summary>
3838 /// <param name="toAddress">Who is receiving the email</param>
3939 /// <param name="subject">The message subject</param>
4040 /// <param name="bodyHtml">The message body</param>
41- void SendMessage ( string toAddress , string subject , string bodyHtml ) ;
41+ bool SendMessage ( string toAddress , string subject , string bodyHtml ) ;
4242
4343 /// <summary>
4444 /// Sends a message to the specified recipient, and CC's with the supplied subject and body
@@ -48,8 +48,8 @@ public interface ISendGridService
4848 /// <param name="subject">The message subject</param>
4949 /// <param name="bodyHtml">The message body</param>
5050 /// <param name="templateName">The optional custom template to override with</param>
51- /// <param name="customKey ">The custom key for API usage if needed</param>
52- void SendMessage ( string toAddress , IEnumerable < string > ccAddressList , string subject , string bodyHtml , string templateName = "" , string customKey = "" ) ;
51+ /// <param name="senderKeyName ">The custom key for API usage if needed</param>
52+ bool SendMessage ( string toAddress , IEnumerable < string > ccAddressList , string subject , string bodyHtml , string templateName = "" , string senderKeyName = "" ) ;
5353
5454 /// <summary>
5555 /// Creates a message with an attachment
@@ -61,17 +61,18 @@ public interface ISendGridService
6161 /// <param name="fileName">Attachment file name</param>
6262 /// <param name="bodyHtml">The HTML body contents</param>
6363 /// <param name="templateName">The optional custom template to override with</param>
64- /// <param name="customKey ">The custom key for API usage if needed</param>
64+ /// <param name="senderKeyName ">The custom key for API usage if needed</param>
6565 /// <returns></returns>
66- void SendMessageWithAttachment ( string toAddress , IEnumerable < string > ccAddressList , string subject ,
67- byte [ ] fileContent , string fileName , string bodyHtml , string templateName = "" , string customKey = "" ) ;
66+ bool SendMessageWithAttachment ( string toAddress , IEnumerable < string > ccAddressList , string subject ,
67+ byte [ ] fileContent , string fileName , string bodyHtml , string templateName = "" , string senderKeyName = "" ) ;
6868 }
6969
7070 /// <inheritdoc />
7171 public class SendGridService : ISendGridService
7272 {
7373 private readonly SendGridServiceOptions _serviceOptions ;
7474 private readonly ISendGridMessageBuilder _messageBuilder ;
75+ private readonly ISendGridSender _sender ;
7576
7677 /// <inheritdoc />
7778 public string AdminEmail => _serviceOptions ? . AdminEmail ;
@@ -80,54 +81,64 @@ public class SendGridService : ISendGridService
8081 /// DI Capable Constructor for SendGrid message delivery using MimeKit/MailKit
8182 /// </summary>
8283 /// <param name="serviceOptions"></param>
83- public SendGridService ( IOptions < SendGridServiceOptions > serviceOptions , ISendGridMessageBuilder messageBuilder )
84+ /// <param name="messageBuilder"></param>
85+ public SendGridService ( IOptions < SendGridServiceOptions > serviceOptions , ISendGridMessageBuilder messageBuilder , ISendGridSender sender )
8486 {
8587 _messageBuilder = messageBuilder ;
88+ _sender = sender ;
8689 _serviceOptions = serviceOptions . Value ;
8790 }
8891
8992 /// <inheritdoc />
90- public void SendMessageToAdministrator ( string subject , string bodyHtml )
93+ public bool SendMessageToAdministrator ( string subject , string bodyHtml )
9194 {
9295 //Force to address
93- SendMessage ( _serviceOptions . AdminEmail , null , subject , bodyHtml ) ;
96+ return SendMessage ( _serviceOptions . AdminEmail , null , subject , bodyHtml ) ;
9497 }
9598
9699 /// <inheritdoc />
97- public void SendMessageToAdministrator ( IEnumerable < string > ccAddressList , string subject , string bodyHtml )
100+ public bool SendMessageToAdministrator ( IEnumerable < string > ccAddressList , string subject , string bodyHtml )
98101 {
99- SendMessage ( _serviceOptions . AdminEmail , ccAddressList , subject , bodyHtml ) ;
102+ return SendMessage ( _serviceOptions . AdminEmail , ccAddressList , subject , bodyHtml ) ;
100103 }
101104
102105 /// <inheritdoc />
103- public void SendMessage ( string toAddress , string subject , string bodyHtml )
106+ public bool SendMessage ( string toAddress , string subject , string bodyHtml )
104107 {
105108 //Call full overload
106- SendMessage ( toAddress , null , subject , bodyHtml ) ;
109+ return SendMessage ( toAddress , null , subject , bodyHtml ) ;
107110 }
108111
109112 /// <inheritdoc />
110- public void SendMessage ( string toAddress , IEnumerable < string > ccAddressList , string subject , string bodyHtml , string templateName = "" , string customKey = "" )
113+ public bool SendMessage ( string toAddress , IEnumerable < string > ccAddressList , string subject , string bodyHtml , string templateName = "" , string senderKeyName = "" )
111114 {
112115 //Get the message to send
113116 var toSend = _messageBuilder . CreateMessage ( _serviceOptions . AdminEmail , toAddress , ccAddressList , subject ,
114117 bodyHtml , templateName ) ;
115118
116-
119+ //Determine the key to use
120+ var apiKey = _serviceOptions . SendGridApiKey ;
121+ if ( ! string . IsNullOrEmpty ( senderKeyName ) )
122+ apiKey = _serviceOptions . AdditionalApiKeys [ senderKeyName ] ;
117123
118- //// Send
119- //_mimeKitService.SendEmail( toSend);
124+ //Send
125+ return _sender . SendMessage ( apiKey , toSend ) . GetAwaiter ( ) . GetResult ( ) ;
120126 }
121127
122128 /// <inheritdoc />
123- public void SendMessageWithAttachment ( string toAddress , IEnumerable < string > ccAddressList , string subject , byte [ ] fileContent , string fileName , string bodyHtml , string templateName = "" , string customKey = "" )
129+ public bool SendMessageWithAttachment ( string toAddress , IEnumerable < string > ccAddressList , string subject , byte [ ] fileContent , string fileName , string bodyHtml , string templateName = "" , string senderKeyName = "" )
124130 {
125131 //Get the message to send
126132 var toSend = _messageBuilder . CreateMessageWithAttachment ( _serviceOptions . AdminEmail , toAddress ,
127133 ccAddressList , fileContent , fileName , subject , bodyHtml , templateName ) ;
128134
129- ////Send
130- //_mimeKitService.SendEmail(toSend);
135+ //Determine the key to use
136+ var apiKey = _serviceOptions . SendGridApiKey ;
137+ if ( ! string . IsNullOrEmpty ( senderKeyName ) )
138+ apiKey = _serviceOptions . AdditionalApiKeys [ senderKeyName ] ;
139+
140+ //Send
141+ return _sender . SendMessage ( apiKey , toSend ) . GetAwaiter ( ) . GetResult ( ) ;
131142 }
132143 }
133144}
0 commit comments