双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

No provider for pop3

wd9053 wd9053 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
想从gmail中收取邮件,但总是报错。classpath设置无误,可以用gmail发送邮件
Exception in thread "main" javax.mail.NoSuchProviderException:No provider for pop3
请大家帮忙找出错误原因
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;

/**
* 用于收取Gmail邮件
* @author Winter Lau
*/
public class MailFetch
{
public static void main(String argv[]) throws Exception
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

// Get a Properties object
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.put("mail.pop3.host", "pop.gmail.com");
//以下步骤跟一般的JavaMail操作相同
Session session = Session.getDefaultInstance(props,null);

//请将红色部分对应替换成你的邮箱帐号和密码
URLName urln = new URLName("pop3","pop.gmail.com",995,null,
"账号", "密码");
Store store = session.getStore(urln);////////、、//运行在该行时报错
Folder inbox = null;
try
{
store.connect();
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
Message[] messages = inbox.getMessages();
inbox.fetch(messages, profile);
System.out.println("收件箱的邮件数:" + messages.length);
for (int i = 0; i < messages.length; i++)
{
//邮件发送者
String from = decodeText(messages[i].getFrom()[0].toString());
InternetAddress ia = new InternetAddress(from);
System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');
//邮件标题
System.out.println("TITLE:" + messages[i].getSubject());
//邮件大小
System.out.println("SIZE:" + messages[i].getSize());
//邮件发送时间
System.out.println("DATE:" + messages[i].getSentDate());
}
}
finally
{
try
{
inbox.close(false);
}
catch (Exception e)
{

}
try
{
store.close();
}
catch (Exception e)
{

}
}
}

protected static String decodeText(String text) throws UnsupportedEncodingException
{
if (text == null)
return null;
if (text.startsWith("=?GB") || text.startsWith("=?gb"))
text = MimeUtility.decodeText(text);
else
text = new String(text.getBytes("ISO8859_1"));
return text;
}
}

...全文
评论 7
收藏 已收藏
分享
返回顶部
wd9053
Java SE 62,621 成员 307,251 内容