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:mitm.application.djigzo.james.mailets.SenderTemplatePropertySendMail.java

@Override
protected Template getTemplate(Mail mail, final SimpleHash root) throws IOException, MessagingException {
    Template template = null;//from w w w . ja  v a 2s. c  om

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

        if (templateProperty != null && originator != null) {
            User user = userWorkflow.getUser(originator.getAddress(),
                    UserWorkflow.GetUserMode.CREATE_IF_NOT_EXIST);

            template = getTemplateFromUser(user, root);

            /*
             * Get the property values from the user so they can be stored in the template root
             */
            if (userProperties != null) {
                UserProperties properties = user.getUserPreferences().getProperties();

                for (String userProperty : userProperties) {
                    userProperty = StringUtils.trimToNull(userProperty);

                    if (userProperty == null) {
                        continue;
                    }

                    String value = properties.getProperty(userProperty, false);

                    if (value != null) {
                        root.put(userProperty, value);
                    }
                }
            }
        }

        if (template == null) {
            getLogger().debug("No template found for " + originator);
            /*
             * The sender was not a valid user or did not have a template. Use the default static 
             * template specified in the mailet config. 
             */
            template = super.getTemplate(mail, root);
        }
    } catch (HierarchicalPropertiesException e) {
        throw new MessagingException("Error getting template.", e);
    }

    return template;
}

From source file:com.cubusmail.user.UserAccountDao.java

/**
 * @param addresses/*  w  w w . j a  va 2  s  . c o  m*/
 */
public void saveRecipients(UserAccount account, InternetAddress[] addresses) {

    ContactFolder recipientFolder = getRecipientContactFolder(account);

    if (recipientFolder == null) {
        recipientFolder = new ContactFolder(ContactFolderType.RECIPIENTS);
        recipientFolder.setFolderName("Recipients");
        recipientFolder.setUserAccount(account);
        saveContactFolder(recipientFolder);
    }
    if (addresses != null) {
        for (InternetAddress address : addresses) {
            Contact contact = getContactByEmail(recipientFolder, address.getAddress());
            if (contact == null) {
                contact = new Contact();
                contact.setLastName(address.getPersonal());
                contact.setEmail(address.getAddress());
                contact.setContactFolder(recipientFolder);
                saveContact(contact);
            }
        }
    }
}

From source file:com.duroty.application.open.manager.OpenManager.java

/**
 * DOCUMENT ME!/*from  w  ww . ja va  2 s.  c om*/
 *
 * @param msession DOCUMENT ME!
 * @param from DOCUMENT ME!
 * @param to DOCUMENT ME!
 * @param username DOCUMENT ME!
 * @param password DOCUMENT ME!
 * @param signature DOCUMENT ME!
 *
 * @throws Exception DOCUMENT ME!
 */
private void notifyToAdmins(Session msession, InternetAddress from, InternetAddress[] to, String user)
        throws Exception {
    try {
        HtmlEmail email = new HtmlEmail();
        email.setMailSession(msession);

        email.setFrom(from.getAddress(), from.getPersonal());

        HashSet aux = new HashSet(to.length);
        Collections.addAll(aux, to);
        email.setTo(aux);

        email.setSubject("User register in Duroty System");
        email.setHtmlMsg(
                "<p>The user solicits register into the system</p><p>The user is: <b>" + user + "</b></p>");

        email.setCharset(MimeUtility.javaCharset(Charset.defaultCharset().displayName()));

        email.send();
    } finally {
    }
}

From source file:org.agnitas.beans.impl.MediatypeEmailImpl.java

public void setParam(String param) throws Exception {
    int tmp = 0;//from   w  w w. j a v a 2 s. c om
    String from = unEscape(AgnUtils.findParam("from", param));

    if (from.length() > 0) {
        InternetAddress adr = new InternetAddress(from);

        this.fromEmail = adr.getAddress();
        this.fromFullname = adr.getPersonal();
    } else {
        this.fromEmail = "";
        this.fromFullname = "";
    }

    from = AgnUtils.findParam("reply", param);
    if (from == null) {
        from = AgnUtils.findParam("from", param);
    }

    if (from.length() > 0) {
        InternetAddress adr = new InternetAddress(from);

        this.replyEmail = adr.getAddress();
        this.replyFullname = adr.getPersonal();
    } else {
        this.replyEmail = "";
        this.replyFullname = "";
    }

    this.charset = AgnUtils.findParam("charset", param);
    if (this.charset == null) {
        this.charset = DEFAULT_CHARSET;
    }
    this.subject = AgnUtils.findParam("subject", param);
    try {
        tmp = Integer.parseInt(AgnUtils.findParam("mailformat", param));
    } catch (Exception e) {
        tmp = 2; // default: Offline-HTML
    }
    this.mailFormat = tmp;
    try {
        tmp = Integer.parseInt(AgnUtils.findParam("linefeed", param));
    } catch (Exception e) {
        tmp = 72; // default: after 72 characters
    }
    this.linefeed = tmp;

    this.onepixel = AgnUtils.findParam("onepixlog", param);
    if (this.onepixel == null) {
        this.onepixel = MediatypeEmailImpl.ONEPIXEL_NONE;
    }
}

