Example usage for javax.mail.internet InternetAddress getPersonal

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

Introduction

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

Prototype

public String getPersonal() 

Source Link

Document

Get the personal name.

Usage

From source file:org.elasticsearch.river.email.EmailToJson.java

public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException {
    String from = "";
    Address[] froms = msg.getFrom();/*w  w  w . j  a  v a  2  s . c om*/
    if (froms.length < 1)
        throw new MessagingException("?!");

    InternetAddress address = (InternetAddress) froms[0];
    String person = address.getPersonal();
    if (person != null) {
        person = MimeUtility.decodeText(person) + " ";
    } else {
        person = "";
    }
    from = person + "<" + address.getAddress() + ">";

    return from;
}

From source file:com.cubusmail.mail.util.MessageUtils.java

/**
 * Parses the adresslist./*from   w  w  w . j av a 2  s. c om*/
 * 
 * @param addresslist
 * @param charset
 * @return
 */
public static InternetAddress[] parseInternetAddress(String addresslist, String charset)
        throws MessagingException {

    if (addresslist != null) {
        addresslist = addresslist.replace(';', ',');
        InternetAddress[] addressArray = InternetAddress.parse(addresslist);

        if (addressArray != null) {
            for (InternetAddress address : addressArray) {
                String personal = address.getPersonal();
                if (personal != null) {
                    try {
                        address.setPersonal(personal, charset);
                    } catch (UnsupportedEncodingException e) {
                        log.error(e.getMessage(), e);
                    }
                }
            }
        }

        return addressArray;
    }

    return null;
}

From source file:com.aurel.track.util.emailHandling.MailReader.java

/**
 * Retrieves the senders name from this message if possible.
 *
 * @param message//w  ww. j  a va  2s. c  om
 *            the message to be processed
 * @return the senders e-mail address
 */
public static String getSenderName(Message message) {
    String from = null;
    try {
        InternetAddress fromAddress = (InternetAddress) message.getFrom()[0];
        if (null != fromAddress) {
            if (null != fromAddress.getPersonal()) {
                // if there is a name, return it
                from = fromAddress.getPersonal();
            }
        }
    } catch (Exception e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
        // can't do much here
    }
    return from;
}

From source file:com.aurel.track.util.emailHandling.MailReader.java

/**
 * Retrieves the sender as a TPersonBean with first and last name
 *
 * @param message//w  ww .  j  a  va 2  s .  c o  m
 *            the message to be processed
 * @return the sender
 */
public static TPersonBean getSender(Message message) {
    String from = null;
    TPersonBean sender = null;
    String firstName = "";
    String lastName = "";
    try {
        if (message.getFrom() != null && message.getFrom().length > 0) {
            InternetAddress fromAddress = (InternetAddress) message.getFrom()[0];
            if (null != fromAddress) {
                from = fromAddress.getAddress();
                String name = fromAddress.getPersonal();
                if (name != null && !"".equals(name)) {
                    String nameparts[] = name.split("\\s+");
                    for (int i = 0; i < nameparts.length - 1; ++i) {
                        firstName = firstName + " " + nameparts[i];
                    }
                    firstName = firstName.trim();
                    lastName = nameparts[nameparts.length - 1];
                }
                sender = new TPersonBean();
                sender.setEmail(from);
                sender.setFirstName(firstName);
                sender.setLastName(lastName);
            }
        } else {
            LOGGER.warn("No from sender address in message!");
        }
    } catch (Exception e) {
        // can't do much here
        LOGGER.warn("Can't get sender email address", e);
    }
    return sender;
}

From source file:com.zimbra.cs.imap.ImapMessage.java

private static void address(PrintStream ps, InternetAddress addr) {
    String address = addr.getAddress().trim(), route = null;

    // handle obsolete route-addr
    int colon;// ww w.  j  av  a  2  s  . c  o m
    if (address.startsWith("@") && (colon = address.indexOf(':')) != -1) {
        route = address.substring(0, colon);
        address = address.substring(colon + 1);
    }

    String[] parts = address.split("@", 2);
    ps.write('(');
    nstring2047(ps, addr.getPersonal());
    ps.write(' ');
    nstring(ps, route);
    ps.write(' ');
    nstring(ps, parts[0]);
    ps.write(' ');
    nstring(ps, parts.length > 1 ? parts[1] : null);
    ps.write(')');
}

