zoukankan      html  css  js  c++  java
  • springboot整合Mail

    一、开启POP3/SMTP服务

     

     二、配置pom.xml

    <!-- 邮件依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

    三、配置application.yml

    # JavaMailSender 邮件发送的配置
    spring:
      mail:
        host: smtp.qq.com
        username: 12345678@qq.com
        # 授权码
        password: aaaaaaaaaaaaaaaa
        properties:
          mail:
            smtp:
              auth: true
              starttls:
                enable: true
                required: true

    四、编写工具类

    @Component
    public class MyEmailUtil {
    
        @Autowired
        private JavaMailSender javaMailSender;
    
        public void sendEmail(String sendTo, String title, String content, File file) {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    
            try {
                MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
                mimeMessageHelper.setFrom("12345678@qq.com");
                mimeMessageHelper.setTo(sendTo);
                mimeMessageHelper.setSubject(title);
                mimeMessageHelper.setText(content);
                if (file.exists()) {
                    FileSystemResource fileSystemResource = new FileSystemResource(file);
                    mimeMessageHelper.addAttachment(file.getName(), fileSystemResource);
                }
            } catch (MessagingException e) {
                e.printStackTrace();
            }
    
            javaMailSender.send(mimeMessage);
        }
    }
  • 相关阅读:
    poj 3264 Balanced Lineup
    poj 2762 Going from u to v or from v to u?
    hdu 3671 Boonie and Clyde
    zoj 3195 Design the city
    poj 1523 SPF
    Codeforces Polo the Penguin and Matrix
    MVC原理的简述(转)
    C#访问权限修饰符
    XML Schema介绍
    Sql批量删除/插入
  • 原文地址:https://www.cnblogs.com/linding/p/12601499.html
Copyright © 2011-2022 走看看