Search in sources :

Example 1 with Mailbox

use of org.apache.james.mime4j.dom.address.Mailbox in project gerrit by GerritCodeReview.

the class RawMailParser method parse.

/**
   * Parses a MailMessage from a string.
   *
   * @param raw String as received over the wire
   * @return Parsed MailMessage
   * @throws MailParsingException
   */
public static MailMessage parse(String raw) throws MailParsingException {
    MailMessage.Builder messageBuilder = MailMessage.builder();
    messageBuilder.rawContentUTF(raw);
    Message mimeMessage;
    try {
        MessageBuilder builder = new DefaultMessageBuilder();
        mimeMessage = builder.parseMessage(new ByteArrayInputStream(raw.getBytes(UTF_8)));
    } catch (IOException | MimeException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    // Add general headers
    if (mimeMessage.getMessageId() != null) {
        messageBuilder.id(mimeMessage.getMessageId());
    }
    if (mimeMessage.getSubject() != null) {
        messageBuilder.subject(mimeMessage.getSubject());
    }
    messageBuilder.dateReceived(new DateTime(mimeMessage.getDate()));
    // Add From, To and Cc
    if (mimeMessage.getFrom() != null && mimeMessage.getFrom().size() > 0) {
        Mailbox from = mimeMessage.getFrom().get(0);
        messageBuilder.from(new Address(from.getName(), from.getAddress()));
    }
    if (mimeMessage.getTo() != null) {
        for (Mailbox m : mimeMessage.getTo().flatten()) {
            messageBuilder.addTo(new Address(m.getName(), m.getAddress()));
        }
    }
    if (mimeMessage.getCc() != null) {
        for (Mailbox m : mimeMessage.getCc().flatten()) {
            messageBuilder.addCc(new Address(m.getName(), m.getAddress()));
        }
    }
    // Add additional headers
    mimeMessage.getHeader().getFields().stream().filter(f -> !MAIN_HEADERS.contains(f.getName().toLowerCase())).forEach(f -> messageBuilder.addAdditionalHeader(f.getName() + ": " + f.getBody()));
    // Add text and html body parts
    StringBuilder textBuilder = new StringBuilder();
    StringBuilder htmlBuilder = new StringBuilder();
    try {
        handleMimePart(mimeMessage, textBuilder, htmlBuilder);
    } catch (IOException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    messageBuilder.textContent(Strings.emptyToNull(textBuilder.toString()));
    messageBuilder.htmlContent(Strings.emptyToNull(htmlBuilder.toString()));
    try {
        // required attributes are missing, so that the caller doesn't fall over.
        return messageBuilder.build();
    } catch (IllegalStateException e) {
        throw new MailParsingException("Missing required attributes after email was parsed", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Message(org.apache.james.mime4j.dom.Message) MimeException(org.apache.james.mime4j.MimeException) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) TextBody(org.apache.james.mime4j.dom.TextBody) Ints(com.google.common.primitives.Ints) InputStreamReader(java.io.InputStreamReader) Multipart(org.apache.james.mime4j.dom.Multipart) Strings(com.google.common.base.Strings) Address(com.google.gerrit.server.mail.Address) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) ImmutableList(com.google.common.collect.ImmutableList) ByteArrayInputStream(java.io.ByteArrayInputStream) CharStreams(com.google.common.io.CharStreams) Entity(org.apache.james.mime4j.dom.Entity) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) Message(org.apache.james.mime4j.dom.Message) Address(com.google.gerrit.server.mail.Address) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.apache.james.mime4j.MimeException)

Example 2 with Mailbox

use of org.apache.james.mime4j.dom.address.Mailbox in project tika by apache.

the class MailContentHandler method getDisplayString.

private String getDisplayString(Address address) {
    if (address instanceof Mailbox) {
        Mailbox mailbox = (Mailbox) address;
        String name = mailbox.getName();
        if (name != null && name.length() > 0) {
            name = DecoderUtil.decodeEncodedWords(name, DecodeMonitor.SILENT);
            return name + " <" + mailbox.getAddress() + ">";
        } else {
            return mailbox.getAddress();
        }
    } else {
        return address.toString();
    }
}
Also used : Mailbox(org.apache.james.mime4j.dom.address.Mailbox)

Example 3 with Mailbox

use of org.apache.james.mime4j.dom.address.Mailbox in project k-9 by k9mail.

the class Address method parse.

/**
     * Parse a comma separated list of addresses in RFC-822 format and return an
     * array of Address objects.
     *
     * @param addressList
     * @return An array of 0 or more Addresses.
     */
public static Address[] parse(String addressList) {
    if (TextUtils.isEmpty(addressList)) {
        return EMPTY_ADDRESS_ARRAY;
    }
    List<Address> addresses = new ArrayList<Address>();
    try {
        MailboxList parsedList = AddressBuilder.DEFAULT.parseAddressList(addressList).flatten();
        for (int i = 0, count = parsedList.size(); i < count; i++) {
            org.apache.james.mime4j.dom.address.Address address = parsedList.get(i);
            if (address instanceof Mailbox) {
                Mailbox mailbox = (Mailbox) address;
                addresses.add(new Address(mailbox.getLocalPart() + "@" + mailbox.getDomain(), mailbox.getName(), false));
            } else {
                Log.e(LOG_TAG, "Unknown address type from Mime4J: " + address.getClass().toString());
            }
        }
    } catch (MimeException pe) {
        Log.e(LOG_TAG, "MimeException in Address.parse()", pe);
        //but we do an silent failover : we just use the given string as name with empty address
        addresses.add(new Address(null, addressList, false));
    }
    return addresses.toArray(EMPTY_ADDRESS_ARRAY);
}
Also used : Mailbox(org.apache.james.mime4j.dom.address.Mailbox) ArrayList(java.util.ArrayList) MimeException(org.apache.james.mime4j.MimeException) MailboxList(org.apache.james.mime4j.dom.address.MailboxList)

Aggregations

Mailbox (org.apache.james.mime4j.dom.address.Mailbox)3 MimeException (org.apache.james.mime4j.MimeException)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CharStreams (com.google.common.io.CharStreams)1 Ints (com.google.common.primitives.Ints)1 Address (com.google.gerrit.server.mail.Address)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 ArrayList (java.util.ArrayList)1 Entity (org.apache.james.mime4j.dom.Entity)1 Message (org.apache.james.mime4j.dom.Message)1 MessageBuilder (org.apache.james.mime4j.dom.MessageBuilder)1 Multipart (org.apache.james.mime4j.dom.Multipart)1 TextBody (org.apache.james.mime4j.dom.TextBody)1 MailboxList (org.apache.james.mime4j.dom.address.MailboxList)1 DefaultMessageBuilder (org.apache.james.mime4j.message.DefaultMessageBuilder)1