打造全方位接口自动化测试平台:邮件及消息发送功能实现

目前正在着手开发一款功能全面的接口自动化测试平台,该平台将涵盖登录、用户管理、权限控制、项目管理、用例管理、测试数据管理、测试报告生成、任务调度、缺陷跟踪以及系统配置等多个核心模块。为了发送测试报告,或者测试节点中的某些信息,我们需要开发一个邮件发送、飞书群机器人相关的消息发送功能。本篇内容主要简单介绍一下这些功能的实现。

在这里插入图片描述

技术选型

  • 后端:采用Java作为主要开发语言。
  • 前端:使用VUE框架进行界面展示。
  • 消息发送: 发送邮件消息,或者webhook发送群机器人消息。

发送邮件

使用JavaMailSender接口来发送邮件,需要在配置文件中配置邮件服务器相关属性。

#email
spring.mail.host=smtp.youremail.com
spring.mail.port=587
spring.mail.username=youremail.com
spring.mail.password=yourpasswrod
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
@Slf4j
public class SendMsg {

    @Autowired
    private JavaMailSender javaMailSender;

    public boolean email(String to, String subject, String text, String filePath) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true); // true表示发送HTML格式的邮件
            helper.addAttachment(filePath.substring(filePath.lastIndexOf("/") + 1), new File(filePath)); // 添加附件,文件名使用路径中的最后一部分
            javaMailSender.send(message);
            log.info("发送邮件成功 {}", text);
            return true;
        } catch (MessagingException e) {
            log.error("发送邮件失败 {}", e.getMessage());
            return false;
        }
    }
}

发送飞书群机器人消息

我们使用飞书签名校验,genSign生成签名秘钥。

@Slf4j
public class SendMsg {

    @Value("${feishu.webhook.url}")
    private String webHookUrl;

    @Value("${feishu.webhook.secret}")
    private String webHookSecret;

    public boolean feishu(String msgType, Map<String, Object> message) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
        int timestamp = (int) Instant.now().toEpochMilli();
        String sign = genSign(webHookSecret, timestamp);
        message.put("timestamp", String.valueOf(timestamp));
        message.put("sign", sign);
        message.put("msg_type", msgType);
        OkHttpClient okHttpClient = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
        RequestBody requestBody = RequestBody.create(mediaType, ByteString.encodeUtf8(message.toString()));
        Request request = new Request.Builder().url(webHookUrl).method("POST",requestBody).addHeader("Content-Type", "application/json").build();
        Response response = okHttpClient.newCall(request).execute();
        String strResponse = response.body().string();
        JSONObject jsonResponse = JSON.parseObject(strResponse);
        if (jsonResponse.get("StatusMessage").equals("success")) {
            log.info("发送飞书消息成功");
            return true;
        }else {
            log.error("发送飞书消息失败");
            return false;
        }

    }


    private static String genSign(String secret, int timestamp) throws NoSuchAlgorithmException, InvalidKeyException {
        //把timestamp+"\n"+密钥当做签名字符串
        String stringToSign = timestamp + "\n" + secret;
        //使用HmacSHA256算法计算签名
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] signData = mac.doFinal(new byte[]{});
        return new String(Base64.encodeBase64(signData));
    }
}

当然,也可以使用钉钉或者企业微信的群机器人发送消息,具体方式和飞书基本一致,可以参考官方开发文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值