java mail eml_Javamail写带附件eml文件并调用Outlook

1 将邮件写入到文件的代码

msg.saveChanges();

File f = new File("d:/test.eml");

msg.writeTo(new FileOutputStream(f));

2 调用outlook的代码

Process p = Runtime.getRuntime().exec("cmd /C start msimn.exe /eml:d:/test.eml");

3 完整的代码如下

package code.jdk.mail;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Date;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Properties;

import java.util.Vector;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Address;

import javax.mail.AuthenticationFailedException;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

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 javax.mail.internet.MimeUtility;

public class EmailWriteToFile {

// 定义发件人、收件人、SMTP、用户名、密码、主题、内容等

private String displayName;

private String to;

private String from;

private String smtpServer;

private String username;

private String password;

private String subject;

private String content;

private boolean ifAuth; // 是否要身份认证

private String filename = "";

private Vector file = new Vector(); // 用于保存发送附件的文件名的集合

private String contentType = "text/html";

private String charset = "utf-8";

public void addFile(String filename) {

file.add(filename);

}

public String getContentType() {

return contentType;

}

public void setContentType(String contentType) {

this.contentType = contentType;

}

public String getCharset() {

return charset;

}

public void setCharset(String charset) {

this.charset = charset;

}

/**

* 设置SMTP服务器地址

*/

public void setSmtpServer(String smtpServer) {

this.smtpServer = smtpServer;

}

/**

* 设置发件人的地址

*/

public void setFrom(String from) {

this.from = from;

}

/**

* 设置显示的名称

*/

public void setDisplayName(String displayName) {

this.displayName = displayName;

}

/**

* 设置服务器是否需要身份认证

*/

public void setIfAuth(boolean ifAuth) {

this.ifAuth = ifAuth;

}

/**

* 设置E-mail用户名

*/

public void setUserName(String username) {

this.username = username;

}

/**

* 设置E-mail密码

*/

public void setPassword(String password) {

this.password = password;

}

/**

* 设置接收者

*/

public void setTo(String to) {

this.to = to;

}

/**

* 设置主题

*/

public void setSubject(String subject) {

this.subject = subject;

}

/**

* 设置主体内容

*/

public void setContent(String content) {

this.content = content;

}

public EmailWriteToFile() {

}

private int port = 25;

public int getPort() {

return port;

}

public void setPort(int port) {

this.port = port;

}

/**

* 发送邮件

*

* @throws IOException

* @throws FileNotFoundException

*/

public boolean send() throws FileNotFoundException, IOException {

HashMap<String, String> map = new HashMap<String, String>();

map.put("state", "success");

String message = "邮件发送成功!";

Session session = null;

Properties props = System.getProperties();

props.put("mail.smtp.host", smtpServer);

props.put("mail.smtp.port", port);

try {

props.put("mail.smtp.auth", "false");

session = Session.getDefaultInstance(props, null);

session.setDebug(false);

Transport trans = null;

Message msg = new MimeMessage(session);

try {

Address from_address = new InternetAddress(from, displayName);

msg.setFrom(from_address);

} catch (java.io.UnsupportedEncodingException e) {

e.printStackTrace();

}

InternetAddress[] address = { new InternetAddress(to) };

msg.setRecipients(Message.RecipientType.TO, address);

msg.setSubject(subject);

Multipart mp = new MimeMultipart();

MimeBodyPart mbp = new MimeBodyPart();

mbp.setContent(content.toString(), getContentType() + "; charset=" + getCharset());

mp.addBodyPart(mbp);

if (!file.isEmpty()) {// 有附件

Enumeration efile = file.elements();

while (efile.hasMoreElements()) {

mbp = new MimeBodyPart();

filename = efile.nextElement().toString(); // 选择出每一个附件名

FileDataSource fds = new FileDataSource(filename); // 得到数据源

mbp.setDataHandler(new DataHandler(fds)); // 得到附件本身并至入BodyPart

mbp.setFileName(MimeUtility.encodeText(fds.getName(), getCharset(),"B")); // 得到文件名同样至入BodyPart

mp.addBodyPart(mbp);

}

file.removeAllElements();

}

msg.setContent(mp); // Multipart加入到信件

msg.setSentDate(new Date()); // 设置信件头的发送日期

// 发送信件

msg.saveChanges();

File f = new File("d:/test.eml");

msg.writeTo(new FileOutputStream(f));

} catch (AuthenticationFailedException e) {

map.put("state", "failed");

message = "邮件发送失败!错误原因: " + "身份验证错误!";

e.printStackTrace();

return false;

} catch (MessagingException e) {

message = "邮件发送失败!错误原因: " + e.getMessage();

map.put("state", "failed");

e.printStackTrace();

Exception ex = null;

if ((ex = e.getNextException()) != null) {

System.out.println(ex.toString());

ex.printStackTrace();

}

return false;

}

// System.out.println(" 提示信息:"+message);

map.put("message", message);

return true;

}

public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {

EmailWriteToFile o = new EmailWriteToFile();

o.setSmtpServer("localhost");

o.setFrom("");

o.setDisplayName("TOM");

o.setTo("");

o.setSubject("Test Subject");

o.setContent("Test Content");

o.setCharset("GBK");

o.addFile("e:/读我.txt");

o.send();

Process p = Runtime.getRuntime().exec("cmd /C start msimn.exe /eml:d:/test.eml");

}

}

【责编:Zenghui】

--------------------next---------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值