Example usage for javax.mail.internet InternetAddress getAddress

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

Introduction

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

Prototype

public String getAddress() 

Source Link

Document

Get the email address.

Usage

From source file:org.xwiki.watchlist.internal.notification.WatchListEventMimeMessageIterator.java

/**
 * Compute the suffix of a conversation ID. It is done similar to what JavaMail does by default, using the session
 * to extract data (user, host, etc.) that can be set by the client, in case multiple instances of XWiki run on the
 * same machine./*from w  w w.  ja v a 2s.  c o m*/
 */
private String getConversationSuffix() {
    String suffix = null;

    Session session = this.sessionFactory.create(Collections.<String, String>emptyMap());
    InternetAddress addr = InternetAddress.getLocalAddress(session);
    if (addr != null) {
        suffix = addr.getAddress();
    } else {
        // Worst-case default
        suffix = "xwiki@localhost";
    }

    return suffix;
}

From source file:com.redhat.rhn.common.messaging.SmtpMail.java

/**
 * Private helper method to do the heavy lifting of setting the recipients field for
 * a message//from   w ww .  ja  v  a2 s .  c  om
 * @param type The javax.mail.Message.RecipientType (To, CC, or BCC) for the recipients
 * @param recipIn A string array of email addresses
 */
private void setRecipients(RecipientType type, String[] recipIn) {
    log.debug("setRecipients called.");
    Address[] recAddr = null;
    try {
        List tmp = new LinkedList();
        for (int i = 0; i < recipIn.length; i++) {
            InternetAddress addr = new InternetAddress(recipIn[i]);
            log.debug("checking: " + addr.getAddress());
            if (verifyAddress(addr)) {
                log.debug("Address verified.  Adding: " + addr.getAddress());
                tmp.add(addr);
            }
        }
        recAddr = new Address[tmp.size()];
        tmp.toArray(recAddr);
        message.setRecipients(type, recAddr);
    } catch (MessagingException me) {
        String msg = "MessagingException while trying to send email: " + me.toString();
        log.warn(msg);
        throw new JavaMailException(msg, me);
    }
}

From source file:mitm.application.djigzo.james.mailets.Log.java

private String getOriginator(Mail mail) {
    String originator = null;//w w  w  .j av  a  2  s.c o  m

    try {
        InternetAddress address = messageOriginatorIdentifier.getOriginator(mail);

        if (address != null) {
            originator = address.getAddress();

            /*
             * We don't want the invalid@invalid.tld user in the logs
             */
            if (EmailAddressUtils.isInvalidDummyAddress(originator)) {
                originator = "<>";
            }
        }
    } catch (MessagingException e) {
        logger.debug("Error getting originator.", e);
    }

    if (StringUtils.isBlank(originator)) {
        originator = "<>";
    }

    return originator;
}

From source file:org.sakaiproject.email.impl.test.EmailServiceTest.java

@Test
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()));
        }//from  w ww.  ja va  2 s  .co m
        tos.put(type, addrs);
    }
    // add the actual recipients
    LinkedList<EmailAddress> addys = new LinkedList<EmailAddress>();
    for (InternetAddress t : to) {
        addys.add(new EmailAddress(t.getAddress(), t.getPersonal()));
    }
    tos.put(RecipientType.ACTUAL, addys);
    msg.setRecipients(tos);
    // add additional headers
    msg.setHeaders(additionalHeaders);
    // send message
    emailService.send(msg);
}

From source file:org.sakaiproject.email.impl.test.EmailServiceTest.java

@Test
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  ww w. j  a v  a2  s .co m
        tos.put(type, addrs);
    }
    // add the actual recipients
    LinkedList<EmailAddress> addys = new LinkedList<EmailAddress>();
    for (InternetAddress t : to) {
        addys.add(new EmailAddress(t.getAddress(), t.getPersonal()));
    }
    tos.put(RecipientType.ACTUAL, addys);
    msg.setRecipients(tos);
    // add additional headers
    msg.setHeaders(additionalHeaders);
    // add attachments
    msg.setAttachments(attachments);
    // send message
    emailService.send(msg);
}

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

