springboot2.x+mail实现邮件发送

这篇文章介绍springboot的邮件发送

由于很简单就没有分出server和imp之类,只是在controller简单写个方法进行测试。

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.2.5.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.ultrapower</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <!-- 打包跳过测试模块 -->
      <skipTests>true</skipTests>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-mail</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
         <exclusions>
            <exclusion>
               <groupId>org.junit.vintage</groupId>
               <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

2.application.yml
包含发送邮箱的地址,账号密码,接信地址,由于只是测试,所以都是固定写死的,如果项目中需要,可以根据自行情况进行修改。

spring:
  mail:
    host: smtp.163.com
    username: [email protected] #发送邮件邮箱地址
    password: xxxxxxxxxxxx #发送邮件邮箱密码
    protocol: smtp
    default-encoding: utf-8
    properties:
      mail:
        smtp:
          port: 465
          auth: true
          ssl:
            enable: true
          socketFactory:
            class: javax.net.ssl.SSLSocketFactory

3.controller的完整代码:

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.GetMapping;
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;

/**
 * @Deacription 邮件发送接口
 * @Author wenyt
 * @Date 2020/5/6 10:47
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/mail")
public class MailController {

    /**
     * 发送方
     */
    private String sender = "[email protected]";

    /**
     * 接收方
     */
    private String receiver = "[email protected]";

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * @method 发送文本邮件
     * @date
     */
    @GetMapping("/sendMail")
    public String sendMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(receiver);
        message.setSubject("大佬");
        message.setText("你好你好你好!");
        javaMailSender.send(message);
        return "success";
    }

    /**
     * @method 发送html邮件
     * @date
     */
    @GetMapping("/htmlMail")
    public String testHtmlMail() {
        String content = "\n" +
                "\n" +
                "    

hello world ! 这是一封Html邮件!

\n"
+ "\n" + ""; MimeMessage message = javaMailSender.createMimeMessage(); //true表示需要创建一个multipart message try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("html mail"); helper.setText(content, true); } catch (MessagingException e) { e.printStackTrace(); } javaMailSender.send(message); return "success"; } /** * @method 发送附件邮件 * @date */ @RequestMapping("/sendFilesMail") public String sendFilesMail() { String filePath = "/Users/dalaoyang/Downloads/article_tag.sql"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("附件邮件"); helper.setText("这是一封带附件的邮件", true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } return "success"; } /** * @method 发送图片邮件 * @date */ @RequestMapping("/sendInlineResourceMail") public String sendInlineResourceMail() { String Id = "dalaoyang12138"; String content = "这是有图片的邮件: + Id + "\' >"; String imgPath = "/Users/dalaoyang/Downloads/dalaoyang.jpeg"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("这是有图片的邮件"); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(Id, res); javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } return "success"; } }

查看邮件是否发送成功

你可能感兴趣的:(SpringBoot,spring,boot,javamail,java)