package com.hz.mail;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
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.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import com.sun.mail.handlers.multipart_mixed;
/**
* @author 作者 是hz啊:
* @version 创建时间:2020年5月24日下午2:17:32 类说明
*/
public class StoreMail extends JFrame {
private static final long serialVersionUID = 1L;
private static JTextArea ta_text;
public final static String MAIL_SERVER_HOST = "pop.qq.com"; // 邮箱服务器
public final static String TYPE_HTML = "text/html;charset=UTF-8"; // 文本内容类型
public final static String MAIL_FROM = "[email protected]"; // 发件人
public final static String MAIL_TO = "[email protected]"; // 收件人
public StoreMail() {
// TODO Auto-generated constructor stub
setTitle("wemail");
getContentPane().setLayout(null); // 设置布局为空布局
setBounds(500, 500, 500, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel lab = new JLabel();
lab.setForeground(new Color(100, 149, 237));
lab.setFont(new Font("", Font.ROMAN_BASELINE, 24));
lab.setText("微邮箱");
lab.setBounds(200, 10, 230, 24);
getContentPane().add(lab);
final JLabel label2 = new JLabel();
label2.setText("接收邮件:");
label2.setBounds(22, 85, 66, 18);
getContentPane().add(label2);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(113, 85, 287, 360);
getContentPane().add(scrollPane);
ta_text = new JTextArea();
// 创建一个视口并设置其视图
scrollPane.setViewportView(ta_text);
ta_text.setLineWrap(true); // 激活自动换行功能
ta_text.setWrapStyleWord(true); // 激活断行不断字功能
final JButton btn = new JButton("确 定");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
recievemessage();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btn.setBounds(100, 480, 85, 28);
getContentPane().add(btn);
final JButton btn_back = new JButton();
btn_back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
new Menu();
}
});
btn_back.setText("返 回");
btn_back.setBounds(200, 480, 85, 28);
getContentPane().add(btn_back);
final JButton btn_exit = new JButton();
btn_exit.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
System.exit(0);
}
});
btn_exit.setText("退 出");
btn_exit.setBounds(300, 480, 85, 28);
getContentPane().add(btn_exit);
setVisible(true);
}
public void recievemessage() throws Exception {
String username = Login.tf_user.getText();
String password = new String(Login.tf_password.getPassword());
// 创建一个有具体连接信息的Properties对象
Properties prop = new Properties();
prop.setProperty("mail.debug", "true");
prop.setProperty("mail.store.protocol", "pop3");
prop.setProperty("mail.pop.host", MAIL_SERVER_HOST);
Session session = Session.getInstance(prop);
Store store = session.getStore();
store.connect(MAIL_SERVER_HOST, username, password);
// 获得邮箱内的邮件夹
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
// 获得邮件夹Folder内的所有邮件Message对象
Message[] messages = folder.getMessages();
ta_text.append("一共收到" + messages.length + "封邮件\n");
for (int i = 0; i < messages.length; i++) {
MimeMessage msg = (MimeMessage) messages[i];
ta_text.append("第 " + (i + 1) + "封邮件\n");
String subject = msg.getSubject();
String from = (msg.getFrom()[0]).toString();
ta_text.append("主题:" + subject + "\n");
ta_text.append("发件人地址:" + from + "\n");
ta_text.append("发件时间:" + getSentDate(msg, null) + "\n");
if (isContainAttachment(null)) {
System.out.println("包含附件\n" );
}
//String content = messages[i].getContent().toString();
StringBuffer content = new StringBuffer(30);
getMailTextContent(msg, content);
ta_text.append("邮件的内容:" + content + "\n");
Multipart mp = (Multipart) messages[i].getContent();
for (int j = 0; j < mp.getCount(); j++) {
BodyPart bp = mp.getBodyPart(j);
if (bp.getDisposition() != null) {
String fileName = bp.getFileName();
if (fileName.startsWith("=?")) {
fileName = MimeUtility.decodeText(fileName); // 需要解析中文名称的文件名(将中文名称加入邮件时用encodeText)
}
ta_text.append("附件:" + fileName);
}
}
ta_text.append("\n");
System.out.println("第 " + (i + 1) + "封邮件的主题:" + subject);
// System.out.println("第 " + (i + 1) + "封邮件的发件人地址:" + from);
// System.out.println("第 " + (i + 1) + "封邮件的内容:" + content);
}
JOptionPane.showMessageDialog(null, "邮件查询成功!");
// 5、关闭
folder.close(false);
store.close();
}
/**
* 获取邮件文本内容
*/
public static void getMailTextContent(Part part, StringBuffer content) throws MessagingException, IOException {
//通过getContent 方法获取文本类型的附件
boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
if (part.isMimeType("text/*") && !isContainTextAttach) {
content.append(part.getContent().toString());
} else if (part.isMimeType("message/rfc822")) {
getMailTextContent((Part)part.getContent(),content);
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
getMailTextContent(bodyPart,content);
}
}
}
/**
* 获得邮件发送时间
* @param msg 邮件内容
* @return yyyy年mm月dd日 星期X HH:mm
* @throws MessagingException
*/
public static String getSentDate(MimeMessage msg, String pattern) throws MessagingException {
Date receivedDate = msg.getSentDate();
if (receivedDate == null)
return "";
if (pattern == null || "".equals(pattern))
pattern = "yyyy年MM月dd日 E HH:mm ";
return new SimpleDateFormat(pattern).format(receivedDate);
}
/**
* 判断邮件中是否包含附件
* @param msg 邮件内容
* @return 邮件中存在附件返回true,不存在返回false
* @throws MessagingException
* @throws IOException
*/
public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
boolean flag = false;
if (part.isMimeType("multipart/*")) {
MimeMultipart multipart = (MimeMultipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBody