栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 定时发送邮件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 定时发送邮件

1.开启SMTP

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

2.添加依赖
     
        
            org.apache.httpcomponents
            httpclient
            4.5.12
        
         
        
            org.springframework.boot
            spring-boot-starter-mail
            2.4.3
        

        
         
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
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);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749670.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号