jbpm 4 smtp検証メール送信

3193 ワード

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;
	}

}


注意してconnect(「smtp.sina.com」,「メールアドレス」,「パスワード」)で使用するアカウントとjbpm.mail.properties

mail.smtp.host=smtp.sina.com.cn
[email protected]
mail.smtp.auth=true