java 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 

throw new IllegalArgumentException("该邮件没有发件人");

}

// 获得发e5a48de588b63231313335323631343130323136353331333337626137件人

InternetAddress address = (InternetAddress) addresses[0];

String senderName = address.getPersonal();

if (senderName != null) {

senderName = MimeUtility.decodeText(senderName);

emailSender = senderName + "";

} 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 

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 

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 

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 

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);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用JavaMail API来读取和处理eml文件JavaMail API是一个用于发送、接收和处理电子邮件的Java标准扩展。下面是一个简单的示例代码,演示如何读取eml文件: ```java import javax.mail.*; import javax.mail.internet.MimeMessage; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class ReadEmlFile { public static void main(String[] args) { // 读取eml文件路径 String emlFilePath = "path/to/eml/file.eml"; // 创建Properties对象,用于配置JavaMail会话 Properties properties = new Properties(); properties.setProperty("mail.store.protocol", "pop3"); properties.setProperty("mail.pop3.host", "pop.gmail.com"); properties.setProperty("mail.pop3.port", "995"); properties.setProperty("mail.pop3.ssl.enable", "true"); try { // 创建会话 Session session = Session.getDefaultInstance(properties); // 创建邮件存储对象 Store store = session.getStore("pop3s"); store.connect("your-email@gmail.com", "your-password"); // 打开文件输入流 FileInputStream emlFileInputStream = new FileInputStream(emlFilePath); // 创建MimeMessage对象 MimeMessage message = new MimeMessage(session, emlFileInputStream); // 获取邮件内容 String subject = message.getSubject(); String from = message.getFrom()[0].toString(); String content = message.getContent().toString(); // 输出邮件内容 System.out.println("Subject: " + subject); System.out.println("From: " + from); System.out.println("Content: " + content); // 关闭文件输入流 emlFileInputStream.close(); // 关闭邮件存储对象 store.close(); } catch (MessagingException | IOException e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,首先需要设置JavaMail会话的配置信息,包括邮件服务器的协议、主机、端口和SSL设置。然后创建会话对象和邮件存储对象,通过调用`connect`方法连接到邮件服务器。接下来,使用`FileInputStream`读取eml文件,并创建`MimeMessage`对象来解析eml文件内容。最后,可以通过`MimeMessage`对象获取邮件的主题、发件人和内容等信息。 请注意,上述示例代码中的邮件服务器配置信息是示例,你需要根据实际情况修改为你自己的邮件服务器配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值