Java SDK doesn't come with inbuilt mail API but Java-EE (now called Jakarta EE) provides several utility library like json, servlet, web-service, jms, validation, xml, regexp including email.
In this example I'm going to show how we can connect to email server (eg: gmail) and download/view seen/unseen emails.
Required Dependency :jakarta.mail
Add this dependency with latest version in your pom.xml or manually download/add the jar file to your project's lib.
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
</dependency>
The API Usage:
Ideally, the steps would be as following:
- Create Session with host, protocal type etc
- Get message store (Store) for the email protocol. The supported protocols are IMAP, POP, SMTP etc
- Connect to the host using username/password
- Open folder and search for messages
The API Usage with code example:
- Create Session with host, protocol type etc
Properties properties = new Properties();
properties.put("mail.imap.host", host);
properties.put("mail.store.protocol", protocal);
Session emailSession = Session.getDefaultInstance(properties);
- Get message store (Store) for the email protocol. The supported protocols are IMAP, POP, SMTP etc
Store store = emailSession.getStore(protocal);
- Connect to the host using username/password
store.connect(host, user, password);
- Open folder and search for messages
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Search using filters
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message[] messages = emailFolder.search(unseenFlagTerm);
Complete Example:
import javax.mail.*;
import javax.mail.search.FlagTerm;
import java.util.Properties;
public class MailllllIMAP {
public static void check(String host, String protocal, String user, String password) {
try {
Properties properties = new Properties();
properties.put("mail.imap.host", host);
properties.put("mail.store.protocol", protocal);
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore(storeType);
store.connect(host, user, password);
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
//unseen email filter
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message[] messages = emailFolder.search(unseenFlagTerm);
System.out.println("Total messages: " + messages.length);
for (Message m : messages) {
System.out.println(("From: " + m.getFrom()[0]));
System.out.println(("Subject: " + m.getSubject()));
System.out.println(("Subject: " + m.getContent()));
//other fields
}
//close or do more stuff
emailFolder.close(false);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String host = "pop.gmail.com";
String protocal = "imaps";
String username = "XXXX@gmail.com";
String password = "XXXXXXX";
check(host, protocal, username, password);
}
}
What if you get an error like this?
AuthenticationFailedException: [AUTH] Username and password not accepted.
You get this error when trying to gmail. You will need to turn ON the "Allow less secure apps" option on https://www.google.com/settings/security/lesssecureapps
No comments :
Post a Comment
Your Comment and Question will help to make this blog better...