Example usage for javax.mail.internet InternetAddress parseHeader

List of usage examples for javax.mail.internet InternetAddress parseHeader

Introduction

In this page you can find the example usage for javax.mail.internet InternetAddress parseHeader.

Prototype

public static InternetAddress[] parseHeader(String addresslist, boolean strict) throws AddressException 

Source Link

Document

Parse the given sequence of addresses into InternetAddress objects.

Usage

From source file:foo.domaintest.email.EmailApiModule.java

/** Parse an email from a header that may be of the form {@code First Last <name@example.tld>}. */
public String getAddressHeader(InternetHeaders headers, String headerName) {
    try {/*www.j a  va2 s.  com*/
        String header = getHeader(headers, headerName);
        if (header == null) {
            return null;
        }
        // The "false" argument means use loose validation when parsing addresses out of the header.
        InternetAddress[] addresses = InternetAddress.parseHeader(header, false);
        return addresses == null || addresses.length == 0 ? null : addresses[0].getAddress();
    } catch (AddressException e) {
        return null;
    }
}

From source file:davmail.exchange.ExchangeSession.java

protected List<InternetAddress> getAllRecipients(MimeMessage mimeMessage) throws MessagingException {
    List<InternetAddress> recipientList = new ArrayList<InternetAddress>();
    for (String recipientHeader : RECIPIENT_HEADERS) {
        final String recipientHeaderValue = mimeMessage.getHeader(recipientHeader, ",");
        if (recipientHeaderValue != null) {
            // parse headers in non strict mode
            recipientList.addAll(Arrays.asList(InternetAddress.parseHeader(recipientHeaderValue, false)));
        }/*from  ww w .  j  a v  a2s  .  com*/

    }
    return recipientList;
}

From source file:davmail.imap.ImapConnection.java

protected void appendMailEnvelopeHeader(StringBuilder buffer, String[] value) {
    buffer.append(' ');
    if (value != null && value.length > 0) {
        try {//from   ww w.ja  va2 s.  co  m
            String unfoldedValue = MimeUtility.unfold(value[0]);
            InternetAddress[] addresses = InternetAddress.parseHeader(unfoldedValue, false);
            if (addresses != null && addresses.length > 0) {
                buffer.append('(');
                for (InternetAddress address : addresses) {
                    buffer.append('(');
                    String personal = address.getPersonal();
                    if (personal != null) {
                        appendEnvelopeHeaderValue(buffer, personal);
                    } else {
                        buffer.append("NIL");
                    }
                    buffer.append(" NIL ");
                    String mail = address.getAddress();
                    int atIndex = mail.indexOf('@');
                    if (atIndex >= 0) {
                        buffer.append('"').append(mail.substring(0, atIndex)).append('"');
                        buffer.append(' ');
                        buffer.append('"').append(mail.substring(atIndex + 1)).append('"');
                    } else {
                        buffer.append("NIL NIL");
                    }
                    buffer.append(')');
                }
                buffer.append(')');
            } else {
                buffer.append("NIL");
            }
        } catch (AddressException e) {
            DavGatewayTray.warn(e);
            buffer.append("NIL");
        } catch (UnsupportedEncodingException e) {
            DavGatewayTray.warn(e);
            buffer.append("NIL");
        }
    } else {
        buffer.append("NIL");
    }
}