// ä»EMLæ件å¾å°MimeMessage对象
MimeMessage message = new MimeMessage(session, new FileInputStream(emlFile));
public static String getMailSubject(Message message) throws Exception {
return MimeUtility.decodeText(message.getSubject());
}
public static String getMailSender(Message message) throws Exception {
String emailSender = null;
Address[] addresses = message.getFrom();
if (addresses == null || addresses.length < 1) {
throw new IllegalArgumentException("该é®ä»¶æ²¡æå件人");
}
// è·å¾å件人
InternetAddress address = (InternetAddress) addresses[0];
String senderName = address.getPersonal();
if (senderName != null) {
senderName = MimeUtility.decodeText(senderName);
emailSender = senderName + "<" + address.getAddress() + ">";
} else {
senderName = address.getAddress();
}
return emailSender;
}
public static String getMailRecipients(Message message, Message.RecipientType recipientType) throws Exception {
StringBuilder builder = new StringBuilder();
Address[] addresses = null;
if (recipientType == null) {
addresses = message.getAllRecipients();
} else {
addresses = message.getRecipients(recipientType);
}
if (addresses == null || addresses.length < 1) {
throw new IllegalArgumentException("该é®ä»¶æ²¡ææ¶ä»¶äºº");
}
for (Address address : addresses) {
InternetAddress iAddress = (InternetAddress) address;
builder.append(iAddress.toUnicodeString()).append(", ");
}
return builder.deleteCharAt(builder.length() - 1).toString();
}
public static String getMailSendDate(Message message, String pattern) throws Exception {
String sendDateString = null;
if (pattern == null || "".equals(pattern.trim())) {
pattern = "yyyyå¹´MMæddæ¥ E HH:mm";
}
Date sendDate = message.getSentDate();
sendDateString = new SimpleDateFormat(pattern).format(sendDate);
return sendDateString;
}
public static boolean containsAttachment(Part part) throws Exception {
boolean flag = false;
if (part != null) {
if (part.isMimeType("multipart/*")) {
MimeMultipart mp = (MimeMultipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bodyPart = mp.getBodyPart(i);
String disposition = bodyPart.getDisposition();
if (disposition != null && (Part.ATTACHMENT.equalsIgnoreCase(disposition)
|| Part.INLINE.equalsIgnoreCase(disposition))) {
flag = true;
} else if (bodyPart.isMimeType("multipart/*")) {
flag = containsAttachment(bodyPart);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("application") != -1) {
flag = true;
}
if (contentType.indexOf("name") != -1) {
flag = true;
}
}
if (flag)
break;
}
} else if (part.isMimeType("message/rfc822")) {
flag = containsAttachment((Part) part.getContent());
}
}
return flag;
}
public static boolean isSeen(Message message) throws Exception {
if (message == null) {
throw new MessagingException("Message is empty");
}
return message.getFlags().contains(Flags.Flag.SEEN);
}
public static boolean isReplaySign(Message message) throws Exception {
if (message == null) {
throw new MessagingException("Message is empty");
}
boolean replaySign = false;
String[] headers = message.getHeader("Disposition-Notification-To");
if (headers != null && headers.length > 0) {
replaySign = true;
}
return replaySign;
}
public static String getMailPriority(Message message) throws Exception {
if (message == null) {
throw new MessagingException("Message is empty");
}
String priority = "æ®é";
String[] headers = message.getHeader("X-Priority");
if (headers != null && headers.length > 0) {
String mailPriority = headers[0];
if (mailPriority.indexOf("1") != -1 || mailPriority.indexOf("High") != -1) {
priority = "ç´§æ¥";
} else if (mailPriority.indexOf("5") != -1 || mailPriority.indexOf("Low") != -1) {
priority = "ä½";
} else {
priority = "æ®é"; // 3æè
Normal;
}
}
return priority;
}
public static void getMailTextContent(Part part, StringBuilder content) throws Exception {
if (part == null) {
throw new MessagingException("Message content is empty");
}
boolean containsTextInAttachment = part.getContentType().indexOf("name") > 0;
if (part.isMimeType("text/*") && containsTextInAttachment) {
content.append(part.getContent().toString());
} else if (part.isMimeType("message/rfc822")) {
getMailTextContent((Part) part.getContent(), content);
} else if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bodyPart = mp.getBodyPart(i);
getMailTextContent(bodyPart, content);
}
} else if (part.isMimeType("image/*")) {
// TODO part.getInputStream()è·å¾è¾å
¥æµç¶åè¾åºå°æå®çç®å½
} else {
// TODO å
¶å®ç±»åçcontentType, æªåå¤ç, ç´æ¥è¾åº
content.append(part.getContent().toString());
}
}
public static void saveAttachment(Part part, String destDir) throws Exception {
if (part == null) {
throw new MessagingException("part is empty");
}
// å¤æçé®ä»¶å
å«å¤ä¸ªé®ä»¶ä½
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
// éåæ¯ä¸ä¸ªé®ä»¶ä½
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bodyPart = mp.getBodyPart(i);
// bodyPartä¹å¯è½æå¤ä¸ªé®ä»¶ä½ç»æ
String disposition = bodyPart.getDisposition();
if (disposition == null && (Part.ATTACHMENT.equalsIgnoreCase(disposition)
|| Part.INLINE.equalsIgnoreCase(disposition))) {
InputStream in = bodyPart.getInputStream();
saveFile(in, destDir, decodeText(bodyPart.getFileName()));
} else if (bodyPart.isMimeType("multipart/*")) {
saveAttachment(bodyPart, destDir);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) {
saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName()));
}
}
}
} else if (part.isMimeType("message/rfc822")) {
saveAttachment((Part) part.getContent(), destDir);
}
}
public static void saveFile(InputStream in, String destDir, String fileName) throws Exception {
FileOutputStream out = new FileOutputStream(new File(destDir + fileName));
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
out.close();
in.close();
}
public static String decodeText(String encodedText) throws Exception {
if (encodedText == null || "".equals(encodedText.trim())) {
return "";
} else {
return MimeUtility.decodeText(encodedText);
}
}
追é®è¿æ¯æ¨ææåcharsetç代ç å
æ¯å¦æä¸é¢è¿ä¹ä¸ä¸ªå符串
Content-Type: text/plain;
charset="gb2312"
Content-Transfer-Encoding: base64
æååºcharset å encodingï¼è¯¥æä¹åï¼
è¿éæ¶åå°ä¸ä¸ªé®é¢ï¼æçå¨çå·ä¸¤è¾¹æç©ºæ ¼ï¼æç人使ç¨åå¼å·ï¼æç人å大å
该æä¹åè½æ£ç¡®æ 误çè·å gb2312å¢
追çå¯ä»¥çï¼MimeMessageæä¸ä¸ªgetHeader('')çæ¹æ³å¯ä»¥ç¨äºè§£æé®ä»¶çheader, 解æåºæ¥çå符串对äºä¸åæµè§å¨å¯è½è¿æé®é¢ï¼è¿ä¸ªè¦é对ä¸åçæ
åµäºï¼æ好åä¸ä¸ªæ£å表达å¼å¤æ