Trigger Custom Mail From Azure Web Job

Recently, I had to create a custom application which had to trigger mails from Azure. I believe, there will be more like me who are having the same requirement. Let’s not re-invent the wheel and instead use the concepts we have, with minimum tweaking.

I will be posting the code with this article and also putting the basic steps to create a package which can be converted to a Web Job in Azure.

Let’s begin.


We are creating a console application and the code is in C# and the program uses System, System.NET, and System.Text dll files.

We will start by creating a function SendMailViaOffice365AndAzure() and then some variables in it. As of now, you do not need to pass any parameters to the function and it doesn’t return anything. Now, keep in mind that Azure has a Console Log where you can see your Console.WriteLines.

Next, we will create the SMTP client object which will be responsible for sending our mail.

As we are using Azure / Office 365, the Host will be smtp.office365.com, the Port will be 587, the SSL will be enabled, and the Delivery Method will be Default network.

The "From" Mail Id must be valid and have a Mail Box enabled, which will be verified by Office 365 to trigger the mail. The credentials of this id will be used. The rest of the code is self-explanatory.

Now, for the packaging, make sure that the code is bug free and properly documented, following the best practices.

  1. Build the program in Release Mode. Make sure, there are no errors.
  2. Navigate to the "bin" folder. Here, you should see the "Release" folder.
  3. Verify that files in the "Release" folder are updated, at least the DLL of your program.
  4. Compress the "Release" folder to a zip.
  5. Log into Azure and navigate to your web app. Create a web app if you have not created it yet.
  6. Select Web Job >> Add New.
  7. Upload your zip file and provide the details to run. Save the Web Job.
  8. Click "Run Now" to test your application.

Do let me know if you get stuck. Please provide your feedback on the article.

Ebook Download
View all
TOP Azure
Read by 1 people
Download Now!
Learn
View all