jbpm4.4中使用mail组件发邮件时要求smtp 验证用户,jbpm那个类没有实现权限验证的连接。
红色代码经过修改,实现了smtp验证服务器发邮件。测试成功!
package org.jbpm.pvm.internal.email.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
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.impl.AddressFilter;
import org.jbpm.pvm.internal.email.impl.MailServer;
import org.jbpm.pvm.internal.email.spi.MailSession;
public class MailSessionImpl implements MailSession {
private List<MailServer> mailServers;
public void send(Collection<Message> emails) {
//**********************************
ClassLoader loader = this.getClass().getClassLoader();
InputStream in = loader.getResourceAsStream("jbpm.mail.properties");
Properties p = new Properties();
try {
p.load(in);
} catch (IOException e) {
e.printStackTrace();
}
String username = p.getProperty("mail.username");
String host = p.getProperty("mail.smtp.host");
String password = p.getProperty("mail.password");
//**********************************
// 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(host, username, password);
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;
}
}