①,首先发送邮箱要开启特定服务,这里以qq邮箱为例html
②,开启java
③,获取密码web
点击生成受权码,而后发送短信到指定的地方,便能获取密码spring
④,点击我已发送,获得受权码,也就是application.properties 要配置的密码springboot
①,pom.xmlsession
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
②,application.properties 配置app
spring.mail.host=smtp.qq.com spring.mail.username=2425373545@qq.com #这里填写你收到的那个密码 spring.mail.password=xxx #必须开启,不然会报503错误 spring.mail.properties.mail.smtp.ssl.enable=true
它为咱们配置了一个JavaMailSenderImpl,而咱们也就是经过这个bean进行收发邮件异步
@Bean public JavaMailSenderImpl mailSender() { JavaMailSenderImpl sender = new JavaMailSenderImpl(); if (this.session != null) { sender.setSession(this.session); } else { applyProperties(sender); } return sender; }
①,因为发送邮件通常不要求实时,因此建议开启异步spring-boot
@EnableAsync//开启异步 @SpringBootApplication public class TaskApplication { public static void main(String[] args) { SpringApplication.run(TaskApplication.class, args); } }
②,发送邮件的代码编写测试
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @Service public class MailService { @Autowired private JavaMailSender javaMailSender; /*发送简单邮件*/ @Async public String send() { SimpleMailMessage message=new SimpleMailMessage(); message.setSubject("告白"); message.setText("见什么世面,见见你就行了"); message.setTo("2539517371@qq.com"); message.setFrom("2425373545@qq.com"); javaMailSender.send(message); return "邮件发送成功。。。。"; } /*发送带html文本以及带上附件*/ @Async public String sendHtml() { MimeMessage message=javaMailSender.createMimeMessage(); try { MimeMessageHelper helper=new MimeMessageHelper(message,true);//支持文件上传 helper.setSubject("告白"); helper.setText("<font color='red'>想你,老戴里都是你</font>",true); helper.setTo("2539517371@qq.com"); helper.setFrom("2425373545@qq.com"); helper.addAttachment("dm.jpg",new File("D:\\dev\\IdeaProjects\\springboot\\task\\src\\main\\resources\\dm.jpg")); javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } return "邮件发送成功。。。。"; } }
③,controller层代码以下
import com.ts.task.service.MailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MailController { @Autowired private MailService mailService; @RequestMapping("send") public String send() { mailService.send(); return "邮件发送成功。。。。"; } /*发送html文本以及带上附件*/ @RequestMapping("sendHtml") public String sendHtml() { mailService.sendHtml(); return "邮件发送成功。。。。"; } }
①,发送简单邮件
②,发送带附件,html文本的邮件
①,
②,
③,