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 < bool > SendEmail ( [ ActivityTrigger ] string serviceBusQueueMessage , ILogger log )
18+ {
1819
1920 try
2021 {
2122 log . LogInformation ( $ "SendEmailActivity trigged from the durable orchestratoion") ;
2223
23- if ( serviceBusQueueMessage != null )
24- {
25- log . LogInformation ( $ "Sending email to recipient with queue message: { serviceBusQueueMessage } .") ;
26- var result = SendEmailAsync ( serviceBusQueueMessage ) ;
27- }
28-
29-
30-
31-
32-
33- log . LogInformation ( $ "Sending email with message: { serviceBusQueueMessage } ") ;
34- return $ "Email sent!";
24+ log . LogInformation ( $ "Sending email to recipient with queue message: { serviceBusQueueMessage } .") ;
25+ bool result = await SendEmailAsync ( serviceBusQueueMessage ) ;
26+ return result ;
3527 }
3628 catch ( Exception )
3729 {
@@ -41,34 +33,38 @@ public static string SendEmail([ActivityTrigger] string serviceBusQueueMessage,
4133
4234 }
4335
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 ( )
36+ private static async Task < bool > SendEmailAsync ( string serviceBusQueueMessage )
37+ {
38+ try
5239 {
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- } ;
40+ var apiKey = Environment . GetEnvironmentVariable ( "SendGrid_API_KEY" ) ;
41+ var adminEmailAddress = Environment . GetEnvironmentVariable ( "AdminEmailAddress" ) ;
42+ var recipientEmailAddress = Environment . GetEnvironmentVariable ( "RecipientEmailAddress" ) ;
5743
58- emailMessage . AddTo ( new EmailAddress ( recipientEmailAddress , "Jonah at Work Email" ) ) ;
59- var response = await client . SendEmailAsync ( emailMessage ) ;
44+ var client = new SendGridClient ( apiKey ) ;
45+ var emailMessage = new SendGridMessage ( )
46+ {
47+ From = new EmailAddress ( adminEmailAddress , "Jonah Andersson" ) ,
48+ Subject = "Email from Service Bus Receive Function for Azure Back to School 2022!" ,
49+ PlainTextContent = serviceBusQueueMessage
50+ } ;
6051
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- {
52+ emailMessage . AddTo ( new EmailAddress ( recipientEmailAddress , "Jonah at Work Email" ) ) ;
53+ var response = await client . SendEmailAsync ( emailMessage ) ;
54+
55+ // A success status code means SendGrid received the email request and will process it.
56+ // Errors can still occur when SendGrid tries to send the email.
57+ // If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
58+ Console . WriteLine ( response . IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!" ) ;
59+
60+ if ( response . IsSuccessStatusCode )
61+ return true ;
62+ else return false ;
6763
6864 }
6965 catch ( Exception )
7066 {
71-
67+ //TODO: More robust error general handling here
7268 throw ;
7369 }
7470 }
0 commit comments