Jbpm4.4 邮件模版的相关配置

Step1:JBPM的邮件发送功能,需要我们自己实现用户认证的类,如下:

public class EmailAuthenticator extends Authenticator{
	
	public EmailAuthenticator() {
		System.out.println("custom email authenticator is loading...");
	}
	
	private String userName;
	private String password;
	
	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(userName, password);
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}	
}


Step2:在classpath下创建用以替换jbpm.default.cfg.xml的文件,我这个地方叫jbpm.customer.cfg.xml

可先直接将jbpm.default.cfg.xml中的内容,复制到jbpm.customer.cfg.xml中,然后修改mail-session部分如下:

    <mail-session>
      <mail-server>
        <session-properties resource="jbpm.mail.properties" /><span style="white-space:pre">	</span>      <!-- 此文件也需要创建在classpath下 --><span style="white-space:pre">		</span>
        <authenticator class="com.andy.jbpm.mail.EmailAuthenticator"> <!-- 此处要用Step1中创建好的认证类 -->
         	<field name="userName"><string value="xxxx"/></field> 
         	<field name="password"><string value="xxxx"/></field>
        </authenticator>
      </mail-server>
    </mail-session>


Ste3:创建jbpm.mail.properties文件如下:

mail.smtp.auth=true
mail.smtp.host=smtp.189.cn
mail.smtp.port=25
[email protected]

Step4:修改jbpm.cfg.xml文件内容如下(用Step2中创建的配置文件替换jbpm.default.cfg.xml):

  <!--<import resource="jbpm.default.cfg.xml" />-->
  <import resource="jbpm.customer.cfg.xml" />
  <import resource="jbpm.businesscalendar.cfg.xml" />
  <import resource="jbpm.tx.hibernate.cfg.xml" />
  <import resource="jbpm.jpdl.cfg.xml" />
  <import resource="jbpm.bpmn.cfg.xml" />
  <import resource="jbpm.identity.cfg.xml" />

Ste5:依然在classpath下创建发送email的模版文件jbpm.mail.templates.xml如下(此处创建了两个模版):

<?xml version="1.0" encoding="UTF-8"?>
<jbpm-configuration>

	<process-engine-context>
		<mail-template name='task-notification'> <!-- 如果task节点中添加了notification,那么当流程执行到该task的时候,会自动调用该模版来发送邮件--><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">		</span>    <span style="white-space:pre">	</span>			<to users="${task.assignee}" /></span>
<subject>${task.name}</subject><text><![CDATA[Hi ${task.assignee},Task "${task.name}" has been assigned to you.${task.description}Sent by jBPM]]></text></mail-template><mail-template name='enotify-template'> <!-- 自定义的邮件模版,email节点的template名字需要和该节点的name一致,这样email节点就能用该模版来发送邮件了--><to users="${mailTo}" /><subject>[test]做个好汉子,热血热肠热</subject><text><![CDATA[Hi 好汉子,Task "热肠热" has been assigned to you.good JobSent by jBPM]]></text></mail-template></process-engine-context></jbpm-configuration>

 
 
Step7:创建测试用户

		IdentityService identityService = processEngine.getIdentityService();
//		identityService.createUser("andy", "andy", "xu", "[email protected]");
		identityService.createUser("sandy", "sandy", "lau", "[email protected]");


Step8:测试-->先测试task节点的notification功能,定义测试流程如下:

<process name="email" xmlns="http://jbpm.org/4.4/jpdl">
   <start g="172,38,48,48" name="start">
      <transition g="-89,-22" name="to email_notify" to="email_notify"/>
   </start>   
   <task g="159,137,92,52" assignee="andy" name="email_notify" >
      <notification/>  
      <transition g="-50,-22" name="to end" to="end"/>    		
   </task>
   <end g="179,243,48,48" name="end"/>  
</process>
-当开启这个测试流程的时候, 用户andy就能在自己的邮箱[email protected]中收到邮件了.. 

Step9:测试-->测试email节点的邮件接收功能,定义测试流程如下:

<process name="mailnotify" xmlns="http://jbpm.org/4.4/jpdl">
   <start g="184,36,48,48" name="start">
      <transition name="to task1" to="task1" g="-52,-22"/>
   </start>
   <end g="182,204,48,48" name="end"/>
   <mail g="161,113,92,52" name="task1" template="enotify-template"> <!-- 此处template的值需要和Ste5中,mail-template的name属性一致 -->
      <transition name="to end" to="end" g="-43,-22"/>   		
   </mail> 
</process>

开启流程:

ExecutionService es = processEngine.getExecutionService();		
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("mailTo", "andy,<span style="font-family: Arial, Helvetica, sans-serif;">sandy</span>");		
		ProcessInstance processInstance = es.startProcessInstanceByKey("mailnotify", vars);
-这样,[email protected]以及[email protected]就都能收模版为enotify-template的邮件了..

你可能感兴趣的:(邮件,jbpm)