本文介绍如何用spring发送邮件
目录结构
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
MailConfig
package com.springlearn.learn.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
public class MailConfig {
@Bean
public JavaMailSender getjavamailsender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.qq.com");
mailSender.setPort(587);
mailSender.setUsername("你的qq邮箱");
mailSender.setPassword("你的授权码");
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.from", "你的qq邮箱");
return mailSender;
}
}
TestController
package com.springlearn.learn.controller;
import java.io.File;
import java.io.IOException;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServletResponse;
import com.springlearn.learn.DemoApplication;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
public JavaMailSender emailSender;
@ResponseBody
@RequestMapping(value = "/sendmail", method = RequestMethod.GET)
public String Test(HttpServletResponse response) throws IOException, MessagingException {
// 发送简单短信
// SimpleMailMessage message = new SimpleMailMessage();
// message.setTo("756029960@qq.com");
// message.setSubject("测试邮件");
// message.setText("发送成功");
// this.emailSender.send(message);
// 发送附件
// MimeMessage message = emailSender.createMimeMessage();
// MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
// helper.setTo("你要发送人的邮件");
// helper.setSubject("测试邮件,有附件!");
// helper.setText("给你发一份附件!", true);
// System.out.println();
// FileSystemResource file1 = new FileSystemResource(new File(DemoApplication.class.getResource("").getPath(), "demo.txt"));
// helper.addAttachment("Txt1 file", file1);
// FileSystemResource file2 = new FileSystemResource(new File(DemoApplication.class.getResource("").getPath(), "demo1.txt"));
// helper.addAttachment("Txt1 file", file2);
// emailSender.send(message);
return "Ok!";
}
}
测试
http://localhost:9001/sendmail