前言
本文通过htmlmail实现发送html样式表格,html可设计任意样式,但有一定区别,这里设计一个excel表格并带附件和图片
话不多说,直接上代码
一、准备工作
这里需要导包,给出包名,可自己去查找对照添加就好
import com.iss.iescp.webfwk.context.PlatformContext;
import com.taiping.claimcore.vo.MailVO;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Objects;
import java.util.Properties;
二.设计一个MailUtil类
代码如下(示例):
public class MailUtil {
private final static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
public static boolean send(MailVO mail) {
// 发送email
HtmlEmail email = new HtmlEmail();
try {
// 这里是SMTP发送服务器的名字:163的如下:"smtp.163.com"
email.setHostName(mail.getHost());
// 字符编码集的设置
email.setCharset(MailVO.ENCODEING);
// 收件人的邮箱
email.addTo(mail.getReceiver());
// 发送人的邮箱
email.setFrom(mail.getSender(), mail.getName());
// 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
email.setAuthentication(mail.getUsername(), mail.getPassword());
// 要发送的邮件主题
email.setSubject(mail.getSubject());
// 要发送的信息,由于使用了HtmlEmail,可以在邮件内容中使用HTML标签
email.setHtmlMsg(mail.getHtml());
// 要发送的附件
if (mail.getFile() != null) {
email.embed(mail.getFile());
}
//加入图片
File file = new File(mail.getImg());
email.embed(file, "image");//给出cid
// 发送
email.send();
if (logger.isDebugEnabled()) {
logger.debug(mail.getSender() + " 发送邮件到 " + mail.getReceiver());
}
return true;
} catch (EmailException e) {
e.printStackTrace();
logger.info(mail.getSender() + " 发送邮件到 " + mail.getReceiver() + " 失败");
return false;
}
}
注意!!!!:这里的图片,使用html不能直接用src=“”加入,需要导入附件以后,给出一个cid标识,再将图片加入正文(当时踩了很多坑)
三.实现类
代码如下(示例):
package com.taiping.claimcore.Email.service;/**
* @author TuGiant
* @param
* @return
* @throws
* @date
*/
import com.iss.iescp.devfwk.exception.ServiceException;
import com.taiping.claimcore.Email.Vo.EmailVo;
import com.taiping.claimcore.util.MailUtil;
import com.taiping.claimcore.vo.MailVO;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
/**
* @program:
* @description:
* @author: TuGiant
* @create: 2021-07-14 09:51
**/
public class SendEmailService {
public void email(List<EmailVo> emaillist) throws ServiceException {
try {
//映射字段判空
for (int i = 0; i < emaillist.size(); i++) {
EmailVo emailVo = emaillist.get(i);
Class<?> clazz = emailVo.getClass();
Field[] declaredFields = clazz.getDeclaredFields();
Arrays.asList(declaredFields).stream().forEach(field -> {
try {
String filedName = field.getName();
PropertyDescriptor pd = new PropertyDescriptor(filedName, clazz);
Method readMethod = pd.getReadMethod();
Object value = readMethod.invoke(emailVo);
if (value == null) {
Method writeMethod = pd.getWriteMethod();
writeMethod.invoke(emailVo, "");
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
MailVO mailVO = new MailVO();
mailVO.setHost("smtp.stluciabj.com");//SMTP发送服务器的名字(这里用的网易企业邮箱)
mailVO.setSender("××××××@×××.com");//发件人
mailVO.setName("××××××@×××.com");//发件人昵称
mailVO.setUsername("××××××@×××.com");//账号
mailVO.setPassword("×××××××××××××××");//授权密码(在邮箱里获取)
mailVO.setSubject("测试");//主题
mailVO.setReceiver(emaillist.get(0).getReceiver());
String excelhtml = excelhtml(emaillist);//生成表格
mailVO.setHtml(excelhtml);//添加表格html
String img = this.getClass().getClassLoader().getResource("/file/嘻嘻.jpg").getPath();//加入图片
if ("".equals(emaillist.get(0).getReceiver())) {
throw new ServiceException("发送失败, 收件人为空");
}
if ("".equals(emaillist.get(0).getExcelPath())) {
throw new ServiceException("发送失败,附件地址为空");
}
mailVO.setFile(new File(emaillist.get(0).getExcelPath()));//添加附件地址
mailVO.setImg(img);
if ("".equals(emaillist.get(0).getCBtachType()) || emaillist.get(0).getCBtachType() == null) {
throw new ServiceException("发送失败, 性质为空");
}
MailUtil.send(mailVO);//发送
} catch (Exception e) {
throw new ServiceException("发送失败", e);
}
}
private String excelhtml(List<EmailVo> emaillist) {
BigDecimal NPayNum = new BigDecimal("0.00");
for(EmailVo emailVos : emaillist){
BigDecimal b1 = new BigDecimal(emailVos.getNPayNum());
NPayNum = NPayNum.add(b1);
}
//=============================网页1===============================
String htmlpustart = "<!DOCTYPE html>\n" +
"<html lang=\"zh\">\n" +
"<head>\n" +
" <title>demo</title>\n" +
"<style type=\"text/css\">\n" +
" table.reference, table.tecspec {\n" +
" border-collapse: collapse;\n" +
" width: 100%;\n" +
" margin-bottom: 4px;\n" +
" margin-top: 4px;\n" +
" position: relative;\n" +
" }\n" +
" table.reference th {\n" +
" border: 1px solid #555;\n" +
" font-size: 12px;\n" +
" padding: 3px;\n" +
" vertical-align: top;\n" +
" }\n" +
" table.reference td {\n" +
" line-height: 2em;\n" +
" min-width: 24px;\n" +
" border: 1px solid #555;\n" +
" padding: 5px;\n" +
" padding-top: 7px;\n" +
" padding-bottom: 7px;\n" +
" vertical-align: top;\n" +
" }\n" +
" .article-body h3 {\n" +
" font-size: 1.8em;\n" +
" margin: 2px 0;\n" +
" line-height: 1.8em;\n" +
" }\n" +
" </style>" +
"</head>\n" +
"<body>\n" +
" <table class=\"reference\">\n" +
" <tbody>\n" +
" <tr>\n" +
" <td>致:</td>\n" +
" <td>" +
emaillist.get(0).getCHospNme() +
"</td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td>" +
"</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>批次号:</td>\n" +
" <td>" +
emaillist.get(0).getCBtachNo() +
"</td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
"<img src=\"cid:image" +
"\" style=\"width: 240px\">" +
" </tr>\n" +
" <tr>\n" +
" <td>支付金额:</td>\n" +
" <td>" +
NPayNum +
"</td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>支付日期:</td>\n" +
" <td>" +
emaillist.get(0).getTPayTm() +
"</td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" </tr>" +
" <tr>\n" +
" <th>\n" +
" <div>序 号</div>\n" +
" <div>No</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>件号</div>\n" +
" <div>Claim#</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>公司</div>\n" +
" <div>Insurer</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>会员号</div>\n" +
" <div>Membership#</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>姓名</div>\n" +
" <div>Patient's Name</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>日期</div>\n" +
" <div>Service</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>号码</div>\n" +
" <div>Invoice#</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>金额1</div>\n" +
" <div>Amount Bill</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>金额</div>\n" +
" <div>Amount Paid</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>金额</div>\n" +
" <div>Amount Refused</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>原因</div>\n" +
" <div>Explanation</div>\n" +
" </th>\n" +
" </tr>\n";
//=============================网页2===============================
String htmlprstart = "<!DOCTYPE html>\n" +
"<html lang=\"zh\">\n" +
"<head>\n" +
" <title>demo</title>\n" +
"<style type=\"text/css\">\n" +
" table.reference, table.tecspec {\n" +
" border-collapse: collapse;\n" +
" width: 100%;\n" +
" margin-bottom: 4px;\n" +
" margin-top: 4px;\n" +
" position: relative;\n" +
" }\n" +
" table.reference th {\n" +
" border: 1px solid #555;\n" +
" font-size: 12px;\n" +
" padding: 3px;\n" +
" vertical-align: top;\n" +
" }\n" +
" table.reference td {\n" +
" line-height: 2em;\n" +
" min-width: 24px;\n" +
" border: 1px solid #555;\n" +
" padding: 5px;\n" +
" padding-top: 7px;\n" +
" padding-bottom: 7px;\n" +
" vertical-align: top;\n" +
" }\n" +
" .article-body h3 {\n" +
" font-size: 1.8em;\n" +
" margin: 2px 0;\n" +
" line-height: 1.8em;\n" +
" }\n" +
" </style>" +
"</head>\n" +
"<body>\n" +
" <table class=\"reference\">\n" +
" <tbody>\n" +
" <tr>\n" +
" <td>公司:</td>\n" +
" <td>" +
emaillist.get(0).getCInsuCompanyNme() +
" </td>\n" +
" <td></td>\n" +
" <td>收款人:</td>\n" +
" <td>" +
emaillist.get(0).getCPayeeNme() +
"</td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td>" +
"</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>姓名:</td>\n" +
" <td>" +
emaillist.get(0).getCInsuredNme() +
"</td>\n" +
" <td></td>\n" +
" <td>账户:</td>\n" +
" <td>" +
emaillist.get(0).getCBankAccountNo() +
"</td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
"<img src=\"cid:image" +
"\" style=\"width: 240px\">" +
" </tr>\n" +
" <tr>\n" +
" <td>单号:</td>\n" +
" <td>" +
emaillist.get(0).getCPlyNo() +
"</td>\n" +
" <td></td>\n" +
" <td>银行:</td>\n" +
" <td>" +
emaillist.get(0).getCBeneficiaryBank() +
"</td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>会员号:</td>\n" +
" <td>" +
emaillist.get(0).getCPlyPartNo() +
"</td>\n" +
" <td></td>\n" +
" <td>日期1:</td>\n" +
" <td>" +
emaillist.get(0).getTPayTm() +
"</td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" <td></td>\n" +
" </tr>" +
" <tr>\n" +
" <th>\n" +
" <div>序 号</div>\n" +
" <div>No</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>案件号</div>\n" +
" <div>Claim#</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>机构</div>\n" +
" <div>CareProvider</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>日期</div>\n" +
" <div>Service</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>号码</div>\n" +
" <div>Invoice#</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>金额3</div>\n" +
" <div>Amount Bill</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>金额2</div>\n" +
" <div>Amount Paid</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>金额1</div>\n" +
" <div>Amount Refused</div>\n" +
" </th>\n" +
" <th>\n" +
" <div>原因</div>\n" +
" <div>Explanation</div>\n" +
" </th>\n" +
" </tr>\n";
String htmlend = "</tbody>\n" +
" </table>\n" +
"</body>\n" +
"</html>";
String kkk = "";
if ("PU".equals(emaillist.get(0).getCBtachType())) {
for (int i = 0; i < emaillist.size(); i++) {
String html = " <tr>\n" +
" <td>" +
(i + 1) +
"</td>\n" +
" <td>" +
emaillist.get(i).getCClmCaseNo() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCInsuCompanyNme() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCPlyPartNo() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCInsuredNme() +
"</td>\n" +
" <td>" +
emaillist.get(i).getTOutpatientTm() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCInvoiceNum() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCBillNum() +
"</td>\n" +
" <td>" +
emaillist.get(i).getNPayNum() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCRefusedNum() +
"</td>\n" +
" <td>" +
"</td>\n" +
" </tr>\n";
kkk = kkk + html;
}
return htmlpustart + kkk + htmlend;//拼接html
} else if ("PR".equals(emaillist.get(0).getCBtachType())) {
for (int i = 0; i < emaillist.size(); i++) {
String html = " <tr>\n" +
" <td>" +
(i + 1) +
"</td>\n" +
" <td>" +
emaillist.get(i).getCClmCaseNo() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCHospNme() +
"</td>\n" +
" <td>" +
emaillist.get(i).getTOutpatientTm() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCInvoiceNum() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCBillNum() +
"</td>\n" +
" <td>" +
emaillist.get(i).getNPayNum() +
"</td>\n" +
" <td>" +
emaillist.get(i).getCRefusedNum() +
"</td>\n" +
" <td>" +
"</td>\n" +
" </tr>\n";
kkk = kkk + html;
}
return htmlprstart + kkk + htmlend;//拼接html
}
return null;
}
}
这里设计了两个表格,根据入参选择的表格,仅供参考
效果展示
总结
如果觉得有用,三连一下!!!!