import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
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.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
/**
* 有一封邮件就需要建立一个ReciveMail对象
*/
public class ReciveMail {
private MimeMessage mimeMessage = null;
private String saveAttachPath = ""; //附件下载后的存放目录
private StringBuffer bodytext = new StringBuffer();//存放邮件文本内容
private StringBuffer bodyhtml = new StringBuffer();//存放邮件内容
private String dateformat = "yy-MM-dd HH:mm"; //默认的日前显示格式
public ReciveMail(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
public void setMimeMessage(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}
/**
* 获得发件人的地址和姓名
*/
public String getFrom() throws Exception {
InternetAddress address[] = (InternetAddress[]) mimeMessage.getFrom();
String from = address[0].getAddress();
if (from == null)
from = "";
String personal = address[0].getPersonal();
if (personal == null)
personal = "";
String fromaddr = personal + "<" + from + ">";
return fromaddr;
}
/**
* 获得邮件的收件人,抄送,和密送的地址和姓名,根据所传递的参数的不同 "to"----收件人 "cc"---抄送人地址 "bcc"---密送人地址
*/
public String getMailAddress(String type) throws Exception {
String mailaddr = "";
String addtype = type.toUpperCase();
InternetAddress[] address = null;
if (addtype.equals("TO") || addtype.equals("CC")
|| addtype.equals("BCC")) {
if (addtype.equals("TO")) {
address = (InternetAddress[]) mimeMessage
.getRecipients(Message.RecipientType.TO);
} else if (addtype.equals("CC")) {
address = (InternetAddress[]) mimeMessage
.getRecipients(Message.RecipientType.CC);
} else {
address = (InternetAddress[]) mimeMessage
.getRecipients(Message.RecipientType.BCC);
}
if (address != null) {
for (int i = 0; i < address.length; i++) {
String email = address[i].getAddress();
if (email == null)
email = "";
else {
email = MimeUtility.decodeText(email);
}
String personal = address[i].getPersonal();
if (personal == null)
personal = "";
else {
personal = MimeUtility.decodeText(personal);
}
String compositeto = personal + "<" + email + ">";
mailaddr += "," + compositeto;
}
mailaddr = mailaddr.substring(1);
}
} else {
throw new Exception("Error emailaddr type!");
}
return mailaddr;
}
/**
* 获得邮件主题
*/
public String getSubject() throws MessagingException {
String subject = "";
try {
subject = MimeUtility.decodeText(mimeMessage.getSubject());
if (subject == null)
subject = "";
} catch (Exception exce) {
}
return subject;
}
/**
* 获得邮件发送日期
*/
public String getSentDate() throws Exception {
Date sentdate = mimeMessage.getSentDate();
SimpleDateFormat format = new SimpleDateFormat(dateformat);
return format.format(sentdate);
}
/**
* 获得邮件正文内容
*/
public String getBodyText() {
return bodytext.toString();
}
/**
* 获得邮件正文Html内容
*/
public String getBodyHtml() {
return bodyhtml.toString();
}
/**
* 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析
*/
public void getMailContent(Part part) throws Exception {
String contenttype = part.getContentType();
int nameindex = contenttype.indexOf("name");
boolean conname = false;
if (nameindex != -1)
conname = true;
// System.out.println("CONTENTTYPE: " + contenttype);
if (part.isMimeType("text/plain") && !conname) {//是纯文本
bodytext.append((String) part.getContent());
} else if (part.isMimeType("text/html") && !conname) { //是网页
bodyhtml.append((String) part.getContent());
} else if (part.isMimeType("multipart/*")) { //文本和附件
Multipart multipart = (Multipart) part.getContent();
int counts = multipart.getCount();
for (int i = 0; i < counts; i++) {
getMailContent(multipart.getBodyPart(i));
}
} else if (part.isMimeType("message/rfc822")) {//是附件
getMailContent((Part) part.getContent());
} else {
}
}
/**
* 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false"
*/
public boolean getReplySign() throws MessagingException {
boolean replysign = false;
String needreply[] = mimeMessage
.getHeader("Disposition-Notification-To");
if (needreply != null) {
replysign = true;
}
return replysign;
}
/**
* 获得此邮件的Message-ID
*/
public String getMessageId() throws MessagingException {
return mimeMessage.getMessageID();
}
/**
* 【判断此邮件是否已读,如果未读返回返回false,反之返回true】
*/
public boolean isNew() throws MessagingException {
boolean isnew = false;
Flags flags = ((Message) mimeMessage).getFlags();
Flags.Flag[] flag = flags.getSystemFlags();
System.out.println("flags's length: " + flag.length);
for (int i = 0; i < flag.length; i++) {
if (flag[i] == Flags.Flag.SEEN) {
isnew = true;
System.out.println("seen Message .");
break;
}
}
return isnew;
}
/**
* 判断此邮件是否包含附件
*/
public boolean isContainAttach(Part part) throws Exception {
boolean attachflag = false;
//String contentType = part.getContentType();
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart mpart = mp.getBodyPart(i);
String disposition = mpart.getDisposition();
if ((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE))))
attachflag = true;
else if (mpart.isMimeType("multipart/*")) {
attachflag = isContainAttach((Part) mpart);
向晨宇
- 粉丝: 294
- 资源: 30
最新资源
- BP神经网络的数据分类预测和故障信号诊断分类matlab代码 ,直接运行出数据分类结果和误差分布,注释详细易读懂,可直接套数据运行
- 2023-04-06-项目笔记 - 第三百七十九阶段 - 4.4.2.377全局变量的作用域-377 -2025.01.15
- Modbus RTU通讯S7-1200主站程序 RS4585总线通讯 TIA博图SCL源码语言编程.可用于西门子S7-1200PLC.S7-1500PLC.简单实用,轻松实现对30个从站数据的读写
- 十二月十五日学习其中之一的笔记
- Allegro PCB封装库(分类、命名很规范,已验证) 该PCB库特点一:类型齐全,包含有阻容感分立元器件;SOIC(SO,SOP,SSOP,TSOP,TSSOP);QFN;QFP(PQFP,SQF
- 十二月十五日的学习成果
- STM32F103C8T6学习笔记,GPIO输入与输出
- 计算机专业实践操作课题笔记
- 完整yolov11项目
- 四轮毂电机驱动,分布式驱动力分配,滑模控制,基于simulink分布式驱动转矩分配控制,采用七自由度车辆模型作为仿真模型,采用分层控制思想,上层为目标控制参数,采用二自由度模型计算理想横摆角速度,中层
- 埃斯顿量产方案绝对值编码器伺服控制器全C代码和硬件图纸 1)TMS320F2812+FPGA量产方案;DSP全C代码,VHDL语言FPGA代码 2)绝对值编码器,或者旋变 3)AD格式硬件电路图
- Python 实现 PSO-IELM(粒子群优化改进极限学习机)在回归和分类预测中的详细示例(含完整的程序,GUI设计和代码详解)
- C#程序源码 Modbus RS485 RTU 主站源码程序 用VS2019 C#编写 功能码支持01 02 03 04 05 06 0F 10 经过测试可以与RS485从站设备进行通
- 扫雷游戏网页版全源 by.久孤2024CSDN
- 课程设计基于Python+Flask+MySQL的图书管理系统源码+数据库(高分项目)
- Apex APRS 是一个不同的新 APRS 客户端应用程序。主要特点:在线和离线缓存的地图查看来自多个热门来源快速、简单、直观且强大的用户界面.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页