SpringBoot实现发邮件

1.导入依赖


   org.springframework.boot
   spring-boot-starter-mail

2.导入配置

spring:
  mail:
    default-encoding: UTF-8
    username: [email protected] #[email protected]
    password: SISQHSNBBOXFEJNW #tynmdlqdelfpjiha
    host: smtp.163.com  #smtp.qq.com
    properties:
      mail:
        smtp:
          ssl:
            enable: true

3.工具类

package com.atguigu.blog.util;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

public class EmailUtil {

    // 默认邮箱配置
    private static final String FROM = "[email protected]";
    private static final String HOST = "smtp.163.com";
    private static final String PASSWORD = "SISQHSNBBOXFEJNW";

    /**
     * 自定义发件人邮箱
     * @param from       发件人邮箱
     * @param to         收件人邮箱
     * @param subject    邮件标题
     * @param text       邮件内容
     * @throws IOException
     * @throws MessagingException
     */
    public static void sendMail(String from, String[] to, String host, String password, String subject, String text, List fileList) {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setUsername(from);
        mailSender.setPassword(password);

        Properties prop = new Properties();
        prop.put("mail.smtp.auth", false);// 参数设为true,需服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", 5000);
        mailSender.setJavaMailProperties(prop);
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            mailSender.setHost(host);
            mailSender.setPassword(password);
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            messageHelper.setSubject(subject);
            messageHelper.setText(text, true);
            messageHelper.setTo(to);
            messageHelper.setFrom(from);
            if (fileList.size() != 0) {
                for (File file : fileList) {
                    messageHelper.addAttachment(file.getName(), file);
                }
            }
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 默认发件人邮箱
     * @param to 收件人邮箱
     * @param subject 标题
     * @param text 内容
     * @param fileList 文件
     */
    public static void sendMailNoFrom(String[] to, String subject, String text, List fileList) {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(HOST);
        mailSender.setUsername(FROM);
        mailSender.setPassword(PASSWORD);

        Properties prop = new Properties();
        prop.put("mail.smtp.auth", false);// 参数设为true,需服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", 5000);
        mailSender.setJavaMailProperties(prop);
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            messageHelper.setSubject(subject);
            messageHelper.setText(text, true);
            messageHelper.setTo(to);
            messageHelper.setFrom(FROM);
            if (fileList.size() != 0) {
                for (File file : fileList) {
                    messageHelper.addAttachment(file.getName(), file);
                }
            }
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


}

4.测试

@GetMapping("/sendtoqq")
    @Log("测试注解")
    public Result sendtoqq() {
        // 163
        String[] to = {"[email protected]", "[email protected]"};// 收件人

        List files = new ArrayList<>();
        File file = new File("/Users/lizhenghang/Desktop/网上购物系统设计与实现.docx");
        files.add(file);
        EmailUtil.sendMailNoFrom(to, "测试标题to qq", "测试内容to qq", files);
        return Result.ok("发送成功!");
    }

    @GetMapping("/sendto163")
    @Log("测试注解")
    public Result sendto163() {
        String from = "[email protected]";// 发件人邮箱
        String[] to = {"[email protected]"};// 收件人邮箱,支持传
        String password = "tynmdlqdelfpjiha";
        String host = "smtp.qq.com";

        List files = new ArrayList<>();
        File file = new File("/Users/lizhenghang/Desktop/网上购物系统设计与实现.docx");
        files.add(file);
        EmailUtil.sendMail(from, to, host, password, "测试标题11111 to 163", "测试内容11111 to 163", files);
        return Result.ok("发送成功!");
    }

 

你可能感兴趣的:(Java,javamail,java,发邮件)