系统对接邮件系统(可个性化配置邮件模板)

1.引入依赖

<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文件中初始化VelocityEngineFactoryBean

<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文件

<!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来生成邮件发送的内容

//result为最后生成的邮件内容,也就是根据数据和模板生成的内容
//this.getVelocityEngine()为spring配置文件中配置的模板加载器
//templateName为模板的地址,为一个String
//"UTF-8"为编码格式
//model为需要像模板中体检的数据,为一个Map集合
String result = VelocityEngineUtils.mergeTemplateIntoString(this.getVelocityEngine(), templateName, "UTF-8", model);

5.引入依赖

<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方法

//该方法的本质为给各种邮件发送的参数赋值
 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;
    }

转载于:https://my.oschina.net/u/3045515/blog/1637824

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值