package com.jumooc.mail;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/** * 邮件接受测试 * */
/** * 使用POP3协议接收邮件 */
public class POP3ReceiveMailTest {
public static void main(String[] args) throws Exception {
resceive();
}
/**
* 接收邮件
* */
public static void resceive() throws Exception {
//因为现在使用的是163邮箱 而163的 pop地址是 pop3.163.com 端口是 110
//比如使用好未来企业邮箱 就需要换成 好未来邮箱的 pop服务器地址 pop.263.net 和 端口 110
String duankou = "110"; // 端口号
String servicePath = "pop3.163.com"; // 服务器地址
// 准备连接服务器的会话信息
Properties props = new Properties();
props.setProperty("mail.store.protocol", "pop3");
// 使用pop3协议
props.setProperty("mail.pop3.port", duankou);
// 端口
props.setProperty("mail.pop3.host", servicePath);
// pop3服务器
// 创建Session实例对象
Session session = Session.getInstance(props);
Store store = session.getStore("pop3");
store.connect("17839673714@163.com", "liliang000");
//163邮箱程序登录属于第三方登录所以这里的密码是163给的授权密码而并非普通的登录密码
// 获得收件箱
Folder folder = store.getFolder("INBOX");
/* Folder.READ_ONLY:只读权限
* * Folder.READ_WRITE:可读可写(可以修改邮件的状态)
* */
folder.open(Folder.READ_WRITE);
//打开收件箱
// 由于POP3协议无法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数
System.out.println("未读邮件数: " + folder.getUnreadMessageCount());
// 由于POP3协议无法获知邮件的状态,所以下面得到的结果始终都是为0
System.out.println("删除邮件数: " + folder.getDeletedMessageCount());
System.out.println("新邮件: " + folder.getNewMessageCount());
// 获得收件箱中的邮件总数
System.out.println("邮件总数: " + folder.getMessageCount());
// 得到收件箱中的所有邮件,并解析
Message[] messages = folder.getMessages();
parseMessage(messages);
//得到收件箱中的所有邮件并且删除邮件
deleteMessage(messages);
//释放资源
folder.close(true);
store.close();
}
/**
* 解析邮件
* @param messages 要解析的邮件列表
*/
public static void parseMessage(Message... messages) throws MessagingException, IOException {
if (messages == null || messages.length < 1)
throw new MessagingException("未找到要解析的邮件!");
// 解析所有邮件
for (int i = 0, count = messages.length; i < count; i++) {
MimeMessage msg = (MimeMessage) messages[i];
System.out.println("------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- ");
System.out.println("主题: " + getSubject(msg));
System.out.println("发件人: " + getFrom(msg));
System.out.println("收件人:" + getReceiveAddress(msg, null));
System.out.println("发送时间:" + getSentDate(msg, null));
System.out.println("是否已读:" + isSeen(msg));
System.out.println("邮件优先级:" + getPriority(msg));
System.out.println("是否需要回执:" + isReplySign(msg));
System.out.println("邮件大小:" + msg.getSize() * 1024 + "kb");
boolean isContainerAttachment = isContainAttachment(msg);
System.out.println("是否包含附件:" + isContainerAttachment);
if (isContainerAttachment) {
saveAttachment(msg, "d:\\testMail\\" + msg.getSubject() + "_" + i + "_");
// 保存附件
}
StringBuffer content = new StringBuffer(30);
getMailTextContent(msg, content);
System.out.println("邮件正文:" + (content.length() > 100 ? content.substring(0, 100) + "..." : content));
System.out.println("------------------第" + msg.getMessageNumber() + "封邮件解析结束-------------------- ");
System.out.println();
}
}
/**
* * 解析邮件 * @param messages 要解析的邮件列表
*/
public static void deleteMessage(Message... messages) throws MessagingException, IOException {
if (messages == null || messages.length < 1)
throw new MessagingException("未找到要解析的邮件!");
// 解析所有邮件
for (int i = 0, count = messages.length; i < count; i++) {
/**
* * 邮件删除
*/
Message message = messages[i];
String subject = message.getSubject();
// set the DELETE flag to true
message.setFlag(Flags.Flag.DELETED, true);
System.out.println("Marked DELETE for message: " + subject);
}
}
/**
* * 获得邮件主题 * @param msg 邮件内容 * @return 解码后的邮件主题
*/
public static String getSubject(MimeMessage msg) throws UnsupportedEncodingException, MessagingException {
return MimeUtility.decodeText(msg.getSubject());
}
/**
* * 获得邮件发件人 * @param msg 邮件内容 * @return 姓名 <Email地址> * @throws
* MessagingException * @throws UnsupportedEncodingException
*/
public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException {
String from = "";
Address[] froms = msg.getFrom();
if (froms.length < 1)
throw new MessagingException("没有发件人!");
InternetAddress address = (InternetAddress) froms[0];
String person = address.getPersonal();
if (person != null) {
person = MimeUtility.decodeText(person) + " ";
} else {
person = "";
}
from = person + "<" + address.getAddress() + ">";
return from;
}
/**
* * 根据收件人类型,获取邮件收件人、抄送和密送地址。如果收件人类型为空,则获得所有的收件人 *
* <p>
* Message.RecipientType.TO 收件人
* </p>
* *
* <p>
* Message.RecipientType.CC 抄送
* </p>
* *
* <p>
* Message.RecipientType.BCC 密送
* </p>
* * @param msg 邮件内容 * @param type 收件人类型 * @return 收件人1 <邮件地址1>, 收件人2 <邮件地址2>,
* ... * @throws MessagingException
*/
public static String getReceiveAddress(MimeMessage msg, Message.RecipientType type) throws MessagingException {
StringBuffer receiveAddress = new StringBuffer();
Address[] addresss = null;
if (type == null) {
addresss = msg.getAllRecipients();
} else {
addresss = msg.getRecipients(type);
}
if (addresss == null || addresss.length < 1)
throw new MessagingException("没有收件人!");
for (Address address : addresss) {
InternetAddress internetAddress = (InternetAddress) address;
receiveAddress.append(inte