1.封装邮件接收地址MODEL和邮件信息MODEL
package com.sicdt.jnzxgzc.common.mail.model; import java.io.Serializable; /** * * <br>类 名: MailAddress * <br>描 述: InternetAddress的封装类 * <br>作 者: shizhenwei * <br>创 建: 2017年8月4日 * <br>版 本: v1.0.0 * <br> * <br>历 史: (版本) 作者 时间 注释 */ public class MailAddress implements Serializable{ /** **/ private static final long serialVersionUID = -2636919632818478505L; private String address;//邮箱地址 private String personal;//邮箱显示名称 public MailAddress() { } public MailAddress(String address) { this.address = address; } public MailAddress(String address, String personal) { this.address = address; this.personal = personal; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPersonal() { return personal; } public void setPersonal(String personal) { this.personal = personal; } @Override public String toString() { return "AddressModel{" + "address=‘" + address + ‘\‘‘ + ", personal=‘" + personal + ‘\‘‘ + ‘}‘; } }
package com.sicdt.jnzxgzc.common.mail.model; import java.io.Serializable; import java.util.Arrays; /** * * <br>类 名: MailMessage * <br>描 述: 邮寄信息类 * <br>作 者: shizhenwei * <br>创 建: 2017年8月4日 * <br>版 本: v1.0.0 * <br> * <br>历 史: (版本) 作者 时间 注释 */ public class MailMessage implements Serializable { /** **/ private static final long serialVersionUID = 1269256094401416828L; private String subject;//主题 private String htmlText;//内容 private String charset;//编码集 private MailAddress[] to;//收件人地址 private MailAddress[] cc;//抄送人地址 private MailAddress[] bcc;//暗送人地址 private MailAddress from;//发件人地址 private MailAddress replyTo;//回复发件人地址 public MailMessage() {} public MailMessage(String subject,String plainText, String htmlText,String charset,boolean multipart, MailAddress[] to, MailAddress[] cc,MailAddress[] bcc,MailAddress from,MailAddress replyTo) { this.subject = subject; this.htmlText = htmlText; this.charset = charset; this.to = to; this.cc = cc; this.bcc = bcc; this.from = from; this.replyTo = replyTo; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getHtmlText() { return htmlText; } public void setHtmlText(String htmlText) { this.htmlText = htmlText; } public String getCharset() { return charset; } public void setCharset(String charset) { this.charset = charset; } public MailAddress[] getTo() { return to; } public void setTo(MailAddress[] to) { this.to = to; } public MailAddress[] getCc() { return cc; } public void setCc(MailAddress[] cc) { this.cc = cc; } public MailAddress[] getBcc() { return bcc; } public void setBcc(MailAddress[] bcc) { this.bcc = bcc; } public MailAddress getFrom() { return from; } public void setFrom(MailAddress from) { this.from = from; } public MailAddress getReplyTo() { return replyTo; } public void setReplyTo(MailAddress replyTo) { this.replyTo = replyTo; } @Override public String toString() { return "MailModel{" + "subject=‘" + subject + ‘\‘‘ + ", htmlText=‘" + htmlText + ‘\‘‘ + ", charset=‘" + charset + ‘\‘‘ + ", to=" + Arrays.toString(to) + ", cc=" + Arrays.toString(cc) + ", bcc=" + Arrays.toString(bcc) + ", from=" + from + ", replyTo=" + replyTo + "} "; } }
2.邮件转换工具类(这一步可能会觉得多余,我只能讲为了以后升级扩展吧)
package com.sicdt.jnzxgzc.common.mail.util; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import javax.mail.internet.InternetAddress; import org.apache.commons.lang3.ArrayUtils; import com.sicdt.jnzxgzc.common.mail.model.MailAddress; /** * * <br>类 名: EmailConvert * <br>描 述: 邮件信息转换类 * <br>作 者: shizhenwei * <br>创 建: 2017年8月4日 * <br>版 本: v1.0.0 * <br> * <br>历 史: (版本) 作者 时间 注释 */ public class EmailConvert { public static InternetAddress[] convert(MailAddress[] ams) throws UnsupportedEncodingException{ if(ArrayUtils.isEmpty(ams)) { return new InternetAddress[0]; } List<InternetAddress> ial = new ArrayList<>(); for(MailAddress am : ams){ ial.add(convert(am)); } return ial.toArray(new InternetAddress[0]); } public static InternetAddress convert(MailAddress am) throws UnsupportedEncodingException{ if(am == null) { return new InternetAddress(); } return new InternetAddress(am.getAddress(), am.getPersonal()); } }
3.邮件发送工具
package com.sicdt.jnzxgzc.common.mail.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.lang3.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.MimeMessageHelper; import com.alibaba.druid.util.StringUtils; import com.sicdt.jnzxgzc.common.mail.model.MailAddress; import com.sicdt.jnzxgzc.common.mail.model.MailMessage; /** * * <br>类 名: MailUtils * <br>描 述: 邮件发送UTILS * <br>作 者: shizhenwei * <br>创 建: 2017年8月4日 * <br>版 本: v1.0.0 * <br> * <br>历 史: (版本) 作者 时间 注释 */ public class MailUtils { private static final Logger LOG = LoggerFactory.getLogger(MailUtils.class); static{ InputStreamReader in; Properties prop = new Properties(); try { in = new InputStreamReader(MailUtils.class.getClassLoader().getResourceAsStream("config.properties"),"UTF-8"); prop.load(in); } catch (IOException e) { e.printStackTrace(); } host=prop.getProperty("mail.smtp.host"); username=prop.getProperty("mail.sender.username"); password=prop.getProperty("mail.sender.password"); timeout=prop.getProperty("mail.smtp.timeout"); username_dispaly = prop.getProperty("mail.sender.username_dispaly"); subject = prop.getProperty("mail.sender.subject"); } public static String host; public static String username; public static String username_dispaly; public static String password; public static String timeout; public static String subject; public static void main(String[] args) throws MessagingException, IOException { MailMessage message = new MailMessage(); /** * 收件人 */ MailAddress t1 = new MailAddress("389262566@qq.com"); MailAddress t2 = new MailAddress("954496177@qq.com"); message.setTo(new MailAddress[]{t1,t2}); /** * 回复人 */ message.setReplyTo(t1); /** * 消息内容 */ message.setHtmlText(readEmailTemplate("mail/mail_template_zwlxhsh.ts")); sendMail(message,true); } public static Session createSession() throws IOException{ Properties mailprop=new Properties(); mailprop.put("mail.host",host); mailprop.put("mail.transport.protocol", "smtp"); mailprop.put("mail.smtp.auth", true); //使用java发送邮件5步骤 //1.创建sesssion Session session=Session.getInstance(mailprop); //开启session的调试模式,可以查看当前邮件发送状态 session.setDebug(true); return session; } public static Transport createTransport(Session session) throws MessagingException{ //2.通过session获取Transport对象(发送邮件的核心API) Transport ts=session.getTransport(); //3.通过邮件用户名密码链接 ts.connect(username, password); return ts; } public static MimeMessage createMimeMessage(Session session) throws AddressException,MessagingException{ //创建邮件对象 MimeMessage mm=new MimeMessage(session); //设置发件人 mm.setFrom(new InternetAddress(username)); return mm; } public static void sendMail(MailMessage message) { sendMail(message, null); } public static void sendMail(MailMessage message, boolean html) { sendMail(message, null, true); } public static void sendMail(MailMessage message, File[] attachment) { sendMail(message, attachment, false); } public static void sendMail(MailMessage message, File[] attachment, boolean html) { try { Session session = createSession(); MimeMessage mm = createMimeMessage(session); if(StringUtils.isEmpty(message.getCharset())){ message.setCharset("utf-8"); } MimeMessageHelper helper = new MimeMessageHelper(mm, true, message.getCharset()); helper.setTo(EmailConvert.convert(message.getTo())); if(null != message.getCc()){ helper.setCc(EmailConvert.convert(message.getCc())); } if(null !=message.getBcc()){ helper.setBcc(EmailConvert.convert(message.getBcc())); } if(null == message.getFrom()){ MailAddress from = new MailAddress(username, username_dispaly); message.setFrom(from); } helper.setFrom(EmailConvert.convert(message.getFrom())); if(null == message.getReplyTo()){ MailAddress replyTo = new MailAddress(username, username_dispaly); message.setReplyTo(replyTo); } helper.setReplyTo(EmailConvert.convert(message.getReplyTo())); if(StringUtils.isEmpty(message.getSubject())){ message.setSubject(subject); } helper.setSubject(message.getSubject()); helper.setText(message.getHtmlText(), html); if (!ArrayUtils.isEmpty(attachment)) { for (int i = 0; attachment != null && i < attachment.length; i++) { File file = attachment[i]; FileSystemResource resource = new FileSystemResource(file); helper.addAttachment(file.getName(), resource); } } Transport ts = createTransport(session); ts.sendMessage(mm, mm.getAllRecipients()); } catch (Exception e) { LOG.warn(message.toString()); LOG.error(e.toString()); } } /** * <br>描 述: 读取模块文件 转换为 String * <br>作 者: shizhenwei * <br>历 史: (版本) 作者 时间 注释 * @param url * @return */ public static String readEmailTemplate(String url){ try { String classpath = MailUtils.class.getResource("/").getPath(); FileInputStream in = new FileInputStream(classpath+"/"+url); int size=in.available(); byte[] buffer=new byte[size]; in.read(buffer); in.close(); return new String(buffer,"UTF-8"); } catch (IOException e) { e.printStackTrace(); } return null; } }
4.配置文件
#mail mail.smtp.host=smtp.163.com mail.sender.username=xxx@163.com mail.sender.password=xxx mail.smtp.timeout=25000 mail.sender.username_dispaly=中国人民 mail.sender.subject=债务履行核实函
文件名称:mail_template_zwlxhsh.st
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Untitled Document</title> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <head/> <body> <!-- 注释 mail_debt_name 欠款人 mail_lender_name 出借人 mail_debt_date 合同签订时间 mail_notarization_cod 公证申请编号 mail_contract_amont 合同金额 mail_contract_deadline 合同期限 mail_dept_principal 欠款本金 mail_dept_endate 借款截止时间 mail_dept_interest_date 欠款利息日 mail_late_charge 利率 mail_send_date 邮件发送日期 mail_statement_date 各个贷款时间 --> <div style="margin-left:80px;"> 债务履行核实函<br /> <br /> mail_debt_name:<br /> <p> mail_debt_date,你方与出借人mail_lender_name签订《借款合同》,并在我处对该合同办理了具有强制执行效力的债权文书公证【公证书编号:mail_notarization_cod】。合同约定:借款人mail_debt_name向出借人mail_lender_name申请借款人民币mail_contract_amont元,借款期限mail_contract_deadline。 </p> <p> 现出借人向本处提出申请称:其于mail_statement_date以银行转帐方式给付mail_debt_name借款共计mail_contract_amont元,现借款期限已到期,但是mail_debt_name未能依约还款,尚欠本金mail_dept_principal元,利息给付至mail_dept_endate。故依合同约定向公证处申请出具《执行证书》,请求确认执行标的:一、借款本金mail_dept_principal元;二、借款利息,自mail_dept_interest_date起至还款之日止,按月利率mail_late_charge%计算。 </P> <p> 现与你方核实上述借款的还款情况,请收到此函后三个工作日内以书面形式回复、或者亲自到我处向承办公证员如实陈述履行合同的情况,并提供相关证据。若到期没有回复或未能提供证据,则视为你方对上述债权主张无异议,我处将按上述合同约定及出借人主张的债权金额,出具执行证书。之后,出借人可以不经诉讼,直接向有管辖权的人民法院对你方申请强制执行。 </p> <br /> <div> <div style="float:right;width:400px"> <div style="text-align:-webkit-center">北京市中信公证处</div> <div style="text-align:-webkit-center">mail_send_date</div> </div> </div> <br /> <div> 联系人:xxx公证员  法务:xxx<br /> 电 话:010-52601155-627或641<br /> 地 址:北京市西城区金融街广宁伯路2号铁通大厦五层<br /><br /> </div> </div> </body>