用Java实现每天给对象发情话

一、引言

最近看到一篇用js代码实现表白的文章,深有感触。
然后发现自己也可以用java代码实现,然后就开始写代码了,发现还挺有意思的,话不多说开搞
实现思路:
使用HttpClient远程获取彩虹屁生成器网站中的内容 网站:https://chp.shadiao.app/

java Mail 实现发送邮件

SpringBoot 整合Scheduled 实现定时发送邮件

二、搭建项目

项目环境在SpringBoot框架基础上,加入邮件发送mail、RPC远程调用httpclient、Scheduled 的一个Maven项目,依赖如下:

org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <!-- httpclient 依赖 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.12</version>
    </dependency>
</dependencies>

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

三、编写配置

在编写配置前需要,在浏览器登录自己的邮箱在账号安全中设置开启POP3/SMTP服务在这里插入图片描述
开始开启POP3/SMTP服务需要输入验证码在这里插入图片描述
复制授权码在这里插入图片描述
勾选SMTP发信后保存到服务器,勾选这一项主要是可以看到自己发送了什么信息,不勾选此项。邮件消息发送成功后,邮箱内看不到自己已发送的信息在这里插入图片描述
根据授权码编写配置spring:
mail:
username: [email protected] # 自己邮箱地址
password: xxxxxxx # SMTP|POP3|IMAP协议授权码
host: smtp.qq.com # 服务器地址。参考邮箱服务运营商提供的信息。
properties:
mail:
smtp:
auth: true # 开启smtp协议验证
port: 587

发给谁的邮箱

she:
mail: [email protected]

四、编写SpringBoot启动类

@EnableScheduling
@SpringBootApplication
public class BiaoBaiApp {
public static void main(String[] args) {
SpringApplication.run(BiaoBaiApp.class,args);
}
五、自动生成发送内容

扫描二维码关注公众号,回复: 12779218 查看本文章

@Component
public class SendMessage {
@Autowired
private JavaMailSender mailSender;
@Value(" s p r i n g . m a i l . u s e r n a m e " ) p r i v a t e S t r i n g f r o m ; @ V a l u e ( " {spring.mail.username}") private String from; @Value(" spring.mail.username")privateStringfrom;@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);//发件内容
        mailSender.send(helper.getMimeMessage());//发送邮件
    } catch (MessagingException e) {
        e.printStackTrace();
    }

}
/**远程获取要发送的信息*/
public static String getOneS(){
    try {
        //创建客户端对象
        HttpClient client = HttpClients.createDefault();
        /*创建地址 https://du.shadiao.app/api.php*/
        HttpGet get = new HttpGet("https://chp.shadiao.app/api.php");
        //发起请求,接收响应对象
        HttpResponse response = client.execute(get);
        //获取响应体,响应数据是一种基于HTTP协议标准字符串的对象
        //响应体和响应头,都是封装HTTP协议数据。直接使用可能出现乱码或解析错误
        HttpEntity entity = response.getEntity();
        //通过HTTP实体工具类,转换响应体数据
        String responseString = EntityUtils.toString(entity, "utf-8");

        return responseString;

    } catch (IOException e) {
        throw  new RuntimeException("网站获取句子失败");
    }
}

}

六、编写定时任务

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

/*定时执行任务方法 每天5点20执行该任务*/
@Scheduled(cron ="0 20 17 * * *")
public void dsrw(){
    String message = sendMessage.getOneS();
    sendMessage.sendMessage("来自清茶淡粥的消息!❤",message);
}

}
七、打包运行

有条件的可以吧jar包放在运服务器上,没有条件的可以在本地win10系统上添加定时任务,每天定时执行jar包。
jar包放在服务器上需要放行端口:587 ,防火墙放行587端口
除了放行,还有放行 http 端口 和 https端口在这里插入图片描述
然后在linux上后台启动jar包
nohup java -jar jar包 >test.log &
win10 定时运jar 包 在任务计划程序中创建任务在这里插入图片描述
新建触发器在这里插入图片描述
新建操作,在程序或脚本输入执行的jar命令,点击确定在这里插入图片描述
然后可以看见,创建好的任务在这里插入图片描述
八、总结

代码还有很大的提升,也有很多不足之处。
由于时间原因,可优化的地方还很多,比如:发送单纯的文字内容的邮件,不美观,可以实现html方式发送邮件,使发送邮件内容更加美观。
public void sendHtmlMessage(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);//true 使用html 方式发送
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
e.printStackTrace();
}
小编还有很多不足之处,欢迎大家在评论区留言一起讨论

猜你喜欢

转载自blog.csdn.net/dcj19980805/article/details/114880657
今日推荐