}
@Override
public synchronized void send(Dispatch dispatch) throws MailException {
Message message = dispatch.getMessage();
String recipient = dispatch.getParams().get(MailChannel.RECIPIENT);
String recipientName = dispatch.getParams().get(MailChannel.RECIPIENT_NAME);
String subject = message.getSubject();
try {
MimeMessage mm = new MimeMessage(getSmtpSession(recipient)) {
@Override
protected void updateMessageID() throws MessagingException {
String domain = _from.getAddress();
int idx = _from.getAddress().indexOf('@');
if (idx >= 0) {
domain = domain.substring(idx + 1);
}
setHeader("Message-ID", "<" + UUID.randomUUID() + "@" + domain + ">");
}
};
mm.setFrom(_from);
mm.setSender(_from);
InternetAddress replyTo = getReplyTo();
if (replyTo != null) {
mm.setReplyTo(new Address[] { replyTo });
}
mm.setHeader("X-Mailer", "molindo-notify");
mm.setSentDate(new Date());
mm.setRecipient(RecipientType.TO,
new InternetAddress(recipient, recipientName, CharsetUtils.UTF_8.displayName()));
mm.setSubject(subject, CharsetUtils.UTF_8.displayName());
if (_format == Format.HTML) {
if (message.getType() == Type.TEXT) {
throw new MailException("can't send HTML mail from TEXT message", false);
}
mm.setText(message.getHtml(), CharsetUtils.UTF_8.displayName(), "html");
} else if (_format == Format.TEXT || _format == Format.MULTI && message.getType() == Type.TEXT) {
mm.setText(message.getText(), CharsetUtils.UTF_8.displayName());
} else if (_format == Format.MULTI) {
MimeBodyPart html = new MimeBodyPart();
html.setText(message.getHtml(), CharsetUtils.UTF_8.displayName(), "html");
MimeBodyPart text = new MimeBodyPart();
text.setText(message.getText(), CharsetUtils.UTF_8.displayName());
/*
* The formats are ordered by how faithful they are to the
* original, with the least faithful first and the most faithful
* last. (http://en.wikipedia.org/wiki/MIME#Alternative)
*/
MimeMultipart mmp = new MimeMultipart();
mmp.setSubType("alternative");
mmp.addBodyPart(text);
mmp.addBodyPart(html);
mm.setContent(mmp);
} else {
throw new NotifyRuntimeException("unexpected format (" + _format + ") or type (" + message.getType()
+ ")");
}
send(mm);