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. |