Connect to Gmail IMAP using javamail

Source: Internet
Author: User
Tags imap

Import java. util. properties;

Import javax. Mail. folder;
Import javax. Mail. message;
Import javax. Mail. Session;
Import javax. Mail. store;

Public class program {

/**
* @ Param ARGs
* @ Throws exception
*/
Public static void main (string [] ARGs) throws exception {
String host = "imap.gmail.com ";
Int Port = 993;
String user = "xxxxx@gmail.com ";
String Password = "XXXXX ";
String target_folder = "inbox ";

Properties props = system. getproperties ();
Session sess = session. getinstance (props, null );
// Sess. setdebug (true );
Store ST = sess. getstore ("IMAPs ");
St. Connect (host, port, user, password );
Folder fol = ST. getfolder (target_folder );
If (fol. exists ()){
For (Folder F: fol. List ()){
System. Out. println (F. getname ());
}
Fol. Open (Folder. read_only );
For (message M: fol. getmessages ()){
System. Out. printf ("% s-% d/N", M. getsubject (), M. getsize ());
}
Fol. Close (false );
} Else {
System. Out. printf ("% s is not exist.", target_folder );
}
St. Close ();

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.