Search in sources :

Example 1 with InternetMessageHeader

use of microsoft.exchange.webservices.data.property.complex.InternetMessageHeader 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);
}
Also used : EmailMessage(microsoft.exchange.webservices.data.core.service.item.EmailMessage) ItemAttachment(microsoft.exchange.webservices.data.property.complex.ItemAttachment) Attachment(microsoft.exchange.webservices.data.property.complex.Attachment) ItemAttachment(microsoft.exchange.webservices.data.property.complex.ItemAttachment) AttachmentCollection(microsoft.exchange.webservices.data.property.complex.AttachmentCollection) EmailAddress(microsoft.exchange.webservices.data.property.complex.EmailAddress) ServiceLocalException(microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) Date(java.util.Date) InternetMessageHeaderCollection(microsoft.exchange.webservices.data.property.complex.InternetMessageHeaderCollection) InternetMessageHeader(microsoft.exchange.webservices.data.property.complex.InternetMessageHeader) Item(microsoft.exchange.webservices.data.core.service.item.Item) EmailAddressCollection(microsoft.exchange.webservices.data.property.complex.EmailAddressCollection) Iterator(java.util.Iterator) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 ServiceLocalException (microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException)1 EmailMessage (microsoft.exchange.webservices.data.core.service.item.EmailMessage)1 Item (microsoft.exchange.webservices.data.core.service.item.Item)1 Attachment (microsoft.exchange.webservices.data.property.complex.Attachment)1 AttachmentCollection (microsoft.exchange.webservices.data.property.complex.AttachmentCollection)1 EmailAddress (microsoft.exchange.webservices.data.property.complex.EmailAddress)1 EmailAddressCollection (microsoft.exchange.webservices.data.property.complex.EmailAddressCollection)1 InternetMessageHeader (microsoft.exchange.webservices.data.property.complex.InternetMessageHeader)1 InternetMessageHeaderCollection (microsoft.exchange.webservices.data.property.complex.InternetMessageHeaderCollection)1 ItemAttachment (microsoft.exchange.webservices.data.property.complex.ItemAttachment)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 ListenerException (nl.nn.adapterframework.core.ListenerException)1 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)1