/** {@inheritDoc} */
@Override
public void sendMessage(Message message, Address[] addresses)
throws MessagingException {
MailService service = MailServiceFactory.getMailService();
MailService.Message msg = new MailService.Message();
String sender = null;
if (message instanceof MimeMessage) {
Address senderAddr = ((MimeMessage) message).getSender();
if (senderAddr != null) {
sender = senderAddr.toString();
}
}
if (sender == null && message.getFrom() != null
&& message.getFrom().length > 0) {
sender = message.getFrom()[0].toString();
}
msg.setSender(sender);
try {
msg.setReplyTo(Joiner.on(", ").useForNull("null").join(message.getReplyTo()));
} catch (NullPointerException e) {
}
boolean toAdmins = false;
Address[] allRecipients = message.getAllRecipients();
if (allRecipients != null) {
for (Address addr : allRecipients) {
if (ADMINS_ADDRESS.equals(addr.toString())) {
toAdmins = true;
}
}
}
if (!toAdmins) {
Set<String> allAddresses = new HashSet<String>();
for (Address addr : addresses) {
allAddresses.add(addr.toString());
}
msg.setTo(convertAddressFields(message.getRecipients(RecipientType.TO), allAddresses));
msg.setCc(convertAddressFields(message.getRecipients(RecipientType.CC), allAddresses));
msg.setBcc(convertAddressFields(message.getRecipients(RecipientType.BCC), allAddresses));
}
msg.setSubject(message.getSubject());
Object textObject = null;
Object htmlObject = null;
String textType = null;
String htmlType = null;
Multipart otherMessageParts = null;
List<MailService.Header> headers = new ArrayList<MailService.Header>();
Enumeration originalHeaders = message.getMatchingHeaders(HEADERS_WHITELIST);
while (originalHeaders.hasMoreElements()) {
Header header = (Header) originalHeaders.nextElement();
headers.add(new MailService.Header(header.getName(), header.getValue()));
}
msg.setHeaders(headers);
if (message.getContentType() == null) {
try {
textObject = message.getContent();
textType = message.getContentType();
} catch (IOException e) {
throw new MessagingException("Getting typeless content failed", e);
}
} else if (message.isMimeType("text/html")) {
try {
htmlObject = message.getContent();
htmlType = message.getContentType();
} catch (IOException e) {
throw new MessagingException("Getting html content failed", e);
}
} else if (message.isMimeType("text/*")) {
try {
textObject = message.getContent();
textType = message.getContentType();
} catch (IOException e) {
throw new MessagingException("Getting text/* content failed", e);
}
} else if (message.isMimeType("multipart/*")) {
Multipart mp;
try {
mp = (Multipart) message.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain") && textObject == null) {
textObject = bp.getContent();
textType = bp.getContentType();
} else if (bp.isMimeType("text/html") && htmlObject == null) {
htmlObject = bp.getContent();
htmlType = bp.getContentType();
} else {
if (otherMessageParts == null) {
String type = mp.getContentType();
assert (type.startsWith("multipart/"));
otherMessageParts = new MimeMultipart(
type.substring("multipart/".length()));
}
otherMessageParts.addBodyPart(bp);
}
}
} catch (IOException e) {
throw new MessagingException("Getting multipart content failed", e);
}
}
if (textObject != null) {
if (textObject instanceof String) {
msg.setTextBody((String) textObject);
} else if (textObject instanceof InputStream) {
try {
msg.setTextBody(inputStreamToString((InputStream) textObject, textType));
} catch (IOException e) {
throw new MessagingException("Stringifying text body failed", e);
}
} else {
throw new MessagingException("Converting text body failed");
}
}
if (htmlObject != null) {
if (htmlObject instanceof String) {
msg.setHtmlBody((String) htmlObject);
} else if (htmlObject instanceof InputStream) {
try {
msg.setHtmlBody(inputStreamToString((InputStream) htmlObject, htmlType));
} catch (IOException e) {
throw new MessagingException("Stringifying html body failed", e);
}
} else {
throw new MessagingException("Converting html body failed");
}
}
if (otherMessageParts != null) {
ArrayList<MailService.Attachment> attachments =
new ArrayList<MailService.Attachment>(otherMessageParts.getCount());
for (int i = 0; i < otherMessageParts.getCount(); i++) {
BodyPart bp = otherMessageParts.getBodyPart(i);
String name = bp.getFileName();
byte[] data;
try {
Object o = bp.getContent();
if (o instanceof InputStream) {
data = inputStreamToBytes((InputStream) o);
} else if (o instanceof String) {
data = ((String) o).getBytes();
} else {
throw new MessagingException("Converting attachment data failed");
}
} catch (IOException e) {
throw new MessagingException("Extracting attachment data failed", e);
}
MailService.Attachment attachment =
new MailService.Attachment(name, data);
attachments.add(attachment);
}
msg.setAttachments(attachments);
}
try {
if (toAdmins) {
service.sendToAdmins(msg);
} else {
service.send(msg);
}
} catch (IOException e) {
notifyTransportListeners(
TransportEvent.MESSAGE_NOT_DELIVERED, new Address[0], addresses,
new Address[0], message);