IBM i > DEVELOPER > GENERAL

Using Google App API to Send Email From IBM i


In the previous series of three Google API articles, I showed you how to interface with the Google Apps for Business (GA4B) APIs to retrieve a list of documents, send a document and create a calendar event. Since that series, I’ve been asked on numerous occasions to show how to send Google Mail (Gmail) from IBM i. This article provides the steps for connecting with the GA4B cloud world to send an email containing a subject line, email body and optionally an attached document from the IFS via an API.

Creating the CMD/PNLGRP/CLLE/RPGLE/Java Source
As in the first article, you must create an interface command to prompt the user for the Google email, password, recipient email address, email subject, email body, attachment and directory parameters to be passed to GA4B for processing.

I created this simple command, GA4BEMAIL (Code Sample 1), and a panel group for command parameter help, GA4BEMAILP (Code Sample 2). Please note, the PASSWORD parameter on your command is not secure with just the DSPINPUT(*NO) variable setting.

The command executes the GA4BEMAILC (Code Sample 3) CLLE command processor program (CPP) to set your Java Class path as before. The GA4BEMAILR RPGLE program is then called with your email, password, recipient email address, email subject, email body, attachment and directory parameters.

The GA4BEMAILR RPGLE (Code Sample 4) is again a simple program that accepts the seven parameters passed to it, parses them into Java string objects and calls the Java main method.

The Java program, GA4BEMAILJ (Code Sample 5), was created in Eclipse. Add this program to your existing GoogleDocs project as was done in our past examples and configure the compiler to use these packages:

  • java.io.File;
  • java.io.IOException;
  • import java.util.Properties;
  • import javax.activation.*;
  • import javax.mail.*;
  • import javax.mail.internet.*;

Configure your Java compiler to use the Java 1.5 Java Runtime Environment at a minimum and create a Java Runtime Configuration for testing and to export the Java Classes as a run-capable JAR. I’ve provided the GoogleDocs JAR on my website (GoogleDocs.JAR) for downloading as well. This JAR contains all the Java objects previously created in the GA4B series.

After you import your required classes and parse the passed parameter data to variables, set up your SMTP connection parameters to connect with your Gmail host and connect to the Java Mail Authenticator to validate your credentials (email and password). Once validated and connected, complete the required parameters to build the email document to send, such as the email subject and body, and then attach the optional document before sending the message to Gmail for the Google user.

Executing Your Utility

From a command line, you can prompt your GA4BEMAIL command and enter the required and optional parameters for your test.

Once your process is executed, the email will be sent via Gmail using the specified Google user account to the email address you provide. You can send to multiple email addresses by separating addresses with a comma.

Automating Your Business Needs

As was the business case for the previous sample articles and code projects, automating the steps to send an email using Gmail and the Google Apps for Business Developer APIs makes the integration of the IBM i DB2 database and the public cloud easier and helps meet your business needs of sharing data between the two platforms.

This example adds to a tool set and demonstrates that it can all be done fairly simply. By being creative, you can push a document (PDF, CSV, DOC, etc.) to Google Drive, retain/retrieve the Google Doc ID assigned to it and send it in an email. Our simple example sends a document, such as a generated report PDF (i.e., Invoice, PO, quote, etc.), from the IFS to an email recipient.

The Google Developers website is a great place to start for information I’ve used in this article series.

I would like to again thank my coding partner Doug Davie, Sr. Software Engineer for SoftLanding Systems, for his assistance in the coding of the examples in this series and Mitch Greenwald and his group of Cloud Solution Specialists at Cloudbakers for all their help and patience.

David Andruchuk is the senior architect for Computer Systems Design Associates Inc.



Advertisement

Advertisement

2017 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

Are You Multilingual?

Rational enables development in multiplatform environments

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters