jbpm4 发送smtp验证的邮件

jbpm4.4中使用mail组件发邮件时如果邮件服务器要求smtp 验证用户 经过反复测试没能成功,通过观察javamail发送到需要smtp验证的服务器的代码,对比jbpm中的实现类,发现jbpm好像那个类没有实现权限验证的连接。
下面代码是经过修改后的,实现了smtp验证服务器发邮件。测试成功!

package org.jbpm.pvm.internal.email.impl;

import java.util.Collection;
import java.util.List;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;

import org.jbpm.api.JbpmException;
import org.jbpm.pvm.internal.email.spi.MailSession;

public class MailSessionImpl implements MailSession {

private List<MailServer> mailServers;

public void send(Collection<Message> emails) {
// Emails need to have the sessions populated.
for (Message email : emails) {
try {
Address[] to = email.getRecipients(RecipientType.TO);
Address[] cc = email.getRecipients(RecipientType.CC);
Address[] bcc = email.getRecipients(RecipientType.BCC);

for (MailServer mailServer : mailServers) {
// Need to apply filter.
AddressFilter addressFilter = mailServer.getAddressFilter();
if (addressFilter != null) {
// Set the email with the new filtered addresses.
email.setRecipients(RecipientType.TO, addressFilter.filter(to));
email.setRecipients(RecipientType.CC, addressFilter.filter(cc));
email.setRecipients(RecipientType.BCC, addressFilter.filter(bcc));
}

// if sender is not present, use local address
Session mailSession = mailServer.getMailSession();
if (email.getFrom() == null) {
email.setFrom(InternetAddress.getLocalAddress(mailSession));
}

// If there is someone to send it to, then send it.
Address[] recipients = email.getAllRecipients();
if (recipients.length > 0) {
Transport transport = mailSession.getTransport(recipients[0]);
try {
// transport.connect(); //原来smtp的连接服务器代码
transport.connect("smtp.sina.com", "邮箱账号", "密码");
transport.sendMessage(email, recipients);
System.out.println("MailSessionImpl.send() ok!");
} finally {
transport.close();
}
}
}
} catch (MessagingException e) {
throw new JbpmException("could not send email: " + email, e);
}
}
}

public List<MailServer> getMailServers() {
return mailServers;
}

protected void setMailServers(List<MailServer> mailServers) {
this.mailServers = mailServers;
}

}




注意 transport.connect("smtp.sina.com", "邮箱账号", "密码");中使用的账号和jbpm.mail.properties

mail.smtp.host=smtp.sina.com.cn
mail.from=xxxxxx@sina.com
mail.smtp.auth=true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值