jdk1.8的版本发送邮件可能会有我上篇博客中提到的问题,请参考 javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
javax.mail方式
package com.healist.email;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* Created by Healist on 2017/1/29.
*/
public class Email {
public static void main(String[] args) throws MessagingException {
// 创建Properties 类用于记录邮箱的一些属性
Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth", "true");
//此处填写SMTP服务器
props.put("mail.smtp.host", "smtp.qq.com");
//端口号,QQ邮箱给出了两个端口,但是另一个不管用,这个587在这是管用的
props.put("mail.smtp.port", "587");
// 此处填写你的账号
props.put("mail.user", "794622537@qq.com");
// QQ邮箱的授权码,去邮箱自己获取
props.put("mail.password", "dffajaehrb");
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人的邮箱
InternetAddress to = new InternetAddress("382808591@qq.com");
message.setRecipient(MimeMessage.RecipientType.TO, to);
// 设置邮件标题
message.setSubject("测试邮件");
// 设置邮件的内容体
message.setContent("这是一封测试邮件", "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
}
}
邮件接受结果:
Spring Mail方式
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<context:component-scan base-package="com.healist.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dev.properties</value>
<!--如果有多个配置文件,在这里继续添加 -->
</list>
</property>
</bean>
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}" />
<property name="port" value="${mail.smtp.port}" />
<property name="username" value="${mail.smtp.username}" />
<property name="password" value="${mail.smtp.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
<prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
</beans>
dev.properties邮箱配置文件:
mail.smtp.host=smtp.qq.com
mail.smtp.auth=true
mail.smtp.timeout=50000
mail.smtp.username=794622537@qq.com
#这个是QQ邮箱的授权码,获取的方式,打开你的邮箱(设置 -> 账户 -> POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 -> POP3/SMTP服务 ,点击开启,短信验证后就会获得授权码)
mail.smtp.password=fsafasdfasdfad
mail.smtp.port=465
mail.smtp.starttls.enable=true
测试类:
/**
* Created by Healist on 2017/1/29.
*/
public class TestEmail {
@Test
public void testExecute() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
JavaMailSender sender = (JavaMailSender) ctx.getBean("javaMailSender");
System.out.println("测试发送邮件.....");
SimpleMailMessage mail = new SimpleMailMessage();
try {
mail.setTo("382808591@qq.com");// 接受者
mail.setFrom("794622537@qq.com");// 发送者,
mail.setSubject("测试主题");// 主题
mail.setText("发送邮件内容测试");// 邮件内容
sender.send(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
}
邮件接受结果: