Vijayanath Viswanathan
Vijayanath Viswanathan

Reputation: 8541

SendGrid mailer with Azure Function V2

I had been going through below piece of code for SendGrid mailer in Azure functions,

[FunctionName("SendEmail")]
public static async void Run(
[ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] Message 
email,
[SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] 
IAsyncCollector<SendGridMessage> messageCollector)
{
var emailObject = JsonConvert.DeserializeObject<OutgoingEmail> 
 (Encoding.UTF8.GetString(email.Body));

var message = new SendGridMessage();
message.AddTo(emailObject.To);
message.AddContent("text/html", emailObject.Body);
 message.SetFrom(new EmailAddress(emailObject.From));
 message.SetSubject(emailObject.Subject);

 await messageCollector.AddAsync(message);
}

public class OutgoingEmail
{
public string To { get; set; }
public string From { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}

If my understanding is correct this will send a message to the message queue "myqueue" but I am confused that do I need to write a custom listener to the queue "myqueue" to get the message from queue and write a logic to call "client.SendEmailAsync" to send the mail or SendGrid bindng have some magic behind to automatically pickup the message and trigger mail?

Upvotes: 0

Views: 1057

Answers (1)

Murray Foxcroft
Murray Foxcroft

Reputation: 13745

Your understanding is not quite correct.

This will LISTEN for a message on the queue.

And then call the SendGrid API to send the message passed in. This function expects a JSON serialsed message as input from the queue:

[ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] Message email

Upvotes: 1

Related Questions