• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Sending E-Mail from RPG, Take Two

    October 27, 2004 Hey, David

    The code for this article is available for download.

    Is there a continuation article to “Sending E-Mail from RPG” from last May? It was a really good article, but I’m not sure I can make modifications to be able to send e-mail with attachments. My problem is in the RPG program. I don’t know how to declare the prototype for the DataSource object (attachments). Could you please help or refer me to an article I could read?

    –Ana

    Your question points out that I could have made the Mailer Java class more RPG friendly. Although it is possible to create a DataSource in RPG, it is much easier to put the code in the Mailer class.

    To solve this problem, I used a feature of Java called method overloading. I left the SendMultiPartMail method intact, with its DataSource array parameter, and added a new SendMultiPartMail method that takes a String array containing file names in place of the DataSource array. So now there are two SendMultiPartMail methods, and the Java runtime environment will call the appropriate method based on the parameter types passed at runtime, which is the essence of overloading.

    I added the new method because it is much easier to create a String array in RPG IV than to create an array of DataSource objects. The following code shows the new method, which creates a DataSource in Java code and calls the original method.

    /**
     * Send a multipart email.
     * @param to        The destination address
     * @param from      The source address
     * @param subject   The subject line for the email
     * @param message   The message for the email
     * @param fileNames The names of files to attach
     * @throws MessagingException
     */
    public static void sendMultiPartMail(
        String to,
        String from,
        String subject,
        String message,
        String[] fileNames)
        throws MessagingException {
        DataSource bodyParts[] = new DataSource[fileNames.length];
    
        for (int i = 0; i < fileNames.length 
             && filenames[i] != null; i++) {
            bodyParts[i] = new FileDataSource(new File(fileNames[i]));
        }
        sendMultiPartMail(to, from, subject, message, bodyParts);
     }
     

    Calling this new method from your RPG IV program is now much easier. The following snippet shows the RPG IV prototype and call for this new Java method.

    DMailer$sendMultiPartMail...
    D                 PR                  STATIC
    D                                     EXTPROC(
    D                                       *JAVA:
    D                                       'demo.Mailer':
    D                                       'sendMultiPartMail')
    D to                                  LIKE(newString)
    D                                     CONST
    D from                                LIKE(newString)
    D                                     CONST
    D subject                             LIKE(newString)
    D                                     CONST
    D message                             LIKE(newString)
    D                                     CONST
    D attachments                         LIKE(newString)
    D                                     CONST
    D                                     DIM(32767)
    D                                     OPTIONS(*VARSIZE)
     …
    Dto               S               O   CLASS(*JAVA:'java.lang.String')
    Dfrom             S               O   CLASS(*JAVA:'java.lang.String')
    Dsubject          S               O   CLASS(*JAVA:'java.lang.String')
    Dmessage          S               O   CLASS(*JAVA:'java.lang.String')
    Dattachments      S               O   CLASS(*JAVA:'java.lang.String')
    D                                     DIM(10)
    ...
     /free
         to = newString('somebody@itjungle.com');
         from = newString('dmorris@itjungle.com');
         subject = newString('This is the subject');
         message = newString('Here is a test message');
         attachments(1) = newString('/java/mailer/demo/Mailer.java');
         Mailer$sendMultiPartMail(to: from: subject: message: attachments);
     /end-free
    

    This example attaches a single file found in the IFS to an e-mail message and sends it. The RPG IV code in this example supports up to 10 attachments; however, you could change DIM(10) to support any number of attachments. Instructions for creating the Mailer Java program are contained in the source. You will also have to modify Mailer.properties to point to your mail server. If you send e-mail directly through an ISP, you may also need to supply a user and password.


    Now change the e-mail addresses and attachment location in the mailerdemo RPG IV program and create it using CRTBNDRPG. Before you run mailerdemo, you need to add Mailer.class, along with Mailer.properties, Java Mail.jar, and Activation.jar, to your classpath. You would do this with the ADDENVVAR command, changing the path to reflect your environment, like this.

    ADDENVVAR VAR('/java/mailer:/java/javamail-1.3.1/
       mail.jar:/java/jaf-1.0.2/activation.jar')
    

    Now run the example. If everything is set up properly, you should find an e-mail in your inbox with the source for the Mailer Java program attached.

    –David Morris

    Click here to contact David Morris by e-mail.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    New BOSaNOVA Tool Integrates iSeries Data with Windows, Office iSeries High Availability Should Be Integrated and Invisible

    Leave a Reply Cancel reply

Volume 4, Number 36 -- October 27, 2004
THIS ISSUE
SPONSORED BY:

WorksRight Software
Advanced Systems Concepts
Guild Companies

Table of Contents

  • Sending E-Mail from RPG, Take Two
  • Spool Control Authority Is a Security Risk
  • Monitoring for System Request Menu Option 2

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18
  • Will The Turbulent Economy Downdraft IBM Systems Or Lift It?
  • How IBM Improved The Database With IBM i 7.6
  • Rocket Celebrates 35th Anniversary As Private Equity Owner Ponders Sale
  • 50 Acres And A Humanoid Robot With An AI Avatar
  • IBM i PTF Guide, Volume 27, Number 17

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle