java 解析 eml的源代码

有没有现成的解析eml文本格式的java程序
也就是解析邮件的java程序

主要想查看一下程序是如何提取附件,如何提取出charset,encoding等等字段的

// ä»Ž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, 解析出来的字符串对于不同浏览器可能还有问题,这个要针对不同的情况了,最好写一个正则表达式判断

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-12-07
public void setTemperature(Integer temperature) {
oldT = t;
t = temperature;
logger.error( “ Temperature set to {}. Old temperature was {}. “ , t, oldT);
if (temperature.intValue() > 50 ) {
logger.info( “ Temperature has risen above 50 degrees. “ );
}
}
public static void main(String[] args) {
Wombat wombat = new Wombat();
wombat.setTemperature( 1 );
wombat.setTemperature( 55 );
相似回答