Search in sources :

Example 6 with EmailMessage

use of microsoft.exchange.webservices.data.core.service.item.EmailMessage in project iaf by ibissource.

the class ExchangeMailListener method getStringFromRawMessage.

public String getStringFromRawMessage(Object rawMessage, Map threadContext) throws ListenerException {
    Item item = (Item) rawMessage;
    try {
        XmlBuilder emailXml = new XmlBuilder("email");
        EmailMessage emailMessage;
        PropertySet ps;
        if (isSimple()) {
            ps = new PropertySet(EmailMessageSchema.Subject);
            emailMessage = EmailMessage.bind(exchangeService, item.getId(), ps);
            addEmailInfoSimple(emailMessage, emailXml);
        } else {
            ps = new PropertySet(EmailMessageSchema.DateTimeReceived, EmailMessageSchema.From, EmailMessageSchema.Subject, EmailMessageSchema.Body, EmailMessageSchema.DateTimeSent);
            emailMessage = EmailMessage.bind(exchangeService, item.getId(), ps);
            addEmailInfo(emailMessage, emailXml);
        }
        if (StringUtils.isNotEmpty(getStoreEmailAsStreamInSessionKey())) {
            emailMessage.load(new PropertySet(ItemSchema.MimeContent));
            MimeContent mc = emailMessage.getMimeContent();
            ByteArrayInputStream bis = new ByteArrayInputStream(mc.getContent());
            threadContext.put(getStoreEmailAsStreamInSessionKey(), bis);
        }
        return emailXml.toXML();
    } catch (Exception e) {
        throw new ListenerException(e);
    }
}
Also used : Item(microsoft.exchange.webservices.data.core.service.item.Item) EmailMessage(microsoft.exchange.webservices.data.core.service.item.EmailMessage) ListenerException(nl.nn.adapterframework.core.ListenerException) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeContent(microsoft.exchange.webservices.data.property.complex.MimeContent) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) PropertySet(microsoft.exchange.webservices.data.core.PropertySet) ServiceLocalException(microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Example 7 with EmailMessage

use of microsoft.exchange.webservices.data.core.service.item.EmailMessage 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

EmailMessage (microsoft.exchange.webservices.data.core.service.item.EmailMessage)7 Message (javax.mail.Message)4 MimeMessage (javax.mail.internet.MimeMessage)4 Item (microsoft.exchange.webservices.data.core.service.item.Item)4 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 MessagingException (javax.mail.MessagingException)2 ExchangeService (microsoft.exchange.webservices.data.core.ExchangeService)2 PropertySet (microsoft.exchange.webservices.data.core.PropertySet)2 ServiceLocalException (microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException)2 Folder (microsoft.exchange.webservices.data.core.service.folder.Folder)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 ListenerException (nl.nn.adapterframework.core.ListenerException)2 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1