通过javaMail发送邮件,可选添加多个收件人,密送,抄送,多个附件
/qq_35434831/article/details/79666035
版权声明:本文为博主原创文章,未经博主允许。 /qq_35434831/article/details/79666035
自己通过学习多人的代码,并整理了一个简单,调用方便的通过javaMail发送邮件。只需填写发件邮箱地址,密码;收件人地址,附件,选择是否保存,设置自己发送邮件时的昵称就ok了。代码自动匹配设置smtp服务地址和端口。
发送邮件需要邮箱地址和密码,开启POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务。各大邮箱使用外部登录验证的方式不一样,有些需要使用授权码登录链接,有些只需要邮箱登录密码,这个得根据你使用的邮箱服务平台的规定了。这里我收集了下面的邮箱smtp服务地址和端口,【QQ、Foxmail、139、126、163、Google(gmail)、Exchange、Outlook、、】这些足以够用了吧!不多说,看代码。
使用方法
public static void main(String[] args) throws Exception {
List<String> map = new ArrayList<>();
map.add("123456@");
map.add("456789@");
map.add("hahaha123@");
new SendEmail("hahaha123@", "密码")
.setDebug(true)
.setMyNickName("这是我的昵称")
.addFile("C:/Users/25171/Pictures/timg.jpg")//添加附件
.addFile("C:/Users/25171/Desktop/QQ图片0317192741.jpg")
// .addFile(List<String> list)//添加附件集合
.setSaveEmail("C:/User/2517/Desktop/name.eml")//保存邮件
.addRecipientT0("251716795@")//添加收件人地址
// .addRecipientT0(map)//添加收件人地址集合
// .addRecipientCC(map)//添加密送收件人地址
// .addRecipientBCC(map)//添加抄送收件人地址
.createMail("标题", "发送的内容", "text/html;charset=utf-8")
.sendEmail(new SendEmail.Callback() {
@Override
public void success(String s) {
System.out.println(s);//发送完成后回调接口
}
@Override
public void error(String s, Exception e) {
System.out.println(s);
e.printStackTrace();//异常失败的回调接口
}
});
}
主体工具代码
package com.sai.mail;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;
public class SendEmail {
public interface Callback {
void success(String s);
void error(String s, Exception e);
}
private Callback callback; //信息回调接口
private Properties properties;//系统属性对象
private String mailAccount; //发送邮箱地址
private String mailPassword; //验证密码
private Session session; //邮件会话对象
private String myNickName; //昵称,发送时自己的昵称
private boolean debug = false;//debug模式
private boolean isSaveEmail = false;
private String pathName = "exc.eml";//邮件保存时的
public SendEmail(String mailAccount, String mailPassword) {
this.mailAccount = mailAccount;
this.mailPassword = mailPassword;
}
public SendEmail setSaveEmail(String pathName) {
isSaveEmail = true;
this.pathName = pathName;
return this;
}
private List<String> recipientT0List = new ArrayList<>();//收件地址
private List<String> recipientCCList = new ArrayList<>();//密送地址
private List<String> recipientBCCList = new ArrayList<>();//抄送地址
private List<String> filePath = new ArrayList<>();//附件
public SendEmail setDebug(boolean sessionDebug) {
debug = sessionDebug;
return this;
}
/*** 设置多人收件人地址 */
public SendEmail addRecipientT0(String address) {
recipientT0List.add(address);
return this;
}
public SendEmail addRecipientCC(String address) {
recipientCCList.add(address);
return this;
}
public SendEmail addRecipientBCC(String address) {
recipientBCCList.add(address);
return this;
}
public SendEmail addRecipientT0(List<String> address) {
recipientT0List.addAll(address);
return this;
}
public SendEmail addRecipientCC(List<String> address) {
recipientCCList.addAll(address);
return this;
}
public SendEmail addRecipientBCC(List<String> address) {
recipientBCCList.addAll(address);
return this;
}
/***添加文件***/
public SendEmail addFile(String filePath) {
this.filePath.add(filePath);
return this;
}
public SendEmail addFile(List<String> list) {
this.filePath.addAll(list);
return this;
}
/****昵称设置**/
public SendEmail setMyNickName(String name) {
myNickName = name;
return this;
}
private MimeMessage message;
/**
* @param title 主题
* @param datas 内容
* @param type 内容格式类型 text/html;charset=utf-8
* @return s
*/
public SendEmail createMail(String title, String datas, String type) {
if (mailAccount.length() == 0 || mailAccount.equals(null)) {
System.err.println("发件地址不存在!");
return this;
}
if (myNickName == null) {
myNickName = mailAccount;
}
getProperties();
if (!sync) return this;
try {
message = new MimeMessage(session);
// 设置发送邮件地址,param1 代表发送地址 param2 代表发送的名称(任意的) param3 代表名称编码方式
message.setFrom(new InternetAddress(mailAccount, myNickName, "utf-8"));
setRecipientT0(); //添加接收人地址
setRecipientCC(); //添加抄送接收人地址
setRecipientBCC(); //添加密送接收人地址
BodyPart messageBodyPart = new MimeBodyPart(); // 创建消息部分
Multipart multipart = new MimeMultipart(); // 创建多重消息
messageBodyPart.setContent(datas, type); // 消息内容
multipart.addBodyPart(messageBodyPart); // 设置文本消息部分
addFile(multipart); //附件部分
// 发送完整消息
message.setContent(multipart);
message.setSubject(title); // 设置邮件主题
message.setSentDate(new Date()); // 设置发送时间
message.saveChanges(); // 保存上面的编辑内容
// 将上面创建的对象写入本地
saveEmail(title);
} catch (Exception e) {
if (callback != null)
callback.error("message error ", e);
sync = false;
}
return this;
}
public void sendEmail(Callback callback) {
this.callback = callback;
if (!sync)
return;
try {
Transport trans = session.getTransport();
// 链接邮件服务器
trans.connect(mailAccount, mailPassword);
// 发送信息
trans.sendMessage(message, message.getAllRecipients());
// 关闭链接
trans.close();
if (callback != null)
callback.success("发送完成");
} catch (Exception e) {
if (callback != null)
callback.error("发送异常", e);
}
}
private void saveEmail(String title) throws IOException, MessagingException {
OutputStream out = null;
if (isSaveEmail) {
if (pathName.length() == 0 || pathName.equals(null)) {
out = new FileOutputStream(title + ".eml");
} else {
String path[] = pathName.split("\\.");
out = new FileOutputStream(path[0] + title + ".eml");
}
}
message.writeTo(out);
out.flush();
out.close();
}
/*** 设置收件人地址信息*/
private void setRecipientT0() throws MessagingException, UnsupportedEncodingException {
if (recipientT0List.size() > 0) {
InternetAddress[] sendTo = new InternetAddress[recipientT0List.size()];
for (int i = 0; i < recipientT0List.size(); i++) {
System.out.println("发送到:" + recipientT0List.get(i));
sendTo[i] = new InternetAddress(recipientT0List.get(i), "", "UTF-8");
}
message.addRecipients(MimeMessage.RecipientType.TO, sendTo);
}
}
/***设置密送地址**/
private void setRecipientCC() throws MessagingException, UnsupportedEncodingException {
if (recipientCCList.size() > 0) {
InternetAddress[] sendTo = new InternetAddress[recipientCCList.size()];
for (int i = 0; i < recipientCCList.size(); i++) {
System.out.println("发送到:" + recipientCCList.get(i));
sendTo[i] = new InternetAddress(recipientCCList.get(i), "", "UTF-8");
}
message.addRecipients(, sendTo);
}
}
/***设置抄送邮件地址**/
private void setRecipientBCC() throws MessagingException, UnsupportedEncodingException {
if (recipientBCCList.size() > 0) {
InternetAddress[] sendTo = new InternetAddress[recipientBCCList.size()];
for (int i = 0; i < recipientBCCList.size(); i++) {
System.out.println("发送到:" + recipientBCCList.get(i));
sendTo[i] = new InternetAddress(recipientBCCList.get(i), "", "UTF-8");
}
message.addRecipients(MimeMessage.RecipientType.BCC, sendTo);
}
}
/***添加附件****/
private void addFile(Multipart multipart) throws MessagingException, UnsupportedEncodingException {
if (filePath.size() == 0)
return;
for (int i = 0; i < filePath.size(); i++) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
// 选择出每一个附件名
String pathName = filePath.get(i);
System.out.println("添加附件 ====>" + pathName);
// 得到数据源
FileDataSource fds = new FileDataSource(pathName);
// 得到附件本身并至入BodyPart
messageBodyPart.setDataHandler(new DataHandler(fds));
//采用这去除中文乱码
messageBodyPart.setFileName(MimeUtility.encodeText(fds.getName()));
multipart.addBodyPart(messageBodyPart);
}
}
private boolean sync = true;
/**
* 规定设置 传输协议为smtp 根据输入的邮箱地址自动匹配smtp服务器地址与smtp服务器地址端口
*/
private void getProperties() {
String account[] = mailAccount.split("@");
String mailTpye = "";
try {
mailTpye = account[1];
} catch (Exception e) {
System.err.println("不正确的邮箱地址!");
sync = false;
return;
}
String SMTPHost = "";//smtp服务器地址
String SMTPPort = "";//smtp服务器地址端口
switch (mailTpye) {
case "":
case "":
SMTPHost = "";
SMTPPort = "465";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "465";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "465";
break;
case "":
SMTPHost = "";
SMTPPort = "465";
break;
default:
System.err.println("暂时不支持此账号作为服务账号发送邮件!");
return;
}
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp"); // 设置邮件传输采用的协议smtp
prop.setProperty("mail.smtp.host", SMTPHost);// 设置发送人邮件服务器的smtp地址
prop.setProperty("mail.smtp.auth", "true"); // 设置验证机制
prop.setProperty("mail.smtp.port", SMTPPort);// SMTP 服务器的端口 (非 SSL 连接的端口一般默认为 25, 可以不添加, 如果开启了 SSL 连接,
prop.setProperty("mail.smtp.socketFactory.class", ".ssl.SSLSocketFactory");
prop.setProperty("mail.smtp.socketFactory.fallback", "false");
prop.setProperty("mail.smtp.socketFactory.port", SMTPPort);
properties = prop;
session = Session.getInstance(properties);
session.setDebug(debug);
}
}
完成这些需要导入的重要jar包:mail-1.4.1.jar(或者更高的版本) 和 activation 包,jdk1.8中rt.jar包含的activation包。所以jdk1.8的就无需考虑activation怎么下载了。
完成解析分配服务器地址和端口的主要函数(写的不怎么好):
private void getProperties() {
String account[] = mailAccount.split("@");
String mailTpye = "";
try {
mailTpye = account[1];
} catch (Exception e) {
System.err.println("不正确的邮箱地址!");
sync = false;
return;
}
String SMTPHost = "";//smtp服务器地址
String SMTPPort = "";//smtp服务器地址端口
switch (mailTpye) {
case "":
case "":
SMTPHost = "";
SMTPPort = "465";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "465";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "25";
break;
case "":
SMTPHost = "";
SMTPPort = "465";
break;
case "":
SMTPHost = "";
SMTPPort = "465";
break;
default:
System.err.println("暂时不支持此账号作为服务账号发送邮件!");
return ;
}
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp"); // 设置邮件传输采用的协议smtp
prop.setProperty("mail.smtp.host", SMTPHost);// 设置发送人邮件服务器的smtp地址
prop.setProperty("mail.smtp.auth", "true"); // 设置验证机制
prop.setProperty("mail.smtp.port", SMTPPort);// SMTP 服务器的端口 (非 SSL 连接的端口一般默认为 25, 可以不添加
prop.setProperty("mail.smtp.socketFactory.class", ".ssl.SSLSocketFactory");
prop.setProperty("mail.smtp.socketFactory.fallback", "false");
prop.setProperty("mail.smtp.socketFactory.port", SMTPPort);
properties = prop;
session = Session.getInstance(properties);
session.setDebug(debug);
}
最后看看效果: