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

java发送html模板的高逼格邮件

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

java发送html模板的高逼格邮件

最近做了一个监测k8s服务pod水平伸缩发送邮件的功能(当pod的cpu/内存达到指定阈值后会水平扩展出多个pod、或者指定时间内pod数应扩展到指定数量),一开始写了个格式很low的邮件,像下面这样:

主流程打通,算个v1版本吧,程序员是个追求完美的人,再说这么低逼格的邮件,给客户看,客户也会不满意。那怎么提高邮件的逼格呢?下面写了个简单的demo,v2版本如下:

感兴趣的小伙伴可以参考,模板可以找你公司前端和美工小姐姐设计。

因为监测k8s服务pod水平伸缩是用go开发的,发送通知邮件提供了个接口,用springboot写的,以下也用springboot做demo

Springboot的pom.xml文件:



    4.0.0
    
 org.springframework.boot
 spring-boot-starter-parent
 2.1.2.RELEASE
  
    
    com.example
    email-demo
    0.0.1-SNAPSHOT
    email-demo
    Demo project for Spring Boot

    
 1.8
    

    
 
     org.springframework.boot
     spring-boot-starter-web
 

 
     org.springframework.boot
     spring-boot-starter-test
     test
 
 
     org.apache.commons
     commons-lang3
     3.8.1
 
 
     com.alibaba
     fastjson
     1.2.47
 

 
     org.springframework.boot
     spring-boot-starter-mail
 
    

    
 
     
  org.springframework.boot
  spring-boot-maven-plugin
     
 
    


pod-scale-alarm.html模板文件:
模板中的{0}、{1}这样的占位符后面java代码会替换掉


Confidential - Scale Alarm Use only
服务实例水平伸缩通知

Hi,

{1}

{2}

{3} {4}

success-alarm.png图标:

java代码如下,简单的demo,优化可以自己在项目中去做。

package com.example.emaildemo;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Properties;



public class SendEmailUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(SendEmailUtil.class);

    public static void main(String[] args) throws MessagingException, IOException {

 JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
 javaMailSender.setUsername("你的邮箱地址");
 javaMailSender.setPassword("你的邮箱AUTH密码,不是登陆密码哦,在邮箱的设置里单独开启和设置");
 javaMailSender.setHost("smtp.exmail.qq.com");
 javaMailSender.setPort(587);
 javaMailSender.setDefaultEncoding("UTF-8");
 Properties props = new Properties();
 props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
 props.setProperty("mail.transport.protocol", "smtp");
 props.setProperty("mail.smtp.auth", "true");
 props.setProperty("mail.smtp.connectiontimeout", "20000");
 props.setProperty("mail.smtp.timeout", "20000");
 javaMailSender.setJavaMailProperties(props);

 MimeMessage message = javaMailSender.createMimeMessage();
 MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
 helper.setTo(new String[]{"收件人邮箱"});
 helper.setCc("抄送人邮箱");
 helper.setFrom("你的邮箱地址");
 helper.setSubject("liang subject");
 helper.setText(buildContent(), true);


 String alarmIconName = "success-alarm.png";
 ClassPathResource img = new ClassPathResource(alarmIconName);
 if (Objects.nonNull(img)) {
     helper.addInline("icon-alarm", img);
 }
 javaMailSender.send(message);
    }

    private static String buildContent() throws IOException {

 //加载邮件html模板
 String fileName = "pod-scale-alarm.html";
 InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
 BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
 StringBuffer buffer = new StringBuffer();
 String line = "";
 try {
     while ((line = fileReader.readLine()) != null) {
  buffer.append(line);
     }
 } catch (Exception e) {
     LOGGER.error("读取文件失败,fileName:{}", fileName, e);
 } finally {
     inputStream.close();
     fileReader.close();
 }


 String contentText = "以下是服务实例伸缩信息, 敬请查看.
below is the information of service instance scale, please check. "; //邮件表格header String header = "分区(Namespace)服务(Service)伸缩结果(Scale Result)伸缩原因(Scale Reason)当前实例数(Pod instance number)"; StringBuilder linesBuffer = new StringBuilder(); linesBuffer.append("" + "myNamespace" + "" + "myServiceName" + "" + "myscaleResult" + "" + "" + "mReason" + "" + "my4" + ""); //绿色 String emailHeadColor = "#10fa81"; String date = DateFormatUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss"); //填充html模板中的五个参数 String htmlText = MessageFormat.format(buffer.toString(), emailHeadColor, contentText, date, header, linesBuffer.toString()); //改变表格样式 htmlText = htmlText.replaceAll("", ""); htmlText = htmlText.replaceAll("", ""); return htmlText; } }

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

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

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