<code>
此代码用的jar文件:mail.jar(我这里用的是1.4.7 的版本);
如果jdk用的是1.8版本会出现SSL错误:这个问题是jdk导致的,jdk1.8里面有一个jce的包,安全性机制导致的访问https会报错,官网上有替代的jar包,如果替换掉就可以了.
这两个jar包的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
下载之后,把这个压缩文件解压,得到两个jar包去覆盖jdk安装目录下的jre\lib\security\下相同的jar包就能解决java8的邮件发送问题。
</code>
<!-- 发送邮件的maven -->
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!--发送邮件的gradle-->
// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.log4j.Logger;
import java.security.GeneralSecurityException;
import java.util.Properties;
/**
* Created by Mcin on 2017/5/18.
*/
public class ExMailUtil {
private static final Logger logger = Logger.getLogger(ExMailUtil.class);
// 这是腾讯企业邮箱的 如果是其他邮箱 自行更换
static String MAIL_TRANSPORT_PROTOCOL = "smtp"; //邮箱协议
static String MAIL_SMTP_HOST = "smtp.exmail.qq.com"; //发件服务器地址
static String MAIL_SMTP_PORT = "465"; // 端口
static String MAIL_SMTP_AUTH = "true"; //使用smtp身份验证
/**
* 邮箱配置
*/
public static Properties setTencentExEmail (){
Properties prop = new Properties();
//协议
prop.setProperty("mail.transport.protocol", MAIL_TRANSPORT_PROTOCOL);
//服务器
prop.setProperty("mail.smtp.host", MAIL_SMTP_HOST);
//端口
prop.setProperty("mail.smtp.port", MAIL_SMTP_PORT);
//使用smtp身份验证
prop.setProperty("mail.smtp.auth", MAIL_SMTP_AUTH);
//开启安全协议 使用SSL,企业邮箱必需!
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
return prop;
}
}
import org.apache.log4j.Logger;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
/**
* Created by mcin on 2017/5/18.
* 发送企业邮箱
*/
public class SendMail {
private static final Logger logger = Logger.getLogger(SendMail.class);
final static String TO_EMAIL_ADDRESS = "收件人账号邮箱"; // 收件人账号邮箱账号
final static String USER_NAME = "要登陆的企业邮箱账号"; // 要登陆的企业邮箱账号
final static String PASS_WORD = "要登陆的企业邮箱密码"; / /要登陆的企业邮箱密码
final static String SUBJECT = "这是企业邮箱发送的主题"; // 邮件主题
final static String CONTENT = "这是企业邮箱发送的内容"; // 邮件内容
static long startTime , endTime; // 用于计算发送的时间耗时
public static void main(String[] args) throws Exception {
//获取Session对象
Session session = Session
.getDefaultInstance(
ExMailUtil
.setTencentExEmail(),
new Authenticator() {
//此访求返回用户和密码的对象
@Override
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication(USER_NAME, PASS_WORD);
return pa;
}
});
//设置session的调试模式,发布时取消
// session.setDebug(true);
/*
// 有循环的情况下,如果实现群发的功能 比如 收件人方可以显示到多少个收件用户的
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(userName,userName));*/
for (int i = 0; i <10 ; i++) {
// 有循环的情况下,实现单独发送的功能 收件人方只显示自己的邮箱
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(USER_NAME,USER_NAME));
mimeMessage.addRecipient(Message
.RecipientType
.TO,
new InternetAddress(
TO_EMAIL_ADDRESS
));
//设置主题
mimeMessage.setSubject(SUBJECT);
mimeMessage.setSentDate(new Date());
//设置内容
mimeMessage.setText(CONTENT);
mimeMessage.saveChanges();
logger.info("***开始发送第 "+(i+1)+" 个邮件***");
startTime = System.currentTimeMillis();
try {
//发送
Transport.send(mimeMessage);
endTime = System.currentTimeMillis();
logger.info("第 "+(i+1)+" 个发送成功***耗时:"
+(endTime - startTime)/1000+" 秒");
logger.info("-------------------------------------------------------------");
} catch (MessagingException e) {
logger.error(e.getMessage());
continue;
}
}
}
}