# 依赖
# spring 基本 jar 包
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context-support</artifactId> | |
<version>4.3.8.RELEASE</version> | |
</dependency> |
# javamail
<dependency> | |
<groupId>javax.mail</groupId> | |
<artifactId>mail</artifactId> | |
<version>1.4</version> | |
</dependency> |
# 配置 bean
<!-- spring mail --> | |
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> | |
<property name="host" value="smtp.qq.com" /> | |
<property name="username" value="你的QQ邮箱,如xxx@qq.com" /> | |
<property name="password" value="你的QQ邮箱授权码,自己去登录邮箱,从设置->账户->开通pops/smtp服务,生成,不是你的qq登录密码" /> | |
<property name="javaMailProperties"> | |
<props> | |
<prop key="mail.smtp.auth">true</prop> | |
<prop key="mail.smtp.timeout">30000</prop> | |
<prop key="mail.debug">true</prop> | |
<prop key="mail.smtp.starttls.enable">true</prop> | |
<!--<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>--> | |
</props> | |
</property> | |
</bean> |
# 使用
@Autowired | |
private JavaMailSender mailSender; | |
@PostMapping("sendEmail") | |
@ResponseBody | |
public ResponseVo<String> sendEmail( HttpServletRequest request ){ | |
try { | |
// 获取参数 | |
String senderAddress = request.getParameter("senderAddress"); | |
String recipientAddress = request.getParameter("recipientAddress"); | |
String subject = request.getParameter("subject"); | |
String message = request.getParameter("message"); | |
// 创建邮件 | |
SimpleMailMessage email = new SimpleMailMessage(); | |
email.setFrom(senderAddress); | |
email.setTo(recipientAddress); | |
email.setSubject(subject); | |
email.setText(message); | |
// 发送邮件 | |
mailSender.send(email); | |
return new ResponseVo<String>(true,"10000","发送成功",null); | |
}catch (Exception e){ | |
log.error("user/sendEmail接口异常",e); | |
return new ResponseVo<String>(true,"10001","邮件发送失败,请联系系统管理员",null); | |
} | |
} |
如果想用邮件模板,可以引入 freemarker
附录:
也可以利用 github 封装好的工具
参考:https://github.com/biezhi/oh-my-email