use of microsoft.exchange.webservices.data.property.complex.EmailAddressCollection in project iaf by ibissource.
the class ExchangeMailListener method addEmailInfo.
private void addEmailInfo(EmailMessage emailMessage, XmlBuilder emailXml) throws Exception {
XmlBuilder recipientsXml = new XmlBuilder("recipients");
EmailAddressCollection eacTo = emailMessage.getToRecipients();
if (eacTo != null) {
for (Iterator it = eacTo.iterator(); it.hasNext(); ) {
XmlBuilder recipientXml = new XmlBuilder("recipient");
EmailAddress ea = (EmailAddress) it.next();
recipientXml.addAttribute("type", "to");
recipientXml.setValue(ea.getAddress());
recipientsXml.addSubElement(recipientXml);
}
}
EmailAddressCollection eacCc = emailMessage.getCcRecipients();
if (eacCc != null) {
for (Iterator it = eacCc.iterator(); it.hasNext(); ) {
XmlBuilder recipientXml = new XmlBuilder("recipient");
EmailAddress ea = (EmailAddress) it.next();
recipientXml.addAttribute("type", "cc");
recipientXml.setValue(ea.getAddress());
recipientsXml.addSubElement(recipientXml);
}
}
EmailAddressCollection eacBcc = emailMessage.getBccRecipients();
if (eacBcc != null) {
for (Iterator it = eacBcc.iterator(); it.hasNext(); ) {
XmlBuilder recipientXml = new XmlBuilder("recipient");
EmailAddress ea = (EmailAddress) it.next();
recipientXml.addAttribute("type", "bcc");
recipientXml.setValue(ea.getAddress());
recipientsXml.addSubElement(recipientXml);
}
}
emailXml.addSubElement(recipientsXml);
XmlBuilder fromXml = new XmlBuilder("from");
fromXml.setValue(emailMessage.getFrom().getAddress());
emailXml.addSubElement(fromXml);
XmlBuilder subjectXml = new XmlBuilder("subject");
subjectXml.setCdataValue(emailMessage.getSubject());
emailXml.addSubElement(subjectXml);
XmlBuilder messageXml = new XmlBuilder("message");
messageXml.setCdataValue(MessageBody.getStringFromMessageBody(emailMessage.getBody()));
emailXml.addSubElement(messageXml);
XmlBuilder attachmentsXml = new XmlBuilder("attachments");
AttachmentCollection ac = emailMessage.getAttachments();
if (ac != null) {
for (Iterator it = ac.iterator(); it.hasNext(); ) {
XmlBuilder attachmentXml = new XmlBuilder("attachment");
Attachment att = (Attachment) it.next();
att.load();
attachmentXml.addAttribute("name", att.getName());
if (att instanceof ItemAttachment) {
ItemAttachment ia = (ItemAttachment) att;
Item aItem = ia.getItem();
if (aItem instanceof EmailMessage) {
EmailMessage em;
em = (EmailMessage) aItem;
addEmailInfo(em, attachmentXml);
}
}
attachmentsXml.addSubElement(attachmentXml);
}
}
emailXml.addSubElement(attachmentsXml);
XmlBuilder headersXml = new XmlBuilder("headers");
InternetMessageHeaderCollection imhc = null;
try {
imhc = emailMessage.getInternetMessageHeaders();
} catch (Exception e) {
log.info("error occurred during getting internet message headers: " + e.getMessage());
}
if (imhc != null) {
for (Iterator it = imhc.iterator(); it.hasNext(); ) {
XmlBuilder headerXml = new XmlBuilder("header");
InternetMessageHeader imh = (InternetMessageHeader) it.next();
headerXml.addAttribute("name", imh.getName());
headerXml.setCdataValue(imh.getValue());
headersXml.addSubElement(headerXml);
}
}
emailXml.addSubElement(headersXml);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date dateTimeSend = emailMessage.getDateTimeSent();
XmlBuilder dateTimeSentXml = new XmlBuilder("dateTimeSent");
dateTimeSentXml.setValue(sdf.format(dateTimeSend));
emailXml.addSubElement(dateTimeSentXml);
Date dateTimeReceived = emailMessage.getDateTimeReceived();
XmlBuilder dateTimeReceivedXml = new XmlBuilder("dateTimeReceived");
dateTimeReceivedXml.setValue(sdf.format(dateTimeReceived));
emailXml.addSubElement(dateTimeReceivedXml);
}
Aggregations