Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Azure Mobile Services - Send an email with SendGrid

From Wiki
Jump to: navigation, search

This article shows you how can send emails from your Azure Mobile Service (directly or app-initiated) using the SendGrid web service.

WP Metro Icon Messages.png
WP Metro Icon Web.png
WP Metro Icon WP8.png
Article Metadata
Compatibility
Platform(s):
Windows Phone 8
Article
Created: galazzo (20 Jun 2013)
Last edited: galazzo (12 Aug 2013)

Contents

Introduction

SendGrid is a cloud-based email service that provides reliable email delivery, scalability, and real-time analytics, along with flexible APIs that make custom integration easy.

This article explains how to add server side scripts to your Mobile Service in order to send emails using SendGrid. When complete, your mobile service will send an email each time a record is inserted.

This is useful, for example, when implementing a user registration process that requires a confirmation email from the service. It is also useful in any other case you need to send an email from your mobile application without opening the built-in email client.

Another key benefit is that many developers don't have their own mail server / service. Using this approach Windows Azure customers can unlock 25,000 free emails each month. These 25,000 free monthly emails will give you access to advanced reporting and analytics and all APIs (Web, SMTP, Event, Parse, Sub-User). For information about additional services provided by SendGrid, see the SendGrid Features page.

Pre-requisites

The instructions in this article require you have an Azure Mobile Service. If you don't have one already then you can create a service by following the instructions in Azure Mobile Services on Windows Phone.

Create a SendGrid account

Log in to the Windows Azure Management Portal.

Click on Create button

Sendgrid-create-button.png

Compute -> Store

Choose an Add-on

In the Choose an Add-on dialog, select SendGrid

Select SendGrid in the "Choose an Add-on" dialog

Enter a name to identify your SendGrid service in your Windows Azure settings, or use the default value of SendGrid. The name must be unique in your list of subscribed Windows Azure Store Items. Select the free option.

Create the SendGrid account

Note.pngNote: You have 25,000 email per month with report analytics! For a mobile application this is a huge number, but if you need more, the payment option is extremely cheap. The payment option allows you access through a REST API - if you plan to share the service with your web site then choosing the payment option makes sense.

Finally you get the recap/summary screen

Recap screen

Now the service is created and you should be redirected into a screen like the one below:

Service summary

Click on the service.

Get SendGrid credentials

Click on Connection Info to get the key to be used in your script to access the service.

Click "Connection Info"
Details to use in your script

Email deliverability settings

To set your email deliverability settings, click the Manage button.

Sendgrid-manage-button.png

This will open the Sendgrid.com web interface where you can login and open your SendGrid Control Panel.

Sendgrid-panel-02.png

Fill all profile setting with your information. For more information on getting started with SendGrid with other languages or platforms, see SendGrid Getting Started.

Register a new script that sends emails

In the Management Portal select Mobile Services and click on Data tab, select the Table you want to use ( i.e. the Users table ) then click on Script working on Insert operation.

Working on Insert it means that we will send an email after an insert operation on our table is performed. If you need to you can do the same thing with update or delete (i.e. to send an email when a user delete their account ).

Sendgrid-prepare-script.png

Let's write the server side script to send the email. The script engine is Node.js, written in Javascript

Replace with the following code:

var SendGrid = require('sendgrid').SendGrid;
 
function insert(item, user, request) {
request.execute({
success: function() {
// After the record has been inserted, send the response immediately to the client
request.respond();
// Send the email in the background
sendEmail(item);
}
});
 
function sendEmail(item) {
var sendgrid = new SendGrid('**username**', '**password**');
 
sendgrid.send({
to: '**email-address**',
from: '**from-address**',
subject: 'New to-do item',
text: 'A new to-do was added: ' + item.text
}, function(success, message) {
// If the email failed to send, log it as an error so we can investigate
if (!success) {
console.error(message);
}
});
}
}
  • username and password: your SendGrid credentials you used creating the SendGrid account.
  • email-address : the address that the email is sent to. In a real-world app, you can use tables to store and retrieve email addresses. When testing your app, just use your own email address.
  • from-address : the address from which the email originates. Consider using a registered domain address that belongs to your organization.

Note.pngNote: If you do not have a registered domain, you can use the domain of your Mobile Service: notifications@your-mobile-service.azure-mobile.net. However, messages sent to this email are ignored.

Click Save. From now on each time you insert a new item into this table an email is sent with the content you created.

This page was last modified on 12 August 2013, at 19:50.
327 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×