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
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
wemail.rar (28个子文件)
wemail
.project 588B
src
com
hz
mail
StoreMail.java 8KB
SendMail.java 8KB
Menu.java 6KB
Login.java 2KB
lib
activation.jar 55KB
javax.mail.jar 644KB
.settings
org.eclipse.wst.common.project.facet.core.xml 121B
org.eclipse.jdt.core.prefs 8KB
image
login.jpg 8KB
.classpath 547B
bin
com
hz
mail
Menu$4.class 990B
SendMail$1.class 1KB
SendMail.class 7KB
Menu.class 3KB
Login.class 3KB
StoreMail$2.class 723B
SendMail$3.class 717B
StoreMail.class 8KB
StoreMail$3.class 698B
Login$1.class 699B
Menu$3.class 1016B
Menu$2.class 1019B
Login$2.class 674B
SendMail$4.class 1KB
StoreMail$1.class 827B
SendMail$2.class 692B
Menu$1.class 1020B
共 28 条
- 1
资源评论
- 贼仙呐2023-07-28这个java简单电子邮件客户端非常实用,能够帮助我快速发送和接收邮件。
- 杜拉拉到杜拉拉2023-07-28该文件的设计简洁明了,操作界面友好,即使没有专业知识也可以轻松上手。
- 萱呀2023-07-28对于初学者来说,这个java简单电子邮件客户端是一个很好的学习资源,能够让他们更深入地理解邮件通信的原理。
- lirumei2023-07-28不得不说,这个文件的代码写得真是太棒了,可读性高,逻辑清晰,方便维护和扩展。
- 山林公子2023-07-28这个java电子邮件客户端的功能齐全,除了基本的邮件发送和接收外,还支持附件的上传和下载,非常方便。
lalalahz
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功