66using System . Text ;
77using System . Threading . Tasks ;
88using Microsoft . Extensions . Logging ;
9+ using SendGrid ;
10+ using SendGrid . Helpers . Mail ;
911
1012namespace MessageSenderService . DurableFunctions . Activities
1113{
1214 public static class SendEmailActivity
1315 {
1416 [ FunctionName ( "SendEmail" ) ]
15- public static string SendEmail ( [ ActivityTrigger ] string serviceBusQueueMessage , ILogger log )
16- {
17-
17+ public static async Task < string > SendEmailAsync ( [ ActivityTrigger ] string serviceBusQueueMessage , ILogger log )
18+ {
1819
1920 try
2021 {
@@ -23,15 +24,18 @@ public static string SendEmail([ActivityTrigger] string serviceBusQueueMessage,
2324 if ( serviceBusQueueMessage != null )
2425 {
2526 log . LogInformation ( $ "Sending email to recipient with queue message: { serviceBusQueueMessage } .") ;
26- var result = SendEmailAsync ( serviceBusQueueMessage ) ;
27+ bool result = await SendEmailAsync ( serviceBusQueueMessage ) ;
28+ if ( result == false )
29+ {
30+
31+ return $ "Email not sent!";
32+ }
33+ else
34+ {
35+ log . LogInformation ( $ "Sending email with message: { serviceBusQueueMessage } ") ;
36+ }
2737 }
28-
29-
30-
31-
32-
33- log . LogInformation ( $ "Sending email with message: { serviceBusQueueMessage } ") ;
34- return $ "Email sent!";
38+ return $ "Email was sent successfully!";
3539 }
3640 catch ( Exception )
3741 {
@@ -41,34 +45,38 @@ public static string SendEmail([ActivityTrigger] string serviceBusQueueMessage,
4145
4246 }
4347
44- private async Task < bool > SendEmailAsync ( string serviceBusQueueMessage )
45- {
46- var apiKey = Environment . GetEnvironmentVariable ( "SendGrid_API_KEY" ) ;
47- var adminEmailAddress = Environment . GetEnvironmentVariable ( "AdminEmailAddress" ) ;
48- var recipientEmailAddress = Environment . GetEnvironmentVariable ( "RecipientEmailAddress" ) ;
49-
50- var client = new SendGridClient ( apiKey ) ;
51- var emailMessage = new SendGridMessage ( )
48+ private static async Task < bool > SendEmailAsync ( string serviceBusQueueMessage )
49+ {
50+ try
5251 {
53- From = new EmailAddress ( adminEmailAddress , "Jonah Andersson" ) ,
54- Subject = "Email from Service Bus Receive Function for Azure Back to School 2022!" ,
55- PlainTextContent = myQueueMessageItem
56- } ;
52+ var apiKey = Environment . GetEnvironmentVariable ( "SendGrid_API_KEY" ) ;
53+ var adminEmailAddress = Environment . GetEnvironmentVariable ( "AdminEmailAddress" ) ;
54+ var recipientEmailAddress = Environment . GetEnvironmentVariable ( "RecipientEmailAddress" ) ;
5755
58- emailMessage . AddTo ( new EmailAddress ( recipientEmailAddress , "Jonah at Work Email" ) ) ;
59- var response = await client . SendEmailAsync ( emailMessage ) ;
56+ var client = new SendGridClient ( apiKey ) ;
57+ var emailMessage = new SendGridMessage ( )
58+ {
59+ From = new EmailAddress ( adminEmailAddress , "Jonah Andersson" ) ,
60+ Subject = "Email from Service Bus Receive Function for Azure Back to School 2022!" ,
61+ PlainTextContent = serviceBusQueueMessage
62+ } ;
6063
61- // A success status code means SendGrid received the email request and will process it.
62- // Errors can still occur when SendGrid tries to send the email.
63- // If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
64- Console . WriteLine ( response . IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!" ) ;
65- try
66- {
64+ emailMessage . AddTo ( new EmailAddress ( recipientEmailAddress , "Jonah at Work Email" ) ) ;
65+ var response = await client . SendEmailAsync ( emailMessage ) ;
66+
67+ // A success status code means SendGrid received the email request and will process it.
68+ // Errors can still occur when SendGrid tries to send the email.
69+ // If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
70+ Console . WriteLine ( response . IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!" ) ;
71+
72+ if ( response . IsSuccessStatusCode )
73+ return true ;
74+ else return false ;
6775
6876 }
6977 catch ( Exception )
7078 {
71-
79+ //TODO: More robust error general handling here
7280 throw ;
7381 }
7482 }
0 commit comments