Java

1、springboot + Java Mail + Html

   项目结构:

   

   pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lfy.cn</groupId>
  <artifactId>springbootTest-1.0.0</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springbootTest-1.0.0</name>
  <url>http://maven.apache.org</url>
  
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--springboot支持Java Mail的配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

  pod-scale-alarm.html

<body style="color: #666; font-size: 14px; font-family: 'Open Sans',Helvetica,Arial,sans-serif;">
<div class="box-content" style=" 80%; margin: 20px auto; max- 800px; min- 600px;">
    <div class="header-tip" style="font-size: 12px;
                                   color: #aaa;
                                   text-align: right;
                                   padding-right: 25px;
                                   padding-bottom: 10px;">
        Confidential - Scale Alarm Use Only
    </div>
    <div class="info-top" style="padding: 15px 25px;
                                 border-top-left-radius: 10px;
                                 border-top-right-radius: 10px;
                                 background: {0};
                                 color: #fff;
                                 overflow: hidden;
                                 line-height: 32px;">
        <img src="cid:icon-alarm" style="float: left; margin: 0 10px 0 0;  32px;" /><div style="color:#010e07"><strong>{5}</strong></div>
    </div>
    <div class="info-wrap" style="border-bottom-left-radius: 10px;
                                  border-bottom-right-radius: 10px;
                                  border:1px solid #ddd;
                                  overflow: hidden;
                                  padding: 15px 15px 20px;">
        <div class="tips" style="padding:15px;">
            <p style=" list-style: 160%; margin: 10px 0;">Hi,</p>
            <p style=" list-style: 160%; margin: 10px 0;">{1}</p>
        </div>
        <div class="time" style="text-align: right; color: #999; padding: 0 15px 15px;">{2}</div>
        <br>
        <table class="list" style=" 100%; border-collapse: collapse; border-top:1px solid #eee; font-size:12px;">
            <thead>
            <tr style=" background: #fafafa; color: #333; border-bottom: 1px solid #eee;">
                {3}
            </tr>
            </thead>
            <tbody>
            {4}
            </tbody>
        </table>
    </div>
</div>
</body>

  SendEmailController.java

package com.lfy.cn.springbootTest;

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 org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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;

@RestController
public class SendEmailController {

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

    @RequestMapping("/SendEmailController/send")
    public String send(String[] args) throws MessagingException, IOException {

        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setUsername("xxxxxxxxxx@163.com");  //代理邮箱
        javaMailSender.setPassword("授权码");
        javaMailSender.setHost("smtp.163.com");
        javaMailSender.setPort(25);
        javaMailSender.setDefaultEncoding("UTF-8");
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", "smtp.163.com");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.auth", "true");   //SSL加密
        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[]{"xxxxxxx@qq.com"});
        helper.setCc("xxxxxxxxx@163.com");
        helper.setFrom("xxxxxxxxx@163.com");
        helper.setSubject("Html邮件模板");
        helper.setText(buildContent(), true);


        String alarmIconName = "image.jpg";
        ClassPathResource img = new ClassPathResource(alarmIconName);
        if (Objects.nonNull(img)) {
            helper.addInline("icon-alarm", img);
        }
helper.addAttachment("image.jpg", new ClassPathResource("image.jpg"));//添加附件 javaMailSender.send(message);
return "send email."; } 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 = "以下是服务实例伸缩信息, 敬请查看.<br>below is the information of service instance scale, please check. "; //邮件表格header String header = "<td>分区(Namespace)</td><td>服务(Service)</td><td>伸缩结果(Scale Result)</td><td>伸缩原因(Scale Reason)</td><td>当前实例数(Pod instance number)</td>"; StringBuilder linesBuffer = new StringBuilder(); linesBuffer.append("<tr><td>" + "myNamespace" + "</td><td>" + "myServiceName" + "</td><td>" + "myscaleResult" + "</td>" + "<td>" + "mReason" + "</td><td>" + "my4" + "</td></tr>"); //绿色 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("<td>", "<td style="padding:6px 10px; line-height: 150%;">"); htmlText = htmlText.replaceAll("<tr>", "<tr style="border-bottom: 1px solid #eee; color:#666;">"); return htmlText; } }

  App.java

package com.lfy.cn.springbootTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class,args);
    }
}

   运行结果:

     在App.java中以鼠标邮件启动springboot。

   

原文地址:https://www.cnblogs.com/ZeroMZ/p/11478819.html