java email操作demo和解析eml文件
来源:互联网 发布:如何精通三坐标编程 编辑:程序博客网 时间:2024/06/08 05:48
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>() {@Overridepublic 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); } }}
源码下载:
0 0
- java email操作demo和解析eml文件
- Java解析eml文件
- Java解析eml内容和附件
- python 解析 eml文件
- C#解析eml文件
- C# 操作.eml文件
- java eml解析
- java读取eml文件
- php mailparse 解析 .eml 文件
- java讀取eml文件
- java发送email Demo
- Java 文件操作简单demo
- 近期目标---解析eml
- java 本地生成带附件的eml文件
- java 邮件操作 email
- java 发送email文件
- 读取eml格式文件,并解析
- 关于eml 解析的学习
- 034.== 两个变量是否相等
- JAVA多线程和并发基础面试问答
- Linux里如何查找文件内容
- 基于攻击链的威胁感知系统
- 【Qt】QString向C/C++转换
- java email操作demo和解析eml文件
- LeetCode 95: Unique Binary Search Trees II
- 151027总结
- MySQL 忘了root密码怎么办
- linux centos python scrapy 环境配置
- MQTT学习笔记-Mosquitto的安装与启动
- C语言 二维数组动态分配和释放
- 开发板与电脑连接局域网
- 6.5 插入符号(不是光标)