/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.engine.impl.bpmn.behavior;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
/**
* @author Joram Barrez
* @author Frederik Heremans
*/
public class MailActivityBehavior extends AbstractBpmnActivityBehavior {
private static final long serialVersionUID = 1L;
protected Expression to;
protected Expression from;
protected Expression cc;
protected Expression bcc;
protected Expression subject;
protected Expression text;
protected Expression html;
protected Expression charset;
public void execute(ActivityExecution execution) {
String toStr = getStringFromField(to, execution);
String fromStr = getStringFromField(from, execution);
String ccStr = getStringFromField(cc, execution);
String bccStr = getStringFromField(bcc, execution);
String subjectStr = getStringFromField(subject, execution);
String textStr = getStringFromField(text, execution);
String htmlStr = getStringFromField(html, execution);
String charSetStr = getStringFromField(charset, execution);
Email email = createEmail(textStr, htmlStr);
addTo(email, toStr);
setFrom(email, fromStr);
addCc(email, ccStr);
addBcc(email, bccStr);
setSubject(email, subjectStr);
setMailServerProperties(email);
setCharset(email, charSetStr);
try {
email.send();
} catch (EmailException e) {
throw new ActivitiException("Could not send e-mail", e);
}
leave(execution);
}
protected Email createEmail(String text, String html) {
if (html != null) {
return createHtmlEmail(text, html);
} else if (text != null) {
return createTextOnlyEmail(text);
} else {
throw new ActivitiException("'html' or 'text' is required to be defined when using the mail activity");
}
}
protected HtmlEmail createHtmlEmail(String text, String html) {
HtmlEmail email = new HtmlEmail();
try {
email.setHtmlMsg(html);
if (text != null) { // for email clients that don't support html
email.setTextMsg(text);
}
return email;
} catch (EmailException e) {
throw new ActivitiException("Could not create HTML email", e);
}
}
protected SimpleEmail createTextOnlyEmail(String text) {
SimpleEmail email = new SimpleEmail();
try {
email.setMsg(text);
return email;
} catch (EmailException e) {
throw new ActivitiException("Could not create text-only email", e);
}
}
protected void addTo(Email email, String to) {
String[] tos = splitAndTrim(to);
if (tos != null) {
for (String t : tos) {
try {
email.addTo(t);
} catch (EmailException e) {
throw new ActivitiException("Could not add " + t + " as recipient", e);
}
}
} else {
throw new ActivitiException("No recipient could be found for sending email");
}
}
protected void setFrom(Email email, String from) {
String fromAddres = null;
if (from != null) {
fromAddres = from;
} else { // use default configured from address in process engine config
fromAddres = Context.getProcessEngineConfiguration().getMailServerDefaultFrom();
}
try {
email.setFrom(fromAddres);
} catch (EmailException e) {
throw new ActivitiException("Could not set " + from + " as from address in email", e);
}
}
protected void addCc(Email email, String cc) {
String[] ccs = splitAndTrim(cc);
if (ccs != null) {
for (String c : ccs) {
try {
email.addCc(c);
} catch (EmailException e) {
throw new ActivitiException("Could not add " + c + " as cc recipient", e);
}
}
}
}
protected void addBcc(Email email, String bcc) {
String[] bccs = splitAndTrim(bcc);
if (bccs != null) {
for (String b : bccs) {
try {
email.addBcc(b);
} catch (EmailException e) {
throw new ActivitiException("Could not add " + b + " as bcc recipient", e);
}
}
}
}
protected void setSubject(Email email, String subject) {
email.setSubject(subject != null ? subject : "");
}
protected void setMailServerProperties(Email email) {
ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
String host = processEngineConfiguration.getMailServerHost();
if (host == null) {
throw new ActivitiException("Could not send email: no SMTP host is configured");
}
email.setHostName(host);
int port = processEngineConfiguration.getMailServerPort();
email.setSmtpPort(port);
email.setSSL(true);
email.setTLS(processEngineConfiguration.getMailServerUseTLS());
String user = processEngineConfiguration.getMailServerUsername();
String password = processEngineConfiguration.getMailServerPassword();
if (user != null && password != null) {
email.setAuthentication(user, password);
}
}
protected void setCharset(Email email, String charSetStr) {
if (charset != null) {
email.setCharset(charSetStr);
}
}
protected String[] splitAndTrim(String str) {
if (str != null) {
String[] splittedStrings = str.split(",");
for (int i = 0; i < splittedStrings.length; i++) {
splittedStrings[i] = splittedStrings[i].trim();
}
return splittedStrings;
}
return null;
}
protected String getStringFromField(Expression expression, DelegateExecution execution) {
if(expression != null) {
Object value = expression.getValue(execution);
if(value != null) {
return value.toString();
}
}
return null;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
spring4+springMVC+mybatis+activiti+maven
共728个文件
bpmn:218个
png:174个
java:141个
3星 · 超过75%的资源 需积分: 9 189 下载量 102 浏览量
2014-10-23
11:30:30
上传
评论
收藏 2.25MB ZIP 举报
温馨提示
spring4+springMVC+mybatis+activiti+maven
资源推荐
资源详情
资源评论
收起资源包目录
spring4+springMVC+mybatis+activiti+maven (728个子文件)
effectInsurance.bpmn 36KB
effectInsurance.bpmn 36KB
leave.bpmn 13KB
leave.bpmn 13KB
quitFlowOnParallelWithSignal.bpmn 10KB
quitFlowOnParallelWithSignal.bpmn 10KB
leave-sj.bpmn 9KB
leave-sj.bpmn 9KB
test.bpmn 8KB
test.bpmn 8KB
qjsq.bpmn 8KB
qjsq.bpmn 8KB
multiSubProcess1.bpmn 8KB
multiSubProcess1.bpmn 8KB
payment.bpmn 8KB
payment.bpmn 8KB
claims.bpmn 8KB
claims.bpmn 8KB
shopping.bpmn 7KB
shopping.bpmn 7KB
quitFlowOnParallel.bpmn 7KB
quitFlowOnParallel.bpmn 7KB
apply.bpmn 7KB
apply.bpmn 7KB
ParallelGatewayNested.bpmn 6KB
ParallelGatewayNested.bpmn 6KB
InclusiveGateway.bpmn 6KB
InclusiveGateway.bpmn 6KB
EventGateway.bpmn 6KB
EventGateway.bpmn 6KB
threeTask.bpmn 6KB
threeTask.bpmn 6KB
MyProcess.bpmn 5KB
MyProcess.bpmn 5KB
ParallelGateway.bpmn 5KB
ParallelGateway.bpmn 5KB
InclusiveGatewayDefalutWay.bpmn 5KB
InclusiveGatewayDefalutWay.bpmn 5KB
billing.bpmn 5KB
billing.bpmn 5KB
ExclusiveGateway.bpmn 5KB
ExclusiveGateway.bpmn 5KB
leave.bpmn 5KB
leave.bpmn 5KB
EventSubprocess.bpmn 5KB
EventSubprocess.bpmn 5KB
ErrorEnd.bpmn 5KB
ErrorEnd.bpmn 5KB
document_sign_paper.bpmn 5KB
document_sign_paper.bpmn 5KB
ErrorBoundaryEventAttachToTask.bpmn 5KB
ErrorBoundaryEventAttachToTask.bpmn 5KB
shopping-only-subprocess.bpmn 5KB
shopping-only-subprocess.bpmn 5KB
testSubProcess.bpmn 5KB
testSubProcess.bpmn 5KB
errorInJob.bpmn 5KB
errorInJob.bpmn 5KB
demo.bpmn 5KB
demo.bpmn 5KB
JumpFee.bpmn 5KB
JumpFee.bpmn 5KB
TimeBoundaryEvent.bpmn 4KB
TimeBoundaryEvent.bpmn 4KB
ExecutionIdNotEqualsProcessInstanceId.bpmn 4KB
ExecutionIdNotEqualsProcessInstanceId.bpmn 4KB
AutoAssignee.bpmn 4KB
AutoAssignee.bpmn 4KB
AutoAssignee.bpmn 4KB
AutoAssignee.bpmn 4KB
Gateway.bpmn 4KB
Gateway.bpmn 4KB
DefaultFlow.bpmn 4KB
DefaultFlow.bpmn 4KB
TimeBoundaryIntermediateEvent.bpmn 4KB
TimeBoundaryIntermediateEvent.bpmn 4KB
AutoClaimForReject.bpmn 4KB
AutoClaimForReject.bpmn 4KB
TimeIntermediateCatchEvent.bpmn 3KB
TimeIntermediateCatchEvent.bpmn 3KB
JavaServiceTask.bpmn 3KB
JavaServiceTask.bpmn 3KB
SignalIntermediateThrowEventComplex.bpmn 3KB
SignalIntermediateThrowEventComplex.bpmn 3KB
SubProcess.bpmn 3KB
SubProcess.bpmn 3KB
NoUserSet.bpmn 3KB
NoUserSet.bpmn 3KB
ErrorEventAndRerun.bpmn 3KB
ErrorEventAndRerun.bpmn 3KB
Shell.bpmn 3KB
Shell.bpmn 3KB
DymaticForm.bpmn 3KB
DymaticForm.bpmn 3KB
UserTaskBpmnAssignee.bpmn 3KB
UserTaskBpmnAssignee.bpmn 3KB
shopping-call-activity.bpmn 3KB
shopping-call-activity.bpmn 3KB
timerInMiddle.bpmn 3KB
timerInMiddle.bpmn 3KB
共 728 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
小窝-java
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页