您的位置:首页 > 编程语言 > Java开发

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


源码下载:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: