Java application to send mail using Sendgrid & IBM Bluemix

IBM® Bluemix™ is the Platform as a Service(PaaS) cloud offering from IBM®. It enables organizations and developers to quickly and easily create, deploy, and manage applications on the cloud. Bluemix delivers enterprise-level services that can easily integrate with your cloud applications without you needing to know how to install or configure them.

SendGrid provides a cloud-based email delivery service that assists businesses with transactional email management while abiding by anti-spam regulations. The service manages various types of email including shipping notifications, friend requests, sign-up confirmations, and email newsletters.

This tutorial will help you to build and deploy a java application that sends registration successful mail using SendGrid and IBM® Bluemix™

Prerequisites

Let’s start building the java application,before that lets go to our Bluemix Catalog and select required services to build it!

Step 1: Goto Catalog and then click on Java Runtime

blog1

Step 2: Enter your application name. Eg: mailreg as shown in the image below. Click on CREATE

4

Step 3: Application has been created and it can be viewed on the Bluemix DASHBOARD. Click on the application

5

Step 4: Click on ADD SERVICE

6

Step 5: In CATALOG goto Web and Application and click on SendGrid

7

Step 6: Change the Service name if required and click on CREATE

8

Step 7: Click on Add a service again and then in CATALOG goto Data and Analytics and click on SQLDB

blog_6

Step 8: Create the service by giving it a service name. Follow the steps to create a table in SQLDB as shown in my previous blog

The service is now up and running in Bluemix!! We can now proceed to Eclipse IDE to build the registration application that inserts details into database and also sends out registration successful mail.

Please ensure that you Copy and paste the SendGrid-java jar to the below shown location

10

Please refer to my previous blog to understand how to add data to SQL database and append the piece of code(shown below) to send mail upon successful completion of registration

import com.sendgrid.*;

public class SendGridExample {
  public static void main(String[] args) {
    SendGrid sendgrid = new SendGrid("sendgrid_username","sendgrid_password");;
//OR
    SendGrid sendgrid = new SendGrid("SENDGRID_APIKEY");

 SendGrid.Email email = new SendGrid.Email();   
 email.addTo("xxxxxxx@gmail.com"); //Can programatically add sender's email id!  
 email.setFrom("noreplybluemix@gmail.com");  
 email.setSubject("Thank You for registering for Email services");  
 email.setHtml("<h1>Thank You for registering for Email Services!</h1>");
 email.setText("Thank You for registering for Email Services!");

  SendGrid.Response response = sendgrid.send(email);  } } 

You can add attachment to the mail & there are more options apart from these!

Push the application to Bluemix(refer my previous blog) and on successful 
registration you will receive a mail in your inbox as shown below

OUTPUT:

Output

Integrating mail services had never been this easy! 
Thanks to IBM Bluemix and SendGrid!

Courtesy: (bluemix.net) IBM Bluemix and SendGrid (sendgrid.com)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s