How to Retrieve an Email Message Using Java Mail API

As we know JavaMail API is a convenient way of sending and retrieving email message in a J2EE based web application. Here we will discuss the procedure of retrieving the email from respective inbox folder by using JavaMail API. For accomplishing this, the J2ee component (which we are using in our application to retrieve the mail) needs to connect to the mail server and retrieve a session.

After this, it requests for the INBOX which contains the email messages. For getting access to it, the email requestor needs to authenticate itself. For understanding this in detail consider the following example:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*

public class RetrievingMail
{
public static void main(String args[])
{
String myhost= “smtp.xyz.com”;
String username= “Robert”;
String pwd= “xyz”;
Properties prp = new Properties();
Session sess1=Session.getDefaultInstance(prp,null);
Store store11 = sess1.getStore("pop3");
store.connect(myhost,username,pwd);
Folder folder11 = store11.getFolder("INBOX");
folder11.open(Folder.READ_ONLY);
Message mesg[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++)
{
System.out.println(i + ": " + mesg [i].getFrom()[0] "\t"
+mesg [i].getSubject());
System.out.println(“want to read? [Y/N]”);
String firstline=new BufferedReader (new InputStreamReader
(System.in)).readLine();
if(“Y” .equals(firstline))
{
System.out.println(mesg[i].getContent());
}
else if (“N”.equals(firstline))
{
break;
}
}
folder11.close(false);
store11.close();
}
}

Here we have declared three String objects: myhost, username and pwd. After this an instance of Property object is passed to the getDefaultInstance() method to create a session. Session object is here referred as sess1. Then we have created Store and Folder objects which are denoted as store11 and folder11. store11 is created using getStore() method which uses pop3 as argument because POP3 protocol is used to store the messages. Now access to INBOX folder is retrieved using getFolder() method.

After this we have used three methods: getMessage() which retrieves the messages from the folder which are represented through array of messages here. Next is getFrom() method which displayed the sender’s name and third method is getSubject() which retrieves the subject line of the email mesaage. Here after displaying the sender’s name and subject of mail we have asked the email requestor whether he or she would like to read the message or not.

The response of client is assigned to the variable firstline and then we have used if loop to evaluate his or her response. If client wants to read or his response is ‘Y’ we have retrieved the email message body using an another method getContent().
After displaying the email body the connection to the Folder and Store object is closed.


.

SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________



| All About Different Types of XML Editors | Generating XML Document Using JSP | How to Retrieve an Email Message Using Java Mail API | How to Send an Email Message Using Java Mail API | Transforming XML Data with XSLT | Understanding Basic Components of a JMS Program | Understanding Basic Technology of ebXML | Understanding Deployment Descriptor in Reference with Servlets | Understanding eXtensible Access Control Markup Language (XACML) | Understanding the Design Goals of XML | Understanding XML Common Biometric Format |

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.