JavaMail 读取 Gmail 邮件的问题

最近在写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! " );
}
}

使用POP3/POP3s连接后统计出来的邮件数目都是不对的。而使用IMAP统计的就正确了。很郁闷。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值