From source file:org.silverpeas.core.mail.TestSmtpMailSending.java

private void assertMailSent(MailToSend verifiedMailToSend) throws Exception {
    assertThat("assertMailSent is compatible with one receiver only...", verifiedMailToSend.getTo(),
            hasSize(1));//from w w w  .  j a  v a2s .co m

    assertThat(verifiedMailToSend.getTo().getRecipientType().getTechnicalType(), is(Message.RecipientType.TO));

    MimeMessage[] messages = greenMailRule.getReceivedMessages();
    assertThat(messages, arrayWithSize(1));

    MimeMessage sentMessage = messages[0];
    MailAddress originalReceiverMailAddress = verifiedMailToSend.getTo().iterator().next();

    assertThat(sentMessage.getFrom().length, is(1));
    assertThat(sentMessage.getFrom()[0], instanceOf(InternetAddress.class));
    InternetAddress internetAddressFrom = (InternetAddress) sentMessage.getFrom()[0];
    assertThat(internetAddressFrom.getAddress(), is(verifiedMailToSend.getFrom().getEmail()));
    assertThat(internetAddressFrom.getPersonal(),
            is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null)));

    assertThat(sentMessage.getRecipients(Message.RecipientType.TO).length, is(1));
    assertThat(sentMessage.getRecipients(Message.RecipientType.CC), nullValue());
    assertThat(sentMessage.getRecipients(Message.RecipientType.BCC), nullValue());
    assertThat(sentMessage.getRecipients(Message.RecipientType.TO)[0], instanceOf(InternetAddress.class));
    InternetAddress internetAddressTo = (InternetAddress) sentMessage
            .getRecipients(Message.RecipientType.TO)[0];
    assertThat(internetAddressTo.getAddress(), is(originalReceiverMailAddress.getEmail()));
    assertThat(internetAddressTo.getPersonal(), nullValue());

    assertThat(sentMessage.getSubject(), is(verifiedMailToSend.getSubject()));
    if (verifiedMailToSend.getContent().getValue() instanceof Multipart) {
        assertThat(sentMessage.getContent(), instanceOf(verifiedMailToSend.getContent().getValue().getClass()));
    } else {
        assertThat(sentMessage.getContent().toString().replaceAll("[\n\r]*$", ""),
                is(verifiedMailToSend.getContent().getValue()));
    }

    assertThat(DateUtils.addSeconds(sentMessage.getSentDate(), 10), greaterThanOrEqualTo(new Date()));

    assertThat(sentMessage.getReplyTo().length, is(1));
    if (verifiedMailToSend.isReplyToRequired()) {
        assertThat(sentMessage.getHeader("Reply-To"), notNullValue());
        assertThat(sentMessage.getReplyTo()[0], instanceOf(InternetAddress.class));
        InternetAddress internetAddressReplyTo = (InternetAddress) sentMessage.getReplyTo()[0];
        assertThat(internetAddressReplyTo.getAddress(), is(verifiedMailToSend.getFrom().getEmail()));
        assertThat(internetAddressReplyTo.getPersonal(),
                is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null)));
    } else {
        assertThat(sentMessage.getHeader("Reply-To"), nullValue());
        assertThat(sentMessage.getReplyTo()[0].toString(), is(internetAddressFrom.toString()));
    }
}

From source file:com.googlecode.gmail4j.javamail.JavaMailGmailMessage.java

/**
 * Gets a {@link List} of {@link EmailAddress} by {@link RecipientType}
 * /* w  w  w . j  av  a2 s .  c  o m*/
 * @param type Recipient type
 * @return List of Addresses
 * @throws MessagingException in case something is wrong
 */
private List<EmailAddress> getAddresses(final RecipientType type) throws MessagingException {
    final List<EmailAddress> addresses = new ArrayList<EmailAddress>();
    for (final Address addr : source.getRecipients(type)) {
        final InternetAddress temp = (InternetAddress) addr;
        addresses.add(new EmailAddress(temp.getPersonal(), temp.getAddress()));
    }
    return addresses;
}

From source file:com.duroty.application.open.manager.OpenManager.java

