0
点赞
收藏
分享

微信扫一扫

Java 定时发送邮件

小安子啊 2022-02-28 阅读 79
javasqllinq

1.开启SMTP

        登录要发送邮件的邮箱,进入设置开启SMTP服务,并且拿到标识码。

2.添加依赖

     <!-- httpclient 依赖 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.12</version>
        </dependency>
         <!-- 邮件 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.4.3</version>
        </dependency>

        <!-- 打包插件 -->
         <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>

3.配置文件

#邮箱的配置
spring:
  mail:
    host: smtp.163.com #发送邮件服务器
    username: 13283341120@163.com #QQ邮箱
    password: OZOBQZLBLXMGNHLG #客户端授权码  这个码都有的哈
    protocol: smtp #发送邮件协议
    properties.mail.smtp.port: 25 #端口号465或587
    default-encoding: utf-8
she:
  mail: 1835350825@qq.com

4.启动类

package com.example.emaildemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

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

5.SendMessage

package com.example.emaildemo.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.swing.plaf.PanelUI;
import java.io.IOException;

@Component
public class SendMessage {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${she.mail}")
    private String[] sheMail;

    public void sendMessage(String subject,String message) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(from);
            helper.setTo(sheMail);
            helper.setSubject(subject);
            helper.setText(message,true);
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            System.out.println(e);
        }
    }

    public String getOnes() {
        try {
            HttpClient client = HttpClients.createDefault();
            HttpGet get = new HttpGet("https://chp.shadiao.app/api.php");
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity, "utf-8");
            return responseString;
        } catch (IOException e) {
            System.out.println(e);
        }
        return null;
    }
}

6.MyScheduled

package com.example.emaildemo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
public class MyScheduled {
    @Autowired
    private SendMessage sendMessage;

    @Scheduled(cron ="0 20 17 * *  *")
    public void dsrw(){
        String message = sendMessage.getOnes();
        sendMessage.sendMessage("来自哲的消息!❤",message);
    }

}
举报

相关推荐

0 条评论