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 ....