关于java:程序员的浪漫用-java-实现每天给对象法发情话

7次阅读

共计 3765 个字符,预计需要花费 10 分钟才能阅读完成。

一、引言

最近看到一篇用 js 代码实现表白的文章,深有感触。

而后发现自己也能够用 java 代码实现,而后就开始写代码了,发现还挺有意思的,话不多说开搞

实现思路:

  • 应用 HttpClient 近程获取彩虹屁生成器网站中的内容 网站:https://chp.shadiao.app/
  • java Mail 实现发送邮件
  • SpringBoot 整合 Scheduled 实现定时发送邮件

二、搭建我的项目

我的项目环境在 SpringBoot 框架根底上,退出邮件发送 mail、RPC 近程调用 httpclient、Scheduled 的一个 Maven 我的项目,依赖如下:

 `<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>
    <dependencies>
        
        <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: xxxxxx@qq.com  # 本人邮箱地址
    password: xxxxxxx        # SMTP|POP3|IMAP 协定受权码
    host: smtp.qq.com        # 服务器地址。参考邮箱服务运营商提供的信息。properties:
      mail:
        smtp:
          auth: true # 开启 smtp 协定验证
    port: 587 

# 发给谁的邮箱
she:
  mail: xxxxxxx@163.com` 

四、编写 SpringBoot 启动类

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

五、主动生成发送内容

`@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);// 发件内容
            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();
        }` 

最初附上源码供大家参考:https://download.csdn.net/dow…

我的项目举荐:

2000 多 G 的计算机各行业电子资源分享(继续更新)

2020 年微信小程序全栈我的项目之喵喵交友【附课件和源码】

Spring Boot 开发小而美的集体博客【附课件和源码】

Java 微服务实战 296 集大型视频 - 谷粒商城【附代码和课件】

Java 开发微服务畅购商城实战【全 357 集大我的项目】- 附代码和课件

最全最具体数据结构与算法视频 -【附课件和源码】​​​​​​​

正文完
 0