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:edu.stanford.muse.index.EmailDocument.java

private static void maskEmailDomain(Address[] addrs, AddressBook ab) {
    if (addrs != null) {
        for (Address a : addrs) {
            if (a instanceof InternetAddress) {
                InternetAddress i = (InternetAddress) a;
                i.setAddress(ab.getMaskedEmail(i.getAddress()));
            }// ww  w  .  j a  va 2s.  c  o m
        }
    }
}

From source file:mailbox.EmailHandler.java

private static void handleMessage(@Nonnull IMAPMessage msg) {
    Exception exception = null;/*from   w w  w  . ja va  2 s  .  c o  m*/
    long startTime = System.currentTimeMillis();
    User author;

    try {
        // Ignore auto-replied emails to avoid suffering from tons of
        // vacation messages. For more details about auto-replied emails,
        // see https://tools.ietf.org/html/rfc3834
        if (isAutoReplied(msg)) {
            return;
        }
    } catch (MessagingException e) {
        play.Logger.warn("Failed to determine whether the email is auto-replied or not: " + msg, e);
    }

    try {
        // Ignore the email if there is an email with the same id. It occurs
        // quite frequently because mail servers send an email twice if the
        // email has two addresses differ from each other: e.g.
        // yobi+my/proj@mail.com and yobi+your/proj@mail.com.
        OriginalEmail sameMessage = OriginalEmail.finder.where().eq("messageId", msg.getMessageID())
                .findUnique();
        if (sameMessage != null) {
            // Warn if the older email was handled one hour or more ago. Because it is
            // quite long time so that possibly the ignored email is actually
            // new one which should be handled.
            if (sameMessage.getHandledDate().before(new DateTime().minusHours(1).toDate())) {
                String warn = String.format(
                        "This email '%s' is ignored because an email with"
                                + " the same id '%s' was already handled at '%s'",
                        msg, sameMessage.messageId, sameMessage.getHandledDate());
                play.Logger.warn(warn);
            }
            return;
        }
    } catch (MessagingException e) {
        play.Logger.warn("Failed to determine whether the email is duplicated or not: " + msg, e);
    }

    InternetAddress[] senderAddresses;

    try {
        senderAddresses = (InternetAddress[]) msg.getFrom();
    } catch (Exception e) {
        play.Logger.error("Failed to get senders from an email", e);
        return;
    }

    if (senderAddresses == null || senderAddresses.length == 0) {
        play.Logger.warn("This email has no sender: " + msg);
        return;
    }

    for (InternetAddress senderAddress : senderAddresses) {
        List<String> errors = new ArrayList<>();

        author = User.findByEmail(senderAddress.getAddress());

        if (author.isAnonymous()) {
            continue;
        }

        try {
            createResources(msg, author, errors);
        } catch (MailHandlerException e) {
            exception = e;
            errors.add(e.getMessage());
        } catch (Exception e) {
            exception = e;
            String shortDescription;
            try {
                shortDescription = IMAPMessageUtil.asString(msg);
            } catch (MessagingException e1) {
                shortDescription = msg.toString();
            }
            play.Logger.warn("Failed to process an email: " + shortDescription, e);
            errors.add("Unexpected error occurs");
        }

        if (errors.size() > 0) {
            String username = senderAddress.getPersonal();
            String emailAddress = senderAddress.getAddress();
            String helpMessage = getHelpMessage(Lang.apply(author.getPreferredLanguage()), username, errors);
            reply(msg, username, emailAddress, helpMessage);
        }

        try {
            log(msg, startTime, exception);
        } catch (MessagingException e) {
            play.Logger.warn("Failed to log mail request", e);
        }

        try {
            MailboxService.updateLastSeenUID(msg);
        } catch (MessagingException e) {
            play.Logger.warn("Failed to update the lastSeenUID", e);
        }
    }
}

From source file:com.aurel.track.util.event.MailHandler.java

