Javamail reading Gmail emails

Source: Internet
Author: User
Tags imap email account gmail mail

Recently I was writing the netbeans plug-in for javamail to receive Gmail mail and found a problem ....

The following is a code snippet connecting the email account with the email recipient:

/**
* Conncect To The Gmail account.
* @ Param username User Name
* @ Param userpassword User Password
* @ Throws exception
*/
Private Static void connect (string username, string userpassword)
Throws exception {
Security. addprovider (New com.sun.net. SSL. Internal. SSL. provider ());
Final string ssl_factory = "javax.net. SSL. sslsocketfactory ";
Properties props = system. getproperties ();
Props. setproperty ("mail. pop3.socketfactory. Class", ssl_factory );
Props. setproperty ("mail. pop3.socketfactory. Fallback", "false ");
Props. setproperty ("mail. pop3.port", "995 ");
Props. setproperty ("mail. pop3.socketfactory. Port", "995 ");

Props. setproperty ("mail. imap. socketfactory. Class", ssl_factory );
Props. setproperty ("mail. imap. socketfactory. Fallback", "false ");
Props. setproperty ("mail. imap. Port", "993 ");
Props. setproperty ("mail. imap. socketfactory. Port", "993 ");

Session session = session. getdefaultinstance (props, null );
Session. setdebug (true); // set debug

// Urlname urln = new urlname ("pop3s", "pop.gmail.com", 995, null,
// Username, userpassword );
Urlname urln = new urlname ("IMAP", "imap.gmail.com", 993, null, username,
Userpassword );
Store = session. getstore (urln );
Store. Connect ();
}

/**
* Retieve all new messages.
* @ Param username User Name
* @ Param userpassword User Password
* @ Return all new messages
* @ Throws exception
*/
Public message [] getnewmails (string username, string userpassword)
Throws exception {
If (! Store. isconnected ()){
Connect (username, userpassword );
}

Folder inbox = store. getfolder ("inbox ");
Inbox. Open (Folder. read_only );
Fetchprofile profile = new fetchprofile ();
Profile. Add (fetchprofile. item. envelope );

If (inbox. getunreadmessagecount ()> 0 ){
Int fetchcount = inbox. getmessagecount ()-inbox. getunreadmessagecount ();
If (fetchcount = 0 ){
Return inbox. getmessages ();
}

Message [] messages = inbox. getmessages (1, 2 );

Return messages;
} Else {
System. Out. println ("no any new mail! ");
Throw new runtimeexception ("no any new mail! ");
}
}

The number of mails counted after POP3/pop3s connection is incorrect. The IMAP statistics are correct. Very depressed ....

 

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.