/**
 * DOCUMENT ME!// w  w w.j  a v a2 s. c om
 *
 * @param session DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param from DOCUMENT ME!
 * @param to DOCUMENT ME!
 * @param subject DOCUMENT ME!
 * @param body DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
private void sendData(Session msession, InternetAddress from, InternetAddress to, String username,
        String password, String signature) throws Exception {
    try {
        HtmlEmail email = new HtmlEmail();
        email.setMailSession(msession);

        email.setFrom(from.getAddress(), from.getPersonal());
        email.addTo(to.getAddress(), to.getPersonal());

        email.setSubject("Duroty System");
        email.setHtmlMsg("<p>Username: <b>" + username + "</b></p><p>Password: " + password + "<b></b></p><p>"
                + signature + "</p>");

        email.setCharset(MimeUtility.javaCharset(Charset.defaultCharset().displayName()));

        email.send();
    } finally {
    }
}

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

private boolean addPhoneNumberAction(final Mail mail, MailAddress recipient, String phoneNumber)
        throws MessagingException, HierarchicalPropertiesException {
    String defaultCountryCode = null;

    User user = null;//from   ww  w. j ava  2s  .c om

    InternetAddress originator = messageOriginatorIdentifier.getOriginator(mail);

    if (originator != null) {
        user = userWorkflow.getUser(originator.getAddress(), GetUserMode.CREATE_IF_NOT_EXIST);
    }

    if (user != null) {
        defaultCountryCode = user.getUserPreferences().getProperties().getPhoneDefaultCountryCode();
    }

    phoneNumber = cleanPhoneNumber(phoneNumber, defaultCountryCode);

    boolean added = false;

    if (StringUtils.isNotEmpty(phoneNumber)) {
        setRecipientPhoneNumber(recipient, phoneNumber);

        added = true;
    } else {
        logger.warn("Phone number is missing.");
    }

    return added;
}

From source file:com.googlecode.gmail4j.javamail.JavaMailGmailMessage.java

@Override
public EmailAddress getFrom() {
    if (from == null) {
        try {/*w w w.j  ava  2s.c om*/
            final InternetAddress f = (InternetAddress) source.getFrom()[0];
            from = new EmailAddress(f.getPersonal(), f.getAddress());
        } catch (final Exception e) {
            throw new GmailException("Failed getting from address", e);
        }
    }
    return from;
}

From source file:org.silverpeas.core.mail.SmtpMailSendingTest.java

private void assertMailSent(MailToSend verifiedMailToSend, GreenMailOperations mail) throws Exception {
    assertThat("assertMailSent is compatible with one receiver only...", verifiedMailToSend.getTo(),
            hasSize(1));/*  ww w.ja  va2  s . co  m*/

    assertThat(verifiedMailToSend.getTo().getRecipientType().getTechnicalType(), is(Message.RecipientType.TO));

    MimeMessage[] messages = mail.getReceivedMessages();
    assertThat(messages, arrayWithSize(1));

    MimeMessage sentMessage = messages[0];
    MailAddress originalReceiverMailAddress = verifiedMailToSend.getTo().iterator().next();

    assertThat(sentMessage.getFrom().length, is(1));
    assertThat(sentMessage.getFrom()[0], instanceOf(InternetAddress.class));
    InternetAddress internetAddressFrom = (InternetAddress) sentMessage.getFrom()[0];
    assertThat(internetAddressFrom.getAddress(), is(verifiedMailToSend.getFrom().getEmail()));
    assertThat(internetAddressFrom.getPersonal(),
            is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null)));

    assertThat(sentMessage.getRecipients(Message.RecipientType.TO).length, is(1));
    assertThat(sentMessage.getRecipients(Message.RecipientType.CC), nullValue());
    assertThat(sentMessage.getRecipients(Message.RecipientType.BCC), nullValue());
    assertThat(sentMessage.getRecipients(Message.RecipientType.TO)[0], instanceOf(InternetAddress.class));
    InternetAddress internetAddressTo = (InternetAddress) sentMessage
            .getRecipients(Message.RecipientType.TO)[0];
    assertThat(internetAddressTo.getAddress(), is(originalReceiverMailAddress.getEmail()));
    assertThat(internetAddressTo.getPersonal(), nullValue());

    assertThat(sentMessage.getSubject(), is(verifiedMailToSend.getSubject()));
    if (verifiedMailToSend.getContent().getValue() instanceof Multipart) {
        assertThat(sentMessage.getContent(), instanceOf(verifiedMailToSend.getContent().getValue().getClass()));
    } else {
        assertThat(sentMessage.getContent().toString().replaceAll("[\n\r]*$", ""),
                is(verifiedMailToSend.getContent().getValue()));
    }

    assertThat(DateUtils.addSeconds(sentMessage.getSentDate(), 10), greaterThanOrEqualTo(new Date()));

    assertThat(sentMessage.getReplyTo().length, is(1));
    if (verifiedMailToSend.isReplyToRequired()) {
        assertThat(sentMessage.getHeader("Reply-To"), notNullValue());
        assertThat(sentMessage.getReplyTo()[0], instanceOf(InternetAddress.class));
        InternetAddress internetAddressReplyTo = (InternetAddress) sentMessage.getReplyTo()[0];
        assertThat(internetAddressReplyTo.getAddress(), is(verifiedMailToSend.getFrom().getEmail()));
        assertThat(internetAddressReplyTo.getPersonal(),
                is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null)));
    } else {
        assertThat(sentMessage.getHeader("Reply-To"), nullValue());
        assertThat(sentMessage.getReplyTo()[0].toString(), is(internetAddressFrom.toString()));
    }
}