springboot实现邮件发送功能

springboot实现邮件发送功能:

1.整体目录结构:

2.新建一个springboot项目:在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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springbootmail</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    </dependencies>


</project>

3.在resources文件目录下写application.yml配置文件如下:

server:
  port: 8999

spring:
  application:
    name: spring-boot-mail

  mail:
    host: smtp.qq.com
    username: XX@qq.com
    # 记得在qq邮箱,设置-》账户-》POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务开启pop3/smtp服务,获得授权码
    password: qomvhauwjtvkjeaa
    default-encoding: utf-8
    #如果不写下面这3句,会报530错误
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

# 表面发送者
mail:
  fromMail:
    addr: xx@qq.com

4.编写发送邮件服务接口,实现发送邮件功能:

package com.nt.service;
 /**

  * 邮件服务接口
    */
public interface MailService {

    /**

    * 发送简单邮件
    * @param to
    * @param subject
    * @param content
    */
    public void sendMail(String to,String subject,String content);
    }
}
package com.nt.service.impl;

import com.nt.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**

* 邮件服务类
*/
@Service
public class MailServiceImpl implements MailService {

   @Autowired
   private JavaMailSender mailSender;

   @Value("${mail.fromMail.addr}")
   private String form;

   /**

   发送简单邮件

   @param to  接受者

   @param subject  主题

   @param content  内容
   */
   @Override
   public void sendMail(String to, String subject, String content) {
       SimpleMailMessage mailMessage=new SimpleMailMessage();
       mailMessage.setFrom(form);//发起者
       mailMessage.setTo(to);//接受者
       //如果发送给多个人的
       //mailMessage.setTo("1xx.com","2xx.com","3xx.com");
       mailMessage.setSubject(subject);
       mailMessage.setText(content);
       try {
           mailSender.send(mailMessage);
           System.out.println("发送简单邮件");
       }catch (Exception e){
           System.out.println("发送简单邮件失败");
       }
   }
}

5.编写邮件测试类:

import com.nt.Application;
import com.nt.service.MailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MailTest {

     @Autowired
     private MailService mailService;
 
     @Value("${mail.fromMail.addr}")
     private String form;

     @Test
     public void sendSimpleMail(){
         mailService.sendMail(form,"简单邮件","SpringBoot实现邮件发送");
     }
 }

6.记得在根目录下编写个springboot的启动类,如果没写的,会报错哦。

package com.nt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

7.run测试类MailTest:,查看结果:

实现定时器发送:

1.创建springboot项目:

编写定时器,可以看下:springboot实现定时器

编写邮件服务类:可以看下:springboot发送简单邮件

2.启动类启用定时:

@SpringBootApplication
@EnableScheduling//开启定时
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

3.主要业务代码:

package com.nt.util;

import com.nt.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**

定时任务类,实现定时发送邮件
*/
 //要声明为bean,没有声明启动类启动无法实现定时效果
@Component
public class SchedulerTask {

    @Autowired
    private MailService mailService;

    @Value("${mail.fromMail.addr}")
    private String form;

    private int count=1;

    //表示每隔6秒发送一次邮件
    @Scheduled(cron = "*/6 * * * * ?")
    private void process(){
        String content="springboot整合定时器实现定时邮件发送,这是第"+(count++)+"封邮件";
        mailService.sendMail(form,"简单邮件",content);
        System.out.println("发送定时邮件成功");
    }

}
package com.nt.service.impl;

import com.nt.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**

邮件服务类
*/
 @Service
 public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String form;

    /**

     * 发送简单邮件
     * @param to  接受者
     * @param subject  主题
     * @param content  内容
     */
   @Override
   public void sendMail(String to, String subject, String content) {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom(form);//发起者
        mailMessage.setTo(to);//接受者
        //如果发送给多个人的
        //mailMessage.setTo("1xx.com","2xx.com","3xx.com");
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        try {
            mailSender.send(mailMessage);
            System.out.println("发送简单邮件");
        }catch (Exception e){
            System.out.println("发送简单邮件失败");
        }
   }
 }

4.run启动类application.class,测试效果:

注:有可能邮件会发送失败的原因:邮件发送过于频繁,网络异常等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值