Home > AS/400 Tips > WebSphere Strategies for iSeries professionals > How to use Java to add e-mail support to your iSeries apps
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

WEBSPHERE STRATEGIES FOR ISERIES PROFESSIONALS

How to use Java to add e-mail support to your iSeries apps


Jim Mason
10.20.2005
Rating: -4.33- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



[TABLE]

If you want to do full e-mail from another language such as RPG or COBOL, you normally have to buy an e-mail solution. You can, however, add e-mail support to your applications and business processes easily with Java -- and with very little coding.

Have you ever thought of automatically sending an order acknowledgement as an Excel workbook to a customer via e-mail? It's not hard to do with Java -- even if you call it from an RPG program on your iSeries. You can also receive e-mail automatically with Java from a mail server. A Java application can "read" the e-mail and its attachments. If they are formatted data (such as an Excel workbook), you can automatically edit and then post the updated data to your database.

With Java's mail support you can use a Java program (application) to automatically send e-mail (text or HTML) with attachments, receive e-mail with attachments, reply to e-mail, forward e-mail and delete e-mail from your mail folders.

I've found that I can automate processes and workflows between companies easily using e-mail. Why? Unlike Web services, messaging and other technologies, all companies already have the ability to send and receive e-mail outside their organization. As a result, setting up an automated workflow application that uses e-mail for input and output on my end won't require my customers, partners, etc. to involve their IT staff in building or configuring interfaces to us -- a huge benefit when trying to easily implement integrated, distributed applications.

The Java Mail API

The Java Mail API offers a simple, consistent way to write mail applications regardless of the mail service provider (mail server) used and the operating environment -- iSeries, Windows, Linux or Web application server). It even accommodates many of the differences on how mail servers are set up with security constraints. Whether you use an in-house mail server or an outside mail server p


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
WebSphere Strategies for iSeries professionals
Application modernization strategies for System i
Application modernization in the i world
Natively supported Web applications for Power running i
Enterprise open source basics
Basic security considerations for a Domino/WebSphere system
Simplifying data access using Java Standard Tag Library
Integrating Microsoft ActiveX components with WebSphere
Choices for running Web workloads on iSeries
Virtual hosting for iSeries Web applications
Automate WebSphere configuration backups on the iSeries (i5)

Web Development
Migrating from RPG to EGL on IBM i
Groovy programming on IBM i
Running PHP open source applications: NOBODY needs authority
Zend Web software teams up with IBM System i
The best technologies and tools for System i programmers in 2009
Seven IBM i project lessons learned in 2008
AS/400 lessons from the past, present, and future: A holiday tale
Application modernization strategies for System i
RPG application modernization for i5
Web skills crucial to iSeries programmer professional development

