Home arrow Java arrow Page 5 - Java Mail API: Transforming Mail into Data Carriers

Java Mail API: Transforming Mail into Data Carriers

Java Mail is eminently adaptable. Indeed, looking at its API, we find that it helps us provide an alternative solution to an old problem: how to transfer large amounts of data between two enterprise applications. In this article, you will discover how to adapt Java Mail to play the role of a data carrier.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
November 22, 2005
  1. · Java Mail API: Transforming Mail into Data Carriers
  2. · Java Mail Core Classes Continued
  3. · Mails As Data Carriers: A Practical Approach
  4. · Sending
  5. · Receiving

print this article

Java Mail API: Transforming Mail into Data Carriers - Receiving
(Page 5 of 5 )

That is all there for sending. Now let's look at receiving. For this, another function, recvDocument, will be defined. It will take the Message object as a parameter.

boolean recvDocument(Message message)
Multipart mp = (Multipart)message.getContent();

for (int i=0, n=multipart.getCount(); i<n; i++) {
  Part part = multipart.getBodyPart(i));

  String disposition = part.getDisposition();

  if ((disposition != null) &&
      ((disposition.equals(Part.ATTACHMENT) ||
       (disposition.equals(Part.INLINE))) {
    saveFile(part.getFileName(), part.getInputStream());

The function creates a Multipart from the message object. Then, until the parts are completed, it is read, checked for deposition (flagging of boundary of attachments) and sent to a function that saves the file by reading from the stream. Due to the simplicity of the save() function, it is not being shown here. That completes the sending and receiving of data documents.

It should be evident from the code how easy it is to make Java Mail API to work as a data carrier. Though this is a different usage of the mailing system, it proves that any API, if viewed from a different perspective, can provide new solutions to old problems. That brings us to the end of this tutorial on Java Mail API. I hope it has provided you with an insight into what can be achieved with Java Mail. Using it for data transfer is just one of many approaches. Dig deeper and you will find many more approaches. Till next time.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials