Free trial
  • On the page (jump to):

Send email from Mobile Services with SendGrid

This topic shows you how can add email functionality to your mobile service. In this tutorial you add server side scripts to send email using SendGrid. When complete, your mobile service will send an email each time a record is inserted.

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. For more information, see http://sendgrid.com.

This tutorial walks you through these basic steps to enable email functionality:

  1. Create a SendGrid account
  2. Add a script to send email
  3. Insert data to receive email

This tutorial is based on the Mobile Services quickstart. Before you start this tutorial, you must first complete Get started with Mobile Services.

Create a new accountCreate a new SendGrid account

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.

To sign up for a SendGrid account

  1. Log in to the Windows Azure Management Portal.

  2. In the lower pane of the management portal, click New.

    command-bar-new

  3. Click Store.

    sendgrid-store

  4. In the Choose an Add-on dialog, select SendGrid and click the right arrow.

  5. In the Personalize Add-on dialog select the SendGrid plan you want to sign up for.

  6. Enter a name to identify your SendGrid service in your Windows Azure settings, or use the default value of SendGrid. Names must be between 1 and 100 characters in length and contain only alphanumeric characters, dashes, dots, and underscores. The name must be unique in your list of subscribed Windows Azure Store Items.

    store-screen-2

  7. Choose a value for the region; for example, West US.

  8. Click the right arrow.

  9. On the Review Purchase tab, review the plan and pricing information, and review the legal terms. If you agree to the terms, click the check mark. After you click the check mark, your SendGrid account will begin the provisioning process.

    store-screen-3

  10. After confirming your purchase you are redirected to the add-ons dashboard and you will see the message Purchasing SendGrid.

    sendgrid-purchasing-message

Your SendGrid account is provisioned immediately and you will see the message Successfully purchased Add-On SendGrid. Your account and credentials are now created. You are ready to send emails at this point.

To modify your subscription plan or see the SendGrid contact settings, click the name of your SendGrid service to open the SendGrid add-ons dashboard.

sendgrid-add-on-dashboard

To send an email using SendGrid, you must supply your account credentials (username and password).

To find your SendGrid credentials

  1. Click Connection Info.

    sendgrid-connection-info-button

  2. In the Connection info dialog, copy the Password and Username to use later in this tutorial.

    sendgrid-connection-info

To set your email deliverability settings, click the Manage button. This will open the Sendgrid.com web interface where you can login and open your SendGrid Control Panel.

sendgrid-control-panel

For more information on getting started with SendGrid, see SendGrid Getting Started.

Register a scriptRegister a new script that sends emails

  1. Log on to the Windows Azure Management Portal, click Mobile Services, and then click your mobile service.

  2. In the Management Portal, click the Data tab and then click the TodoItem table.

  3. In todoitem, click the Script tab and select Insert.

    This displays the function that is invoked when an insert occurs in the TodoItem table.

  4. Replace the insert function 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);
            }
        });
    }
    
    
    }
  5. Replace the placeholders in the above script with the correct values:

    • username and password: the SendGrid credentials you identified in Create a 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

      If you do not have a registered domain, you can instead use the domain of your Mobile Service, in the format notifications@your-mobile-service.azure-mobile.net. However, messages sent to your mobile service domain are ignored.

  6. Click the Save button. You have now configured a script to send an email each time a record is inserted into the TodoItem table.

Insert test dataInsert test data to receive email

  1. In the client app project, run the quickstart application.

    This topic shows the Windows Store version of the quickstart,

  2. In the app, type text in Insert a TodoItem, and then click Save.

  3. Notice that you receive an email, such as one shown in the notification below.

Congratulations, you have successfully configured your mobile service to send email by using SendGrid.

Next Steps

Now that you’ve seen how easy it is to use the SendGrid email service with Mobile Services, follow these links to learn more about SendGrid.