本文共 4460 字,大约阅读时间需要 14 分钟。
1.通过配置文件设置发送邮件发送方,接收方,抄送方 2.目前实现了 自定义发送内容3.实现了excel 邮件发送内容
package com.javatool.email.proxy;import com.javatool.configer.DefaultConfigerContext;import com.javatool.email.model.MailAuthenticator;import com.javatool.email.model.SendMailConfigModel;import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import java.util.Date;import java.util.List;import java.util.Properties;/** * @author haisong * @create 2018/09/13 14:21 */public class SendMailManager { /** * 发送邮件 * @param subject 邮件主题 * @param content 邮件内容 * @return success 发送成功 failure 发送失败 * @throws Exception */ public static String sendMail( String subject, String content) throws Exception { SendMailConfigModel sendMailConfigModel = DefaultConfigerContext.getInstance().getModelFromProperties(SendMailConfigModel.class); String to = sendMailConfigModel.getTo(); if (to != null){ Properties props = System.getProperties(); props.put("mail.smtp.host", sendMailConfigModel.getMail_smtp_host()); props.put("mail.smtp.auth", "true"); props.put("mail.transport.protocol", "smtp"); MailAuthenticator auth = new MailAuthenticator(); Session session = Session.getInstance(props, auth); session.setDebug(true); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(sendMailConfigModel.getFrom())); if (!to.trim().equals("")) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(to.trim())); } ListcopyToList = sendMailConfigModel.getCopyToList(); if(copyToList!=null&©ToList.size()>0) { Address[] addresses = new Address[copyToList.size()]; int i = 0; for (String copy : copyToList) { addresses[i] = new InternetAddress(copy); i++; } message.addRecipients(Message.RecipientType.CC, addresses); } message.setSubject(subject); MimeBodyPart mbp1 = new MimeBodyPart(); // 正文 mbp1.setContent(content, "text/html;charset=utf-8"); Multipart mp = new MimeMultipart(); // 整个邮件:正文+附件 mp.addBodyPart(mbp1); message.setContent(mp); message.setSentDate(new Date()); message.saveChanges(); Transport trans = session.getTransport("smtp");// trans.connect("smtp.163.com", sendMailConfigModel.getUser(), sendMailConfigModel.getPassword()); trans.send(message); System.out.println(message.toString()); } catch (Exception e){ e.printStackTrace(); return "failure"; } return "success"; }else{ return "failure"; } } public static String SendExcelMail(String subject,List
> conteList) throws Exception { String htmlStr =""; for(int i=0;i data,boolean headFlage,boolean endFlage) { String html_msg=""; if(headFlage){ html_msg = " "+data.get(column)+""; } html_msg = html_msg+" "+data.get(column)+""; } html_msg = html_msg+" "; html_msg = html_msg+"
"; } return originHtml+html_msg; }}"; for(int column=0;column "; }else{ html_msg = html_msg+""; for(int column=0;column "; } if(endFlage){ html_msg = html_msg + "
sendMail.mail_smtp_host=smtp.163.comsendMail.mail_smtp_auth=loginsendMail.user=***@163.comsendMail.password=******sendMail.from=*****@163.comsendMail.to=auguszero@163.com
如果你觉得可以 可以直接在github 上将该项目打包好 进行引入 作为工具类 。
同时欢迎大家一起来完善这个github项目 。
欢迎一起交流: