0
votes

I have developed a C# console application sending emails to our customers when certain conditions occur. Mails are not sent when application is deployed as azure webjob.

When testing from visual studio in my development environment Mails are sent without any problems as intended, but when the application is deployed and run as an azure app-service webjob no mails are sent. The application is otherwise working as intended also when deployed as azure webjob.

public enum MailType : short { Plain, Html }

private static string MimeType(MailType type) 
{ return (type == MailType.Html) ? "text/html" : "text/plain";  }

private static async Task sendMail(string to, string name, string subject, MailType mailType, string content)
{
    SendGridMessage msg = new SendGridMessage();
    msg.From = new EmailAddress("[email protected]", "Foo Ltd.");
    msg.AddTo(to, name);
    msg.Subject = subject;
    msg.AddContent(MimeType(mailType), content);
    SendGridClient client = new SendGridClient(SendGridAPIKey);
    await client.SendEmailAsync(msg);
}

public static void SendMail(string to, string name, string subject, MailType mailType, string content)
{
    sendMail(to, name, subject, mailType, content).Wait();
}

Answers to all other questions regarding this issue (a lot of developers have met this problem) focus on the code; especially the async implementation, but it seems this is not the issue. Does azure have some restrictions to the communication between the application deployed as webjob and the sendgrid server? (I am using the sendgrid and sendgrid.helpers.mail)

1
When you publish the webjob to azure, have you set SendGridAPIKey in Configuration in azure webapp? - Joey Cai
Checking the SendGridAPIKey revealed a bug, but fixing it did not solve the problem. Requests from the application when deployed as azure webjob are not logged in the SendGrid activity list (but request from my dev. environment are) - Hans Henrik
Did you ever resolve this and if yes, what was the solution? - Patric
I didn't resolve the SendGrid problem and use MailJet instead - Hans Henrik

1 Answers

0
votes

I test with you code and it work well both on local and on Azure. I invoke the SendMail method in a queueTrigger.

public static void ProcessQueueMessage([QueueTrigger("queue1")] string message, TextWriter log)
{
    log.WriteLine(message);
    SendMail("[email protected]", "xxxxx", "hello world", MailType.Plain, "have a good day.");
}

So you could refer to the following ways to troubleshoot it.

1.Check your SendGridAPIKey. If you stored SendGridAPIKey as an environment variable in the project, you need to go to Configuration>Application Settings and add the key in it.

2.Check your SendGrid Activity.The problem could be getting your request to SendGrid, or it could be that SendGrid is getting your request but isn't fulfilling it. Check the Activity Feed and search for the particular email you are expecting