java发送邮件示例
程序员文章站
2022-07-08 11:10:49
...
我们如果做管理系统的项目,不可避免要使用发送邮件,很多项目中用到mail的依赖比较老旧。其实spring提供JavaMailSender接口实现邮件发送,非常方便这里做过例子展示出来,供大家参考。
1、maven依赖(这里有用velocity模板,故也引入velocity的依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-velocity -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2、application.properties的配置文件
spring.mail.host=smtp.163.com
spring.mail.username=(邮件账号)
spring.mail.password=(邮件的授权码,不是密码)
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
3、发送文本加附件
@Autowired
private JavaMailSenderImpl javaMailSender;
@PostMapping("/test/sendMail")
public JsonVO sendMailMessage(@RequestBody LamdaVO lamdaVO) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setFrom(javaMailSender.getUsername());
helper.setTo("aaa@qq.com");
helper.setSubject("3tsudo");
helper.setText("this is frist eamil", true);
File file = new File("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
helper.addAttachment("studio-3t.txt",file);
javaMailSender.send(mimeMessage);
JsonVO<Object> jsonVO = new JsonVO<>(200, "success");
return jsonVO;
}
4、通过书写java代码携带静态文件,发送邮件
@Autowired
private JavaMailSenderImpl javaMailSender;
@PostMapping("/test/sendMail2")
public JsonVO sendMailMessage2(@RequestBody LamdaVO lamdaVO) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setFrom(javaMailSender.getUsername());
helper.setTo("aaa@qq.com");
helper.setSubject("3tsudo2");
helper.setText("<html><body><img src=\"cid:photo\" ></body></html>", true);
FileSystemResource file = new FileSystemResource(new File("C:\\Users\\Administrator\\Desktop\\Snipaste_2019-12-25_21-41-00.png"));
//这里需要注意的是addInline函数中资源名称photo需要与正文中cid:photo对应起来
helper.addInline("photo", file);
javaMailSender.send(mimeMessage);
JsonVO<Object> jsonVO = new JsonVO<>(200, "success");
return jsonVO;
}
5、通过配置velocity模板发送邮件
首先在resources下新建template.vm文件
<tr>
<td style="padding: 70px 0 0 185px">
<span style="font-size:14px">名字</span>
</td>
<td style="padding: 70px 0 0 185px">
<span style="font-size:14px">$name</span>
</td>
</tr>
<tr>
<td style="padding: 12px 0 70px 185px">
<span style="font-size:14px">电话号码</span>
</td>
<td style="padding: 12px 0 70px 185px">
<span style="font-size:14px">$tel</span>
</td>
</tr>
@Autowired
private JavaMailSenderImpl javaMailSender;
@PostMapping("/test/sendMail3")
public JsonVO sendMailMessage3(@RequestBody LamdaVO lamdaVO) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setFrom(javaMailSender.getUsername());
helper.setTo("aaa@qq.com");
helper.setSubject("3tsudo2");
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("name", "你好");
velocityContext.put("tel", "12345678");
String mailContent = getMailContent(velocityContext, "template.vm");
helper.setText(mailContent,true);
javaMailSender.send(mimeMessage);
JsonVO<Object> jsonVO = new JsonVO<>(200, "success");
return jsonVO;
}
public String getMailContent(VelocityContext velocityContext,String file) {
StringWriter stringWriter = new StringWriter();
// velocity引擎
VelocityEngine velocityEngine = new VelocityEngine();
// 设置文件路径属性
Properties properties = new Properties();
String dir = this.getClass().getResource("/").getPath();
properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, dir + "static/template/");
// 引擎初始化属性配置
velocityEngine.init(properties);
// 加载指定模版
org.apache.velocity.Template template = velocityEngine.getTemplate(file, "utf-8");
// 填充模板内容
// 写到模板
template.merge(velocityContext, stringWriter);
return stringWriter.toString();
}