java email操作demo和解析eml文件
2015-10-27 16:13
603 查看
package com.nerve.core.test.email; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Part; import javax.mail.Session; import javax.mail.internet.MimeMessage; //http://www.yiibai.com/javamail_api/javamail_api_sending_simple_email.html //java email操作demo和解析eml文件。 public class eml { public static void main(String args[]) throws Exception { Files.walkFileTree(Paths.get("G:\\测试数据\\hehe\\eml"), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.toFile().getAbsolutePath().endsWith(".eml")) { test(file.toFile().getAbsolutePath()); } return super.visitFile(file, attrs); } }); } public static void test(String emlPath) { try { System.out.println(emlPath); Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); InputStream inMsg; inMsg = new FileInputStream(emlPath); Message msg = new MimeMessage(session, inMsg); String[] date = msg.getHeader("Date"); // String[] from= // String[] to= if (msg.isMimeType("multipart/*") || msg.isMimeType("MULTIPART/*")) { System.out.println("multipart"); Multipart mp = (Multipart) msg.getContent(); int totalAttachments = mp.getCount(); if (totalAttachments > 0) { for (int i = 0; i < totalAttachments; i++) { Part part = mp.getBodyPart(i); String s = getMailContent(part); String attachFileName = part.getFileName(); String disposition = part.getDisposition(); String contentType = part.getContentType(); if ((attachFileName != null && attachFileName .endsWith(".ics")) || contentType.indexOf("text/calendar") >= 0) { String[] dateHeader = msg.getHeader("date"); } System.out.println(s); System.out.println(attachFileName); System.out.println(disposition); System.out.println(contentType); } inMsg.close(); } } else { System.out.println("part"); System.out.println(msg.getContent().toString()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static String getMailContent(Part part) throws Exception { String contenttype = part.getContentType(); int nameindex = contenttype.indexOf("name"); boolean conname = false; if (nameindex != -1) { conname = true; } StringBuilder bodytext = new StringBuilder(); if (part.isMimeType("text/plain") && !conname) { bodytext.append((String) part.getContent()); } else if (part.isMimeType("text/html") && !conname) { bodytext.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 { } return bodytext.toString(); } }
package com.nerve.core.test.email; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import com.nerve.commons.util.io.PropertiesLoader; public class SendAttachmentInEmail { public static void main(String[] args) { // Recipient's email ID needs to be mentioned. String to = "542335496@qq.com"; PropertiesLoader loader=new PropertiesLoader("email.properties"); final String username=loader.getProperty("username"); final String password=loader.getProperty("password"); // Sender's email ID needs to be mentioned String from = username; // Assuming you are sending email through relay.jangosmtp.net String host = "smtp.mail.yahoo.com"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "25"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Now set the actual message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "d:/fuck.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } } }
源码下载:
相关文章推荐
- eclipse、myeclipse 打开当前文件所在文件夹的方法
- 冒泡排序算法及其简单优化(基于Java)
- java Vamei快速教程02 方法和数据成员
- 我读过的几本java好书
- spring jdbc万能数据库封装类
- Java 枚举的详细说明
- Spring的特点
- java Vamei快速教程01
- Java嵌套类
- java Vamei快速教程00
- Java实现字符全阵列阵列
- spring/hibernate 优化
- Java获取文件后缀名
- maven中下载jar包源码和javadoc的命令介绍
- Java中的Enum的使用与分析
- Mac中下载JDK手动更新出现“只支持10.7.3以上的系统版本”问题解决方案
- java String字符串替换
- Maven搭建SpringMVC+Hibernate项目详解
- Spring AOP基础
- java获得当前系统信息和服务器信息