*/
private void sendMail(MailOutTransportInfo outInfo, MessageContext msgContext)
throws AxisFault, MessagingException, IOException {
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter = null;
try {
messageFormatter = TransportUtils.getMessageFormatter(msgContext);
} catch (AxisFault axisFault) {
throw new BaseTransportException("Unable to get the message formatter to use");
}
WSMimeMessage message = new WSMimeMessage(session);
Map trpHeaders = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
// set From address - first check if this is a reply, then use from address from the
// transport out, else if any custom transport headers set on this message, or default
// to the transport senders default From address
if (outInfo.getTargetAddresses() != null && outInfo.getFromAddress() != null) {
message.setFrom(outInfo.getFromAddress());
message.setReplyTo((new Address []{outInfo.getFromAddress()}));
} else if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_FROM)) {
message.setFrom(
new InternetAddress((String) trpHeaders.get(MailConstants.MAIL_HEADER_FROM)));
message.setReplyTo(InternetAddress.parse(
(String) trpHeaders.get(MailConstants.MAIL_HEADER_FROM)));
} else {
if (smtpFromAddress != null) {
message.setFrom(smtpFromAddress);
message.setReplyTo(new Address[] {smtpFromAddress});
} else {
handleException("From address for outgoing message cannot be determined");
}
}
// set To address/es to any custom transport header set on the message, else use the reply
// address from the out transport information
if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_TO)) {
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse((String) trpHeaders.get(MailConstants.MAIL_HEADER_TO)));
} else if (outInfo.getTargetAddresses() != null) {
message.setRecipients(Message.RecipientType.TO, outInfo.getTargetAddresses());
} else {
handleException("To address for outgoing message cannot be determined");
}
// set Cc address/es to any custom transport header set on the message, else use the
// Cc list from original request message
if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_CC)) {
message.setRecipients(Message.RecipientType.CC,
InternetAddress.parse((String) trpHeaders.get(MailConstants.MAIL_HEADER_CC)));
} else if (outInfo.getTargetAddresses() != null) {
message.setRecipients(Message.RecipientType.CC, outInfo.getCcAddresses());
}
// set Bcc address/es to any custom addresses set at the transport sender level + any
// custom transport header
InternetAddress[] trpBccArr = null;
if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_BCC)) {
trpBccArr = InternetAddress.parse((String) trpHeaders.get(MailConstants.MAIL_HEADER_BCC));
}
InternetAddress[] mergedBcc = new InternetAddress[
(trpBccArr != null ? trpBccArr.length : 0) +
(smtpBccAddresses != null ? smtpBccAddresses.length : 0)];
if (trpBccArr != null) {
System.arraycopy(trpBccArr, 0, mergedBcc, 0, trpBccArr.length);
}
if (smtpBccAddresses != null) {
System.arraycopy(smtpBccAddresses, 0, mergedBcc, mergedBcc.length, smtpBccAddresses.length);
}
if (mergedBcc != null) {
message.setRecipients(Message.RecipientType.BCC, mergedBcc);
}
// set subject
if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_SUBJECT)) {
message.setSubject((String) trpHeaders.get(MailConstants.MAIL_HEADER_SUBJECT));
} else if (outInfo.getSubject() != null) {
message.setSubject(outInfo.getSubject());
} else {
message.setSubject(BaseConstants.SOAPACTION + ": " + msgContext.getSoapAction());
}
// if a custom message id is set, use it
if (msgContext.getMessageID() != null) {
message.setHeader(MailConstants.MAIL_HEADER_MESSAGE_ID, msgContext.getMessageID());
message.setHeader(MailConstants.MAIL_HEADER_X_MESSAGE_ID, msgContext.getMessageID());
}
// if this is a reply, set reference to original message
if (outInfo.getRequestMessageID() != null) {
message.setHeader(MailConstants.MAIL_HEADER_IN_REPLY_TO, outInfo.getRequestMessageID());
message.setHeader(MailConstants.MAIL_HEADER_REFERENCES, outInfo.getRequestMessageID());
} else {
if (trpHeaders != null &&
trpHeaders.containsKey(MailConstants.MAIL_HEADER_IN_REPLY_TO)) {
message.setHeader(MailConstants.MAIL_HEADER_IN_REPLY_TO,
(String) trpHeaders.get(MailConstants.MAIL_HEADER_IN_REPLY_TO));
}
if (trpHeaders != null && trpHeaders.containsKey(MailConstants.MAIL_HEADER_REFERENCES)) {
message.setHeader(MailConstants.MAIL_HEADER_REFERENCES,
(String) trpHeaders.get(MailConstants.MAIL_HEADER_REFERENCES));
}
}
// set Date
message.setSentDate(new Date());
// set SOAPAction header
message.setHeader(BaseConstants.SOAPACTION, msgContext.getSoapAction());
// write body
ByteArrayOutputStream baos = null;
String contentType = messageFormatter.getContentType(
msgContext, format, msgContext.getSoapAction());
DataHandler dataHandler = null;
MimeMultipart mimeMultiPart = null;
OMElement firstChild = msgContext.getEnvelope().getBody().getFirstElement();
if (firstChild != null) {
if (BaseConstants.DEFAULT_BINARY_WRAPPER.equals(firstChild.getQName())) {
baos = new ByteArrayOutputStream();
OMNode omNode = firstChild.getFirstOMChild();
if (omNode != null && omNode instanceof OMText) {
Object dh = ((OMText) omNode).getDataHandler();
if (dh != null && dh instanceof DataHandler) {
dataHandler = (DataHandler) dh;
}
}
} else if (BaseConstants.DEFAULT_TEXT_WRAPPER.equals(firstChild.getQName())) {
if (firstChild instanceof OMSourcedElementImpl) {
baos = new ByteArrayOutputStream();
try {
firstChild.serializeAndConsume(baos);
} catch (XMLStreamException e) {
handleException("Error serializing 'text' payload from OMSourcedElement", e);
}
dataHandler = new DataHandler(new String(
baos.toByteArray(), format.getCharSetEncoding()), MailConstants.TEXT_PLAIN);
} else {
dataHandler = new DataHandler(firstChild.getText(), MailConstants.TEXT_PLAIN);
}
} else {
baos = new ByteArrayOutputStream();
messageFormatter.writeTo(msgContext, format, baos, true);
// create the data handler
dataHandler = new DataHandler(
new String(baos.toByteArray(), format.getCharSetEncoding()), contentType);