package com.rong.excel.util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
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;
/**
* javax 实现发邮件
*
* <pre>
* <dependency>
* <groupId>javax.mail</groupId>
* <artifactId>mail</artifactId>
* <version>1.5.0-b01</version>
* </dependency>
*
* <dependency>
* <groupId>javax.activation</groupId>
* <artifactId>activation</artifactId>
* <version>1.1.1</version>
* </dependency>
* </pre>
*
* @author
*
*/
public class JavaxEmailUtils {
public static void main(String[] args) {
// 收件人信息,邮件内容信息
String toAddr = "";
String ccAddr = null;
String bccAddr = null;
String subject = "test title";
String content = "test content";
Vector<File> attFiles = null;
// Vector<File> attFiles = new Vector<File>();
// 发件人信息
String fromHost = "smtp.qq.com";
String fromNickNm = "";
String fromAddr = "";
String fromPwd = "";
boolean useSSL = true;
String sslPort = "465";
try {
send(fromHost, fromNickNm, fromAddr, fromPwd, useSSL, sslPort, //
toAddr, ccAddr, bccAddr, subject, content, attFiles);
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("finished!");
}
static {
System.setProperty("mail.mine.splitlongparameters", "false");
System.setProperty("mail.mine.charset", "UTF-8");
}
/**
* javax 发送邮件
*
* @param fromHost 发件人Host
* @param fromNickNm 发件人 昵称
* @param fromAddr 发件人账号
* @param fromPwd 发件人密码
* @param useSSL 是否使用SSL
* @param sslPort SSL端口
* @param toAddr 收件人地址,多个用英文逗号分隔
* @param ccAddr 抄送人地址,多个用英文逗号分隔
* @param bccAddr 密送人地址,多个用英文逗号分隔
* @param subject 主题(标题)
* @param content 正文
* @param attFiles 附件
* @return
*/
public static void send( //
String fromHost, //
String fromNickNm, //
String fromAddr, //
String fromPwd, //
boolean useSSL, //
String sslPort, //
String toAddr, //
String ccAddr, //
String bccAddr, //
String subject, //
String content, //
Vector<File> attFiles//
) throws NoSuchProviderException, MessagingException, UnsupportedEncodingException {
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.smtp.host", fromHost);
if (useSSL) {
prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.setProperty("mail.smtp.socketFactory.port", sslPort);
prop.setProperty("mail.smtp.port", sslPort);
}
Transport transport = null;
try {
// 创建session
Session session = Session.getInstance(prop);
session.setDebug(false);
// 创建 transport
transport = session.getTransport("smtp");
// 连接到发件服务器
transport.connect(fromAddr, fromPwd);
// 准备邮件主体
MimeMessage message = new MimeMessage(session);
// 发件人信息
message.setFrom(new InternetAddress(fromAddr, fromNickNm, "UTF-8"));
// 收件人
message.setRecipients(MimeMessage.RecipientType.TO, toAddr);
// 抄送人
message.setRecipients(MimeMessage.RecipientType.CC, ccAddr);
// 密送人
message.setRecipients(MimeMessage.RecipientType.BCC, bccAddr);
// 标题
message.setSubject(subject);
/*
* 正文 start
*/
// 简单正文
// message.setText("content");
// 复杂正文
MimeMultipart multipart = new MimeMultipart();
multipart.setSubType("mixed");
MimeBodyPart contentBodyPart = new MimeBodyPart();
contentBodyPart.setContent(content, "text/html;charset=UTF-8");
multipart.addBodyPart(contentBodyPart);
// 添加附件
if (attFiles != null && !attFiles.isEmpty()) {
for (File attFile : attFiles) {
MimeBodyPart attBodyPart = new MimeBodyPart();
DataHandler dataHandler = new DataHandler(new FileDataSource(attFile));
attBodyPart.setDataHandler(dataHandler);
String fileName = dataHandler.getName();
fileName = MimeUtility.encodeText(fileName);
attBodyPart.setFileName(fileName);
multipart.addBodyPart(attBodyPart);
}
}
/*
* 正文 end
*/
// 将正文添加到主体
message.setContent(multipart);
message.saveChanges();
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
} catch (NoSuchProviderException e) {
throw e;
} catch (MessagingException e) {
throw e;
} catch (UnsupportedEncodingException e) {
throw e;
} finally {
try {
if (transport != null) {
transport.close();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
javax 实现发邮件
最新推荐文章于 2024-03-25 16:57:22 发布