最近在写JavaMail接收Gmail邮件的NetBeans插件,发现一个问题。。。。
下面是连接邮件账户与接收邮件的代码片断:
/**
*conncecttothegmailaccount.
* @param userNameusername
* @param userPassworduserpassword
* @throws Exception
*/
private static void connect(StringuserName,StringuserPassword)
throws Exception{
Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider());
final StringSSL_FACTORY = " javax.net.ssl.SSLSocketFactory " ;
Propertiesprops = 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 " );
Sessionsession = Session.getDefaultInstance(props, null );
session.setDebug( true ); // setdebug
// URLNameurln=newURLName("pop3s","pop.gmail.com",995,null,
// userName,userPassword);
URLNameurln = new URLName( " imap " , " imap.gmail.com " , 993 , null ,userName,
userPassword);
store = session.getStore(urln);
store.connect();
}
/**
*Retieveallnewmessages.
* @param userNameusername
* @param userPassworduserpassword
* @return allnewmessages
* @throws Exception
*/
public Message[]getNewMails(StringuserName,StringuserPassword)
throws Exception{
if ( ! store.isConnected()){
connect(userName,userPassword);
}
Folderinbox = store.getFolder( " Inbox " );
inbox.open(Folder.READ_ONLY);
FetchProfileprofile = 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( " Noanynewmail! " );
throw new RuntimeException( " Noanynewmail! " );
}
}
*conncecttothegmailaccount.
* @param userNameusername
* @param userPassworduserpassword
* @throws Exception
*/
private static void connect(StringuserName,StringuserPassword)
throws Exception{
Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider());
final StringSSL_FACTORY = " javax.net.ssl.SSLSocketFactory " ;
Propertiesprops = 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 " );
Sessionsession = Session.getDefaultInstance(props, null );
session.setDebug( true ); // setdebug
// URLNameurln=newURLName("pop3s","pop.gmail.com",995,null,
// userName,userPassword);
URLNameurln = new URLName( " imap " , " imap.gmail.com " , 993 , null ,userName,
userPassword);
store = session.getStore(urln);
store.connect();
}
/**
*Retieveallnewmessages.
* @param userNameusername
* @param userPassworduserpassword
* @return allnewmessages
* @throws Exception
*/
public Message[]getNewMails(StringuserName,StringuserPassword)
throws Exception{
if ( ! store.isConnected()){
connect(userName,userPassword);
}
Folderinbox = store.getFolder( " Inbox " );
inbox.open(Folder.READ_ONLY);
FetchProfileprofile = 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( " Noanynewmail! " );
throw new RuntimeException( " Noanynewmail! " );
}
}
使用POP3/POP3s连接后统计出来的邮件数目都是不对的。而使用IMAP统计的就正确了。很郁闷。。。。