利用JavaMail通过POP3协议、IMAP协议收取Gmail邮箱
来源:互联网 发布:阿里云 搭建ss 编辑:程序博客网 时间:2024/05/14 17:01
POP3和IMAP的区别到处都有,这里不再敖述,只通过例子说明如下:
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";// Get a Properties objectProperties 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.pop3.disabletop", "true");props.setProperty("mail.pop3.ssl.enable", "true");props.setProperty("mail.pop3.useStartTLS", "true");// 以下步骤跟一般的JavaMail操作相同Session session = Session.getDefaultInstance(props, null);URLName urln = new URLName("pop3", "pop.gmail.com", 995, null, username, password);Store store = session.getStore(urln);store.connect();
Folder inbox = null;try {store.connect();inbox = store.getFolder("INBOX");inbox.open(Folder.READ_ONLY);FetchProfile profile = new FetchProfile();profile.add(FetchProfile.Item.ENVELOPE);//profile.add(FetchProfile.Item.CONTENT_INFO);//profile.add(FetchProfile.Item.FLAGS);profile.add(UIDFolder.FetchProfileItem.UID);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());//邮件IDString uid = ((POP3Folder)inbox).getUID(messages[i]);System.out.println("UID:"+ uid);//收件人String to = decodeText(messages[i].getAllRecipients()[0].toString());InternetAddress iaTo = new InternetAddress(to);System.out.println("TO:" + iaTo.getPersonal() + '(' + iaTo.getAddress() + ')');//内容//String disposition; //Multipart mp = (Multipart) messages[i].getContent(); //int mpCount = mp.getCount(); //System.out.println("");//for (int m = 0; m < mpCount; m++){ ////BodyPart part = mp.getBodyPart(m); //disposition = part.getDisposition(); //if (disposition != null && disposition.equals(Part.ATTACHMENT)){ ////saveAttach(part,Constant.ZIP_DIR); //} else{ ////System.out.println(part.getContent()); //} //}File f = new File("/mail/test"+uid+".eml");messages[i].writeTo(new FileOutputStream(f)); }} finally {try {inbox.close(false);} catch (Exception e) {}try {store.close();} catch (Exception e) {}}
IMAP的见另一篇
- 利用JavaMail通过POP3协议、IMAP协议收取Gmail邮箱
- javamail使用IMAP协议收取gmail邮件
- 收取邮件的协议 : pop3 exchange imap
- gmail,Qmail,163邮箱的IMAP/SMTP/POP3地址及协议端口
- pop3协议收取邮件
- 邮箱系统原理——POP3协议和IMAP协议
- pop3,stmp,imap协议
- SMTP、POP3、IMAP协议
- 关于邮箱的三种协议POP3、SMTP和IMAP
- 邮件协议POP3/IMAP/SMTP服务的区别 常用邮箱的 IMAP/POP3/SMTP 设置
- 在使用JavaMail实现imap协议收取各种邮箱服务器的邮件的时候,碰到了几个比较郁闷的问题
- smtp,POP3,IMAP协议解释
- SMTP、POP3、IMAP协议解释
- SMTP、POP3、IMAP协议解释
- SMTP、POP3、IMAP协议解释
- SMTP、POP3、IMAP协议解释
- SMTP,POP3,IMAP 邮件协议
- 邮件协议SMTP/POP3/IMAP
- 中国环境一号卫星
- 验证索引列NULL不走索引
- 杭电 1856 并查集路径压缩+按秩合并
- 关于Foxit Phantom 2.0 Suite 的问题
- ICMP重定向
- 利用JavaMail通过POP3协议、IMAP协议收取Gmail邮箱
- php伪静态介绍
- Ruby编程语言_Chapter02_笔记
- 上传图片
- C语言函数参数调用顺序
- Oracle GoldenGate Veridata Installation and Configuration
- Python3之"IndentationError: unexpected indent"错误
- handler/ runOnUiThread /AsyncTask
- php的类