本篇文章给大家谈谈javamail读取并解析eml文件,以及java邮件全部解析 eml对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、Delphi7读取eml邮件文件用什么方法好2、java 解析 eml的源代码3、java读取本地*.msg如何获取收件人4、怎么用java生成一个.eml文件
Delphi7读取eml邮件文件用什么方法好
直接读取文件内容,然后解析。大部分EML文件是base64编码的字符串,你可以自己解析它。读取eml文件的类应该也有。
java 解析 eml的源代码
// 从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);
}
}
java读取本地*.msg如何获取收件人
1、可以当String来解析
2、java.mail , MimeMessage 再取相关的——应该是.eml文件。
怎么用java生成一个.eml文件
.eml文件是邮件文件,使用javamail发送邮件,在发送邮件的时候,将邮件保存在本地即可得到.eml文件
javamail读取并解析eml文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java邮件全部解析 eml、javamail读取并解析eml文件的信息别忘了在本站进行查找喔。