Sending E-Mail from RPG
May 12, 2004 Hey, David
[The code for this article is available for download.]
A few years ago you wrote an article about sending e-mail using the QzmfCrtMailMsg API. I used your code at my last job to send e-mail from RPG programs. I told my new boss I could set it up so that we could send e-mail from our RPG programs, but I don’t have the magazine with that article. Is that article available online, and is the QzmfCrtMailMsg still the way to go?
–Frank
Back in 1998, QzmfCrtMailMsg was a good option for RPG programmers, but there are better options today. One of those options is the JavaMail API. The advantages of the JavaMail API are price, reliability, cross-platform compatibility, and simplicity. The disadvantages are performance and no vendor support.
Writing a Java program that uses the JavaMail API isn’t too difficult, once you get past some of the nuances of creating and running Java programs on the iSeries. I wrote a sample Mailer Java program you can use to get started. You can call that sample program from an RPG IV program, like the sample MailerDemo RPG IV program. Comments in the source for the sample programs describe how to compile and run these programs on the iSeries.
The following summary describes the methods found in the Mailer class.
- getSession creates a JavaMail Session object for the URL defined in a mail.smtp.host property, using an optional JavaMail Authenticator object. The authenticator is created if mail.user and mail.password properties exists.
- sendSimpleMail (to, from, subject, message) creates and sends a JavaMail message with no attachments.
- sendMultiPartMail (to, from, subject, message, bodyParts) creates and sends a JavaMail message with multiple parts that can have attachments.
Properties used by the Mailer class can be defined in a Mailer.properties file or by setting system properties. The sample mailer JUnit test allows you to test the Mailer from a Java program. For more information on running JUnit, see “JUnit Automates Java Testing.”
The Mailer starts by opening a session to an SMTP server. You specify the URL for the SMTP server in a mail.smtp.host property. If you supply a user and password, via the mail.user and mail.password properties, the program uses them to authenticate to the SMTP server. After creating a session, a message is created and sent using the session.
The RPG program will run only on V5R1 or later releases that have support for direct Java calls. The RPG program starts by creating four string objects. Those four strings contain the “from” and the “to” addresses, along with the subject and message. The next thing the RPG program does is to call the Mailer’s sendSimpleMail method to send a message.
Mixing RPG and Java to send mail may seem complex, but this solution is actually much simpler than the convoluted Mail Server Framework APIs.
–David