private static void naddresses(PrintStream ps, InternetAddress[] addrs) {
    int count = 0;
    if (addrs != null && addrs.length > 0) {
        for (InternetAddress addr : addrs) {
            if (addr.isGroup()) {
                // 7.4.2: "[RFC-2822] group syntax is indicated by a special form of address
                //         structure in which the host name field is NIL.  If the mailbox name
                //         field is also NIL, this is an end of group marker (semi-colon in RFC
                //         822 syntax).  If the mailbox name field is non-NIL, this is a start of
                //         group marker, and the mailbox name field holds the group name phrase."
                try {
                    String serialized = addr.getAddress();
                    int colon = serialized.indexOf(':');
                    String name = colon == -1 ? serialized : serialized.substring(0, colon);
                    InternetAddress[] members = addr.getGroup(false);

                    if (count++ == 0) {
                        ps.write('(');
                    }/*w  w  w. j  a v  a2s . com*/
                    ps.print("(NIL NIL ");
                    nstring(ps, name);
                    ps.print(" NIL)");
                    if (members != null) {
                        for (InternetAddress member : members) {
                            address(ps, member);
                        }
                    }
                    ps.print("(NIL NIL NIL NIL)");
                } catch (ParseException e) {
                }
            } else if (addr.getAddress() == null) {
                continue;
            } else {
                // 7.4.2: "The fields of an address structure are in the following order: personal
                //         name, [SMTP] at-domain-list (source route), mailbox name, and host name."
                if (count++ == 0) {
                    ps.write('(');
                }
                address(ps, addr);
            }
        }
    }

    if (count == 0) {
        ps.write(NIL, 0, 3);
    } else {
        ps.write(')');
    }
}

From source file:com.cosmicpush.plugins.smtp.EmailMessage.java

public List<String> getTo() {
    List<String> retVal = new ArrayList<String>();
    for (InternetAddress addr : toAddresses) {
        retVal.add(addr.getAddress());
    }/*from   ww  w  .jav  a  2s  .  c o  m*/
    return retVal;
}

From source file:com.cosmicpush.plugins.smtp.EmailMessage.java

public List<String> getReplyTo() {
    List<String> retVal = new ArrayList<String>();
    for (InternetAddress addr : replyToAddress) {
        retVal.add(addr.getAddress());
    }//  w  w  w.  j a v  a 2 s.  com
    return retVal;
}

From source file:com.redhat.rhn.common.messaging.SmtpMail.java

private boolean verifyAddress(InternetAddress addr) {
    log.debug("verifyAddress called ...");
    boolean retval = true;
    String domain = addr.getAddress();
    int domainStart = domain.indexOf('@');
    if (domainStart > -1 && domainStart + 1 < domain.length()) {
        domain = domain.substring(domainStart + 1);

    }/*from   w  ww.  j av  a 2  s  .c o m*/
    if (log.isDebugEnabled()) {
        log.debug("Restricted domains: " + StringUtils.join(restrictedDomains, " | "));
        log.debug("disallowedDomains domains: " + StringUtils.join(disallowedDomains, " | "));
    }
    if (restrictedDomains != null && restrictedDomains.length > 0) {
        if (ArrayUtils.lastIndexOf(restrictedDomains, domain) == -1) {
            log.warn("Address " + addr.getAddress() + " not in restricted domains list");
            retval = false;
        }
    }

    if (retval && disallowedDomains != null && disallowedDomains.length > 0) {
        if (ArrayUtils.lastIndexOf(disallowedDomains, domain) > -1) {
            log.warn("Address " + addr.getAddress() + " in disallowed domains list");
            retval = false;
        }
    }
    log.debug("verifyAddress returning: " + retval);
    return retval;
}

From source file:mitm.application.djigzo.james.matchers.HasValidOriginator.java

@Override
public Collection<MailAddress> matchMail(Mail mail) throws MessagingException {
    InternetAddress originator = messageOriginatorIdentifier.getOriginator(mail);

    boolean match = false;

    /*/*from w  w w . java  2 s.  co  m*/
     * The sender has a valid originator if not null and not equal to invalid email address
     */
    if (originator != null && !EmailAddressUtils.INVALID_EMAIL.equals(originator.getAddress())) {
        match = true;
    }

    if (!mustMatch) {
        match = !match;
    }

    if (match) {
        return MailAddressUtils.getRecipients(mail);
    }

    return null;
}