JavaMail – read email
Today we have a quick update for your, code that can be used to read email messages from IMAP
or POP3
server. This code uses JavaMail API for reading emails from the server. It displays inbox contents including email sender, title and body. Take a look at our code.
JavaMail read emails- the code
package com.itcuties.java; import java.util.Properties; import javax.mail.Address; import javax.mail.Folder; import javax.mail.Message; import javax.mail.Session; import javax.mail.Store; /** * Class reads emails * * @author itcuties * */ public class JavaMailReader { public static void main(String[] args) { readEmails(true); } /** * Method reads emails from the IMAP or POP3 server. * @param isImap - if true then we are reading messages from the IMAP server, if no then read from the POP3 server. */ private static void readEmails(boolean isImap) { // Create all the needed properties - empty! Properties connectionProperties = new Properties(); // Create the session Session session = Session.getDefaultInstance(connectionProperties,null); try { System.out.print("Connecting to the IMAP server..."); // Connecting to the server // Set the store depending on the parameter flag value String storeName = isImap ? "imaps" : "pop3"; Store store = session.getStore(storeName); // Set the server depending on the parameter flag value String server = isImap ? "imap.itcuties.com" : "pop3.itcuties.com"; store.connect(server,"coding@itcuties.com","P@ssw0rd1"); System.out.println("done!"); // Get the Inbox folder Folder inbox = store.getFolder("Inbox"); // Set the mode to the read-only mode inbox.open(Folder.READ_ONLY); // Get messages Message messages[] = inbox.getMessages(); System.out.println("Reading messages..."); // Display the messages for(Message message:messages) { for (Address a: message.getFrom()) System.out.println("From:" + a); System.out.println("Title: " + message.getSubject()); System.out.println(); System.out.println(message.getContent()); System.out.println("---"); } } catch (Exception e) { e.printStackTrace(); } } }
When you run this code you will get the following output:
Connecting to the IMAP server...done! Reading messages... From:Team ITC <team@itcuties.com> Title: Test message subject Hello email :) --- From:Team ITC <team@itcuties.com> Title: Message title Hi there! Take IT easy itcuties ---
If you are using maven like we do, this is the pom.xml
file with all the needed artifacts the javamail
, javax.activation
and the javax.mail
extensions.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itcuties.java</groupId> <artifactId>java-mail-reader</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- JavaMail API --> <dependency> <groupId>javamail</groupId> <artifactId>javamail</artifactId> <version>1.3.3</version> </dependency> <!-- Java Activation --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!-- JavaMail extension --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.6-rc1</version> </dependency> </dependencies> </project>
Download this sample code here.
This code is available on our GitHub repository as well.
Nice Example.. thanks was very helpful for my project…
hi,
I have exception
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target;
My server doesn’t have certificate.
please help me.
It seems that you might be trying to connect to a server via the SSL protocol. If so your local java machine needs to trust this certificate. You need to add the certificate to your local JAVA’s keystore. Check out this link – http://docs.oracle.com/javase/tutorial/security/toolsign/rstep2.html.
Hi, I used above code to read emails from my gmail account but I am getting null pointer exception at message.getContent();
Can u please tell me y i am getting that exception.
I added mail.jar and activation.jar in my classpath and I am getting remaining details from gmail except content.
Thank you.
Chandu.
how to read mails using java other than imap and pop3 protocols??
In IT companies they are not giving any exchange server names.
can you please suggest any other way..
God Bless you, i couldn’t believe it, it works like magic
Thanks! It worked for me!
server…