public static TPersonBean getReplayTo(TProjectBean projectBean, TPersonBean changedByPerson) {
    if (projectBean != null) {
        String projectTrackSystemEmail = PropertiesHelper.getProperty(projectBean.getMoreProperties(),
                TProjectBean.MOREPPROPS.TRACK_EMAIL_PROPERTY);
        String projectEmailPersonName = PropertiesHelper.getProperty(projectBean.getMoreProperties(),
                TProjectBean.MOREPPROPS.EMAIL_PERSONAL_NAME);
        boolean useTrackFromAddressDisplay = "true".equalsIgnoreCase(PropertiesHelper.getProperty(
                projectBean.getMoreProperties(), TProjectBean.MOREPPROPS.USE_TRACK_FROM_ADDRESS_DISPLAY));

        if (useTrackFromAddressDisplay && projectTrackSystemEmail != null
                && projectTrackSystemEmail.length() > 0) {
            boolean useTrackFromAddressAsReplay = "true".equalsIgnoreCase(PropertiesHelper.getProperty(
                    projectBean.getMoreProperties(), TProjectBean.MOREPPROPS.USE_TRACK_FROM_AS_REPLAY_TO));
            if (useTrackFromAddressAsReplay) {
                //verify address
                try {
                    InternetAddress tmp = new InternetAddress(projectTrackSystemEmail);
                    LOGGER.info(tmp.getAddress() + " is valid!");
                    TPersonBean sendFrom = new TPersonBean("", projectEmailPersonName, projectTrackSystemEmail);
                    return sendFrom;
                } catch (AddressException e) {
                    LOGGER.error("The email address:'" + projectTrackSystemEmail + "' set in project"
                            + projectBean.getLabel() + " is invalid!");
                }/*from w  ww  .j  a v a 2s.c om*/
            }
        }
    }
    return null;
}

From source file:org.freebxml.omar.server.common.Utility.java

/**
 * Mimics javamail boundary id generator
 *//*  w w  w .  j  av  a2 s .  c o  m*/
public static String getMimeBoundary() {
    javax.mail.Session session = null; //Session.getDefaultInstance(new Properties());

    String s = null;
    javax.mail.internet.InternetAddress internetaddress = javax.mail.internet.InternetAddress
            .getLocalAddress(session);

    if (internetaddress != null) {
        s = internetaddress.getAddress();
    } else {
        s = "javamailuser@localhost";
    }

    StringBuffer stringbuffer = new StringBuffer();
    stringbuffer.append(stringbuffer.hashCode()).append('.').append(System.currentTimeMillis()).append('.')
            .append("omar.").append(s);

    return stringbuffer.toString();
}

From source file:com.aurel.track.util.event.MailHandler.java

/**
 * Gets the sender text/* w w w. j a  v  a 2 s  .co  m*/
 * @param projectBean
 * @param changedByPerson
 * @return
 */
public static TPersonBean getSendFrom(TProjectBean projectBean, TPersonBean changedByPerson) {
    if (projectBean != null) {
        String projectTrackSystemEmail = PropertiesHelper.getProperty(projectBean.getMoreProperties(),
                TProjectBean.MOREPPROPS.TRACK_EMAIL_PROPERTY);
        String projectEmailPersonName = PropertiesHelper.getProperty(projectBean.getMoreProperties(),
                TProjectBean.MOREPPROPS.EMAIL_PERSONAL_NAME);
        boolean useTrackFromAddress = "true".equalsIgnoreCase(PropertiesHelper.getProperty(
                projectBean.getMoreProperties(), TProjectBean.MOREPPROPS.USE_TRACK_FROM_ADDRESS_DISPLAY));

        if (useTrackFromAddress && projectTrackSystemEmail != null && projectTrackSystemEmail.length() > 0) {
            boolean useTrackFromAddressAsReplay = "true".equalsIgnoreCase(PropertiesHelper.getProperty(
                    projectBean.getMoreProperties(), TProjectBean.MOREPPROPS.USE_TRACK_FROM_AS_REPLAY_TO));
            if (useTrackFromAddressAsReplay == false) {
                //verify address
                try {
                    InternetAddress tmp = new InternetAddress(projectTrackSystemEmail);
                    LOGGER.info(tmp.getAddress() + " is valid!");
                    TPersonBean sendFrom = new TPersonBean("", projectEmailPersonName, projectTrackSystemEmail);
                    return sendFrom;
                } catch (AddressException e) {
                    LOGGER.error("The email address:'" + projectTrackSystemEmail + "' set in project"
                            + projectBean.getLabel() + " is invalid!");
                }
            }
        }
    }
    String emailAddress = ApplicationBean.getInstance().getSiteBean().getSendFrom().getEmail();
    TPersonBean sendFromPersonbean;
    if (ApplicationBean.getInstance().getSiteBean().getUseTrackFromAddressDisplay()
            .equals(TSiteBean.SEND_FROM_MODE.SYSTEM)) {
        //use the track-email address as sender address
        sendFromPersonbean = ApplicationBean.getInstance().getSiteBean().getSendFrom();
        sendFromPersonbean.setEmail(emailAddress);
    } else {
        // we use the one who changed the item as sender address
        sendFromPersonbean = new TPersonBean(changedByPerson.getFirstName(), changedByPerson.getLastName(),
                changedByPerson.getEmail());
        if (sendFromPersonbean.getLastName() == null) {
            sendFromPersonbean.setLastName("");
        }
        sendFromPersonbean.setLastName(
                sendFromPersonbean.getLastName() + ApplicationBean.getInstance().getSiteBean().getTrackEmail());
    }
    LOGGER.debug("send from:" + sendFromPersonbean.getFullName() + ": " + sendFromPersonbean.getEmail());
    return sendFromPersonbean;
}

From source file:it.cnr.icar.eric.server.common.Utility.java

/**
 * Mimics javamail boundary id generator
 *//*  w  ww.  ja  v a  2  s. c  o  m*/
public static String getMimeBoundary() {
    javax.mail.Session session = null; //Session.getDefaultInstance(new Properties());

    String s = null;
    javax.mail.internet.InternetAddress internetaddress = javax.mail.internet.InternetAddress
            .getLocalAddress(session);

    if (internetaddress != null) {
        s = internetaddress.getAddress();
    } else {
        s = "javamailuser@localhost";
    }

    StringBuffer stringbuffer = new StringBuffer();
    stringbuffer.append(stringbuffer.hashCode()).append('.').append(System.currentTimeMillis()).append('.')
            .append("eric.").append(s);

    return stringbuffer.toString();
}

From source file:com.joshlong.lazyblogger.integrations.EmailToBlogTransformer.java

private String emailFromAddress(Address address) {
    if (address instanceof InternetAddress) {
        InternetAddress iaInternetAddress = (InternetAddress) address;
        return iaInternetAddress.getAddress();
    }//w ww. j  a v a  2  s. c  om
    return address.toString();
}

From source file:ru.org.linux.user.RegisterRequestValidator.java

protected void checkEmail(InternetAddress email, Errors errors) {
    if (BAD_DOMAINS.contains(email.getAddress().replaceFirst("^[^@]+@", "").toLowerCase())) {
        errors.reject("email", " email ");
    }/*from   ww w .  j  av a 2  s. c o  m*/
}

From source file:org.nuxeo.ecm.platform.mail.action.CheckSenderAction.java

public boolean execute(ExecutionContext context) throws MessagingException {
    Message message = context.getMessage();
    Address[] addresses = message.getFrom();
    if (addresses == null || addresses.length == 0 || !(addresses[0] instanceof InternetAddress)) {
        log.debug("No internet messages, stopping the pipe: " + message);
        return false;
    }//w w w  . ja va2  s  . c om
    InternetAddress address = (InternetAddress) addresses[0];
    String principal = getPrincipal(address.getAddress());
    if (principal == null) {
        log.debug("Sender not in user directory. Stop processing");
        return false;
    }
    context.put("sender", principal);
    return true;
}

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

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

    Matcher matcher = pattern.matcher(originator != null ? originator.getAddress() : "");

    if (matcher.matches()) {
        return MailAddressUtils.getRecipients(mail);
    }//from  ww  w. ja  v a  2 s.c  o  m

    return Collections.emptyList();
}