iSeries Java programming
Groovy programming on IBM i
EGL Rich UI on IBM i: Do you Dojo?
Programming for the Web on the IBM i, what is possible
Database performance comparisons on IBM i
Database drivers on the i: MySQL vs. IBM Toolbox
How to: Output SQL script to a text file from an AS/400
Application modernization for the iSeries: Why bother?
JDBCODBC functionality -- Java to Excel for complex workbooks
Necessity leads to iSeries Watchdog development
Accessing AS/400 data using Excel ODBC drivers
iSeries Java programming Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
WebSphere Development Studio Client (WDSC)  (Search400.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


rovided by your ISP, you should be able to use the Java Mail API to send and receive mail easily. The Java Mail API implements a lot of the mail functions defined in the World Wide Web mail standards (known as RFCs).

Mail service providers

The three most common sources of mail servers to interface with are your company's in-house mail server (if one is available), a large, public mail server service (Yahoo or Google), or your ISP's mail server.

There are different types of mail providers that can be used to send or receive mail. The mail server you use determines the type of mail providers you'll need to use in your application. In the example in this article, I used the default mail providers included with the Java Mail API jar files -- Simple Mail Transport Protocol (SMTP) for sending mail and Post Office Protocol 3 (POP3) for receiving mail from an Inbox mail folder. You can add any provider you need through the Provider interface if you want to create your own mail server service. Here are the mail providers in the mail.jar file:

Here's the POP3 provider included in the pop3.jar file from Sun:

Key Java Mail classes

The Java Mail API has a few key classes you'll use to send and receive mail. I've summarized them in the below table.

[TABLE]

Java Mail Environment properties

The Java Mail API services use different environment properties to configure their behavior, primarily for the different mail protocols supported by the service.

[TABLE]

Writing a Java mail client

I used Eclipse 3.0 to create my simple Java mail client to send and receive mail. Eclipse is free from www.eclipse.org. You can also you use any other Java toolset. Most iSeries developers use IBM's WebSphere Development Studio Client for the iSeries. You should use at least JDK 1.4.2 or higher for a mail application, although older ones can work.

With Eclipse, I created a Java project first. Then to set up my environment for a mail application, I added the three Java Mail API files to my Java project's build path and I created a MailClient1.java class in the src folder of my project. The build path has the imports section for the javax.mail classes and the POP3Message class I imported.

Sending mail from Java

I chose to use Yahoo's mail servers to send and receive mail. Each mail server uses specific mail protocols to send and receive that are implemented a specific way. You need to get this information from your mail provider. Yahoo has a specific URL to send mail (smtp.mail.yahoo.com) and a specific URL to receive mail (pop.mail.yahoo.com). For Yahoo, the server also requires an authentication object to specify the correct user and password for the connection.

Another issue you may run into is an ISP that doesn't allow you to send mail using a program or client on the default SMTP port of 25. One of the ISPs I use has this restriction. To get around the problem, Yahoo also runs its mail servers on port 587. To set the port for the SMTP mail server, I had to set another system property (environment variable) the Java Mail SMTP protocol accesses, "mail.smtp.port". Here's the setting for the SMTP port for the mail client: Usually you'll have to supply valid authentication information (user id and password) in a PasswordAuthentication object to send or receive mail to the mail server. Your mail provider can give you this information if you don't already have it.

To send mail, I created a method that does the following:

Here's a test mail message I sent to myself from Java over the Yahoo mail server.

[TABLE]

Receiving mail in Java

I chose to use Yahoo's mail servers to receive mail also. For Yahoo, the server also requires an authentication object to specify the correct user and password for the connection.

To receive mail, I created a method that did the following:

This shows the resulting mail message output received from the mail server that I dumped to the system console in Java:

To read a mail message, I turned the input stream from the MimeBodyPart of the MimeMessage into a text string for display. The actual output from this method included ALL parts of the mail message as text including headers. I abbreviated the output shown to focus just on the text content of the mail message.

Content type determines how you'll read and send e-mails. In my case, I received mail in plain/text format as the content type (vs. plain/HTML format). You CAN use the HTML format to send nicely formatted e-mails with style sheets. You know those annoying vendor e-mails you get with the nice graphics? That's how they do it.

After receiving all the mail, I shut down the mail folder I opened and closed the mail store.

Java Mail development options

You can add the mail support to your application by downloading the three mail API jar files from Sun's Web site directly (mail.jar, activation.jar and pop3.jar) or you can just use the latest version of the J2EE jar file (j2ee.jar) that includes the mail support.

Once you've downloaded the mail jar flies, you'll need to add them to your classpath. The line below shows how I added the jar files to the classpath in the Windows Command Shell environment:

Remember to also run the System.setProperty methods to set the SMTP environment variables for mail server ("mail.smtp.host") and the authentication value ("mail.stmp.auth") if needed.

Resources

We've covered only the basics of sending and receiving mail in this article. There is a lot more you can do with Java Mail. If you can take the time, investigate the resources listed below.

---------------------------------------

About the author: Jim Mason works at ebt-now, an iSeries Web integration company, providing QuickWebServices for iSeries customers: Web planning, WebSphere, WebFacing, Web development, Web networking, Web support, Web security and training services. Jim is creating a self-study course for RPG programmers that teaches "hands-on" rapid visual development with WDSC for all types of iSeries and e-business applications without the need to become a Java expert. Rochester Initiative will publish the course. You can reach Jim at jemason@ebt-now.com.


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts