1.首先引入Jar包支持
<!--spring支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!--邮件发送-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
2.配置xml文件
<!--邮件配置-->
<context:property-placeholder location="classpath:mail.properties" ignore-unresolvable="true"/>
<!--配置邮件接口-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}"/>
<property name="username" value="${mail.smtp.username}"/>
<property name="password" value="${mail.smtp.password}"/>
<property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
</props>
</property>
3.新建mail.properties文件
#服务器主机名 smtp.xx.com
mail.smtp.host=smtp.163.com
mail.smtp.username=huachun_w@163.com
#密码/客户端授权码
mail.smtp.password=xxxxxxxxxx
#编码字符
mail.smtp.defaultEncoding=utf-8
#是否进行用户名密码校验
mail.smtp.auth=true
#设置超时时间
mail.smtp.timeout=20000
4.配置一个测试类
package com.imooc.oa.quartz;
import org.springframework.beans.factory.annotation.Autowired;
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.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
@Controller
@RequestMapping("/mail")
public class SendMailController {
@Autowired
private JavaMailSender javaMailSender;//在spring中配置的邮件发送的bean
@RequestMapping("/send")
public Object sendMail03(){
MimeMessage mMessage=javaMailSender.createMimeMessage();//创建邮件对象
MimeMessageHelper mMessageHelper;
Properties prop = new Properties();
String from;
try {
//从配置文件中拿到发件人邮箱地址
prop.load(this.getClass().getResourceAsStream("/mail.properties"));
from = prop.get("mail.smtp.username")+"";
mMessageHelper=new MimeMessageHelper(mMessage,true);
mMessageHelper.setFrom(from);//发件人邮箱
mMessageHelper.setTo("huachun_w@163.com");//收件人邮箱
mMessageHelper.setSubject("Spring的邮件发送");//邮件的主题
mMessageHelper.setText("<p>这是使用spring的邮件功能发送的一封邮件</p><br/>" +
"<a href='https://blog.csdn.net/Mr__Viking'>打开我的博客主页</a><br/>" +
"<img src='fengye'>",true);//邮件的文本内容,true表示文本以html格式打开
File file=new File("D:/图片/大雄.jpg");//在邮件中添加一张图片
FileSystemResource resource=new FileSystemResource(file);
mMessageHelper.addInline("fengye", resource);//这里指定一个id,在上面引用
mMessageHelper.addAttachment("D:/图片/静香.jpg", resource);//在邮件中添加一个附件
javaMailSender.send(mMessage);//发送邮件
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "发送成功";
}
}
参考博客:
https://blog.csdn.net/Mr__Viking/article/details/81090046
有时候会出先544异常问题
解决方法:把MessagingException改成Exception就可以成功接到Exception。线程就不会阻塞。
参考博客:
https://blog.csdn.net/xiaojiahao_kevin/article/details/51489955
版权声明:本文为hauchun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。