1.引入依賴html
<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.5</version> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.5.RELEASE</version> </dependency>
2.在spring的xml文件中初始化VelocityEngineFactoryBeanjava
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="velocityProperties"> <props> <prop key="resource.loader">class</prop> <prop key="class.resource.loader.class"> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader </prop> <prop key="velocimacro.library"></prop> </props> </property> </bean>
3.建立郵件模板,郵件模板爲一個vm文件spring
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>工單系統郵件模板</title> <style> *{ margin:0; padding: 0; line-height: 30px; } table{ width: 100%; border-collapse: collapse; border-spacing: 0; padding: 20px; } th,td{ padding: 10px; border:1px solid #e8e8e8; } .title{ color: orange; font-size:20px; } .blue{ color: blue; } .gray-bgc{ background-color: #ccc; color: #000; } .cent-tab{} .cent-tab td{ text-align: center; } </style> </head> <body> <table> <tr> <th class="title"> 郵件標題:客服工單系統-抄送通知 </th> </tr> <tr> <td> <strong><span class="blue"></span>您好! </strong> </td> </tr> <tr> <td> <div style="text-indent:2em;"> ${userOrg}<span class="blue">${userName}${userEmail}</span>,經過客服工單系統給您發送工單信息,請查看。本郵件爲系統發送,請勿直接回復,謝謝! </div> <div> 登陸網址:<a class="blue" href="http://kfgd.hengchang6.com">http://kfgd.hengchang6.com</a> </div> <div> 登陸帳號:${loginName}(員工登陸名) </div> </td> </tr> <tr> <td> <strong>工單詳情</strong> </td> </tr> <tr> <td class="gray-bgc"> <strong style="font-size:18px;">·</strong>客戶信息 </td> </tr> <tr> <td> <table> <tr> <th>保單ID: </th> <td>$detail.insuranceId</td> <th>投保單號: </th> <td>${detail.insuranceNum}</td> </tr> <tr> <th>投保人: </th> <td>$detail.insureName</td> <th>投保人聯繫方式: </th> <td>${detail.contactMethod}</td> </tr> <tr> <th>被保險人: </th> <td>$detail.customerName</td> <th>受理日期: </th> <td>${detail.acceptDate}</td> </tr> <tr> <th>業務員姓名: </th> <td>${detail.salesmanName}</td> <th>業務員證件號: </th> <td>${detail.customerCid}</td> </tr> <tr> <th>保險公司: </th> <td>${detail.insuranceCompany}</td> <th>保單狀態: </th> <td>${detail.insuranceStatus}</td> </tr> <tr> <th>保險產品名稱: </th> <td>${detail.insuranceProduct}</td> <th>所屬省份公司: </th> <td>${detail.managerArea}</td> </tr> <tr> <th>所屬地分公司: </th> <td>${detail.managerCity}</td> <th></th> <td></td> </tr> </table> </td> </tr> <tr> <td class="gray-bgc"> <strong style="font-size:18px;">·</strong>基本信息 </td> </tr> <tr> <td> <table class="cent-tab"> <tr> <th>工單編號: </th> <td>$detail.workorderNumber</td> <th>緊急程度: </th> <td>${detail.urgencyLevel}</td> </tr> <tr> <th>坐席: </th> <td>${detail.createByEmpFlag}</td> <th>建立時間: </th> <td>${detail.applyTime}</td> </tr> <tr> <th>一級工單: </th> <td>${detail.firstLevel}</td> <th>二級工單: </th> <td>${detail.secondLevel}</td> </tr> <tr> <th>是否敏感: </th> <td>${detail.sensitiveDegree}</td> <th>是否標註: </th> <td>${detail.labelDegree}</td> </tr> <tr> <th width='100'> 問題描述: </th> <td colspan="3" style="text-indent:2em;"> ${detail.workorderRemark} </td> </tr> <tr> <th> 追加描述: </th> <td style="text-indent:2em;"> <table> #foreach($member2 in $appendLogs) <tr> <td>$member2.operatorName</td> <td>$member2.organizationName</td> <td>$member2.createTimeStr</td> <td>$member2.processRemark</td> </tr> #end </table> </td> </tr> </table> </td> </tr> <tr> <td class="gray-bgc"> <strong style="font-size:18px;">·</strong>處理記錄 </td> </tr> <tr> <td> <table class="cent-tab"> <tr> <th>日期</th> <th>處理動做</th> <th>處理人</th> <th>所屬組</th> <th>處理內容</th> </tr> #foreach($member in $processLogs) <tr> <td>$member.createTimeStr</td> <td>$member.processType</td> <td>$member.operatorName</td> <td>$member.organizationName</td> <td>$member.processRemark</td> </tr> #end </table> </td> </tr> </table> </body> </html>
4.根據velocityEngine來生成郵件發送的內容apache
//result爲最後生成的郵件內容,也就是根據數據和模板生成的內容 //this.getVelocityEngine()爲spring配置文件中配置的模板加載器 //templateName爲模板的地址,爲一個String //"UTF-8"爲編碼格式 //model爲須要像模板中體檢的數據,爲一個Map集合 String result = VelocityEngineUtils.mergeTemplateIntoString(this.getVelocityEngine(), templateName, "UTF-8", model);
5.引入依賴api
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.14</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.14</version> </dependency>
6.初始化,給transport的屬性賦值。transport.protocol=smtps 7.發送郵件服務器
// 若是須要身份認證,則建立一個密碼驗證器 //參數爲發件人的用戶名和密碼 MailAuthenticator authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session Session sendMailSession = Session.getDefaultInstance(mailInfo.getProperties(), authenticator); // 根據session建立一個郵件消息 Message mailMessage = new MimeMessage(sendMailSession); // 建立郵件發送者地址 Address from = new InternetAddress(mailInfo.getFromAddress(),mailInfo.getFromNickName(),"UTF-8"); // 設置郵件消息的發送者 mailMessage.setFrom(from); mailMessage.setSentDate(new Date()); // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象 Multipart mainPart = new MimeMultipart(); // // 建立一個包含HTML內容的MimeBodyPart MimeBodyPart html = new MimeBodyPart(); // // 設置HTML內容 html.setContent(result, "text/html; charset=utf-8"); mainPart.addBodyPart(html); //to爲到達地址好比某個接受郵箱 InternetAddress address = new InternetAddress(to); mailMessage.setRecipient(Message.RecipientType.TO, address); if (checkAddress(cc)) { mailMessage.setRecipient(Message.RecipientType.CC, new InternetAddress(cc)); } // 設置郵件消息的主題 mailMessage.setSubject(this.getSubject()); // 設置郵件消息發送的時間 mailMessage.setSentDate(new Date()); // 設置郵件消息的主要內容 mailMessage.setContent(mainPart); logger.debug(cc+"郵件準備完畢,開始發送--[{}]"+to,new Date()); // 發送郵件 Transport.send(mailMessage);
其中的getproperties方法session
//該方法的本質爲給各類郵件發送的參數賦值 public Properties getProperties() throws GeneralSecurityException { Properties props = new Properties(); //郵件發送服務器的地址和端口 props.put("mail.smtp.host", this.mailServerHost); props.put("mail.smtp.port", this.mailServerPort); //郵件發送豬蹄確認 props.put("mail.smtp.auth", validate ? "true" : "false"); //SSL MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); props.put("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.transport.protocol", "smtps"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.connectiontimeout", 1000*60*2); props.put("mail.smtp.timeout", 1000*60*2); return props; }