From source file:com.openkm.util.MailUtils.java

/**
 * Conversion from Address to String.//from ww  w .j a va  2  s. c om
 */
public static String addressToString(Address a) {
    if (a != null) {
        InternetAddress ia = (InternetAddress) a;

        if (ia.getPersonal() != null) {
            return ia.getPersonal() + " <" + ia.getAddress() + ">";
        } else {
            return ia.getAddress();
        }
    } else {
        return "";
    }
}

From source file:com.haulmont.cuba.core.app.EmailSender.java

protected void assignFromAddress(SendingMessage sendingMessage, MimeMessage msg) throws MessagingException {
    InternetAddress[] internetAddresses = InternetAddress.parse(sendingMessage.getFrom());
    for (InternetAddress internetAddress : internetAddresses) {
        if (StringUtils.isNotEmpty(internetAddress.getPersonal())) {
            try {
                internetAddress.setPersonal(internetAddress.getPersonal(), StandardCharsets.UTF_8.name());
            } catch (UnsupportedEncodingException e) {
                throw new MessagingException("Unsupported encoding type", e);
            }/*w w w .  j  ava  2 s  .co  m*/
        }
    }

    if (internetAddresses.length == 1) {
        msg.setFrom(internetAddresses[0]);
    } else {
        msg.addFrom(internetAddresses);
    }
}

From source file:org.sakaiproject.email.impl.TestEmailService.java

public void testSendMessageWithoutAttachments() throws Exception {
    // create message with from, subject, content
    EmailMessage msg = new EmailMessage(from.getAddress(), subject, content);
    // add message recipients that appear in the header
    HashMap<RecipientType, List<EmailAddress>> tos = new HashMap<RecipientType, List<EmailAddress>>();
    for (RecipientType type : headerToMap.keySet()) {
        ArrayList<EmailAddress> addrs = new ArrayList<EmailAddress>();
        for (InternetAddress iaddr : headerToMap.get(type)) {
            addrs.add(new EmailAddress(iaddr.getAddress(), iaddr.getPersonal()));
        }//  w  w w  .  j  a va2 s.co m
        tos.put(type, addrs);
    }
    // add the actual recipients
    tos.put(RecipientType.ACTUAL, EmailAddress.toEmailAddress(to));
    msg.setRecipients(tos);
    // add additional headers
    msg.setHeaders(additionalHeaders);
    // send message
    emailService.send(msg);
}

From source file:org.sakaiproject.email.impl.TestEmailService.java

public void testSendEmailMessage() throws Exception {
    // create message with from, subject, content
    EmailMessage msg = new EmailMessage(from.getAddress(), subject + " with attachments", content);
    // add message recipients that appear in the header
    HashMap<RecipientType, List<EmailAddress>> tos = new HashMap<RecipientType, List<EmailAddress>>();
    for (RecipientType type : headerToMap.keySet()) {
        ArrayList<EmailAddress> addrs = new ArrayList<EmailAddress>();
        for (InternetAddress iaddr : headerToMap.get(type)) {
            addrs.add(new EmailAddress(iaddr.getAddress(), iaddr.getPersonal()));
        }/*from  w w  w  . ja  va 2 s .co m*/
        tos.put(type, addrs);
    }
    // add the actual recipients
    tos.put(RecipientType.ACTUAL, EmailAddress.toEmailAddress(to));
    msg.setRecipients(tos);
    // add additional headers
    msg.setHeaders(additionalHeaders);
    // add attachments
    msg.setAttachments(attachments);
    // send message
    emailService.send(msg);
}

From source file:ninja.postoffice.commonsmail.CommonsmailHelperImpl.java

@Override
public Tuple<String, String> createValidEmailFromString(String email) throws AddressException {

    InternetAddress internetAddress = new InternetAddress(email);

    Tuple<String, String> tuple = new Tuple<String, String>(internetAddress.getAddress(),
            internetAddress.getPersonal());

    return tuple;

}