1、邮箱开启服务
以 QQ 邮箱为例:
进入网页邮箱 -> 设置
开启之后会得到一个授权码,待会配置需要这个授权码
2、添加依赖
在 pom.xml 添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version> // 版本号根据自己使用的 spring 版本号对应
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${commons-httpclient.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
3、配置文件
application.properties
:
# QQ 邮箱配置 使用 465 端口会出现超时错误,使用 587 亲测无异常
qq.mail.host = smtp.qq.com
qq.mail.port = 587
qq.mail.username = ******* # 发送人邮箱
qq.mail.password = *********** # 授权码
qq.mail.default-encoding = UTF-8
# 163 邮箱配置
163.mail.host = smtp.163.com
# 126 邮箱配置
126.mail.host = smtp.126.com
applicationContext.xml
:
<!-- 加载 application.properties 配置文件 -->
<context:property-placeholder location="classpath:application.properties"/>
<!-- 邮件发送 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${qq.mail.host}"/>
<property name="port" value="${qq.mail.port}"/>
<property name="username" value="${qq.mail.username}"/>
<property name="password" value="${qq.mail.password}"/>
<property name="defaultEncoding" value="${qq.mail.default-encoding}"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.a uth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
<!--<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>-->
<!-- 如果是网易邮箱, mail.smtp.starttls.enable 设置为 false-->
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
4、业务逻辑层
MailService.java
接口
package service;
/**
* create by 1311230692@qq.com on 2018/10/8 14:30
* 发送邮件业务接口
**/
public interface MailService {
/**
* 简单文本邮件发送
* @param to 邮件接收者
* @param subject 邮件主题
* @param content 邮件内容
* */
void sendSimpleMail(String to, String subject, String content);
/**
* 发送富文本邮件 支持文本、附件、HTML、图片等
* @param to 邮件接收者
* @param subject 邮件主题
* @param content 邮件内容
* */
void sendHtmlMail(String to, String subject, String content);
/**
* 发送带附件的邮件
* @param to 邮件接收者
* @param subject 邮件主题
* @param content 邮件内容
* @param filePath 附件地址
* */
void sendAttatchmentMail(String to, String subject, String content, String filePath);
}
实现:
MailServiceImpl.java
package service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* create by 1311230692@qq.com on 2018/10/8 14:33
* 邮件发送业务逻辑层实现
**/
@Service("MailService")
public class MailServiceImpl implements MailService{
@Autowired
private JavaMailSender mailSender;
@Value("${qq.mail.username}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(from);
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(content);
try {
mailSender.send(simpleMailMessage);
System.out.println("普通文本邮件已发送");
} catch (Exception e) {
System.out.println("普通文本邮件发送异常");
e.printStackTrace();
}
}
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8"); // 设置字符编码,避免中文乱码
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(mimeMessage);
System.out.println("HTML 格式邮件发送成功");
} catch(MessagingException e) {
System.out.println("HTML 格式邮件发送异常");
e.printStackTrace();
}
}
@Override
public void sendAttatchmentMail(String to, String subject, String content, String filePath) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); // 设置字符编码,避免中文乱码
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName,file);
mailSender.send(mimeMessage);
System.out.println("带附件邮件发送成功");
} catch(MessagingException e) {
System.out.println("带附件邮件发送异常");
e.printStackTrace();
}
}
}
5、测试类:
MailTest.java
:
package test;
import org.junit.Test;
import service.MailService;
import javax.annotation.Resource;
/**
* create by 1311230692@qq.com on 2018/10/8 15:01
* 邮件发送测试类
**/
public class MailTest extends BaseJunit{
@Resource(name="MailService")
private MailService mailService;
@Test
public void mailSendTest() throws Exception {
mailService.sendSimpleMail("1441159735@qq.com", "邮件收发测试", "这是第一封邮件");
System.out.println("邮件发送成功");
}
@Test
public void mailHtmlTest() throws Exception {
String content = "<html>\n" + "<body>\n" +
"<h3>hello world 这是第一封 html 邮件!</h3>\n" + "<a href=https://www.baidu.com>激活</a>\n" +
"</body>\n" + "</html>\n";
mailService.sendHtmlMail("1441159735@qq.com", "邮件收发测试", content);
System.out.println("HTML 格式邮件发送成功");
}
@Test
public void mailAttatchmentTest() throws Exception {
String filePath = "**********"; // 加入附件路径
mailService.sendAttatchmentMail("1441159735@qq.com", "邮件收发测试", "有附件,请查收", filePath);
System.out.println("带附件邮件发送成功");
}
}
6、测试结果:
版权声明:欢迎转载, 转载请保留原文链接。