Example usage for javax.mail.internet InternetAddress toString

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Convert this address into a RFC 822 / RFC 2047 encoded address.

Usage

From source file:com.sonicle.webtop.calendar.ManagerUtils.java

public static String buildOrganizer(UserProfileId profileId) {
    UserProfile.Data ud = WT.getUserData(profileId);
    InternetAddress ia = InternetAddressUtils.toInternetAddress(ud.getEmail().getAddress(),
            ud.getDisplayName());/*from   w  w w .  j a  v  a  2 s  . co m*/
    return ia.toString();
}

From source file:com.arsdigita.util.parameter.EmailParameter.java

protected void doValidate(final Object value, final ErrorList errors) {
    super.doValidate(value, errors);

    final InternetAddress email = (InternetAddress) value;

    if (!s_perl.match(s_regex, email.toString())) {
        final ParameterError error = new ParameterError(this, "The value is not a valid email address");

        errors.add(error);/*from  www .  j a  v a 2s . c  o m*/
    }
}

From source file:com.adobe.acs.commons.email.impl.EmailServiceImpl.java

@Override
public List<String> sendEmail(final String templatePath, final Map<String, String> emailParams,
        final String... recipients) {

    List<String> failureList = new ArrayList<String>();

    if (recipients == null || recipients.length <= 0) {
        throw new IllegalArgumentException(MSG_INVALID_RECIPIENTS);
    }//from  w  w w.  j a v  a  2s.c  o m

    List<InternetAddress> addresses = new ArrayList<InternetAddress>(recipients.length);
    for (String recipient : recipients) {
        try {
            addresses.add(new InternetAddress(recipient));
        } catch (AddressException e) {
            log.warn("Invalid email address {} passed to sendEmail(). Skipping.", recipient);
        }
    }
    InternetAddress[] iAddressRecipients = addresses.toArray(new InternetAddress[addresses.size()]);
    List<InternetAddress> failureInternetAddresses = sendEmail(templatePath, emailParams, iAddressRecipients);

    for (InternetAddress address : failureInternetAddresses) {
        failureList.add(address.toString());
    }

    return failureList;
}

From source file:com.adobe.acs.commons.email.impl.EmailServiceImpl.java

@Override
public List<String> sendEmail(String templatePath, Map<String, String> emailParams,
        Map<String, DataSource> attachments, String... recipients) {
    List<String> failureList = new ArrayList<String>();

    if (recipients == null || recipients.length <= 0) {
        throw new IllegalArgumentException(MSG_INVALID_RECIPIENTS);
    }//  w ww  .  ja v a  2s.  c  o m

    List<InternetAddress> addresses = new ArrayList<InternetAddress>(recipients.length);
    for (String recipient : recipients) {
        try {
            addresses.add(new InternetAddress(recipient));
        } catch (AddressException e) {
            log.warn("Invalid email address {} passed to sendEmail(). Skipping.", recipient);
        }
    }
    InternetAddress[] iAddressRecipients = addresses.toArray(new InternetAddress[addresses.size()]);
    List<InternetAddress> failureInternetAddresses = sendEmail(templatePath, emailParams, attachments,
            iAddressRecipients);

    for (InternetAddress address : failureInternetAddresses) {
        failureList.add(address.toString());
    }

    return failureList;
}

From source file:org.obm.opush.command.email.SmartReplyHandlerTest.java

@Test
public void testSentEmailAttribute() throws Exception {
    testUtils.appendToINBOX(greenMailUser, "eml/OBMFULL-4924-inboxEmail.eml");

    mocksControl.replay();/*from w  ww  .j a va  2 s .  c  om*/
    opushServer.start();
    boolean success = opClient().emailReply(testUtils.loadEmail("eml/OBMFULL-4924-replyingEmail.eml"),
            inboxCollectionId, serverId);
    mocksControl.verify();

    assertThat(success).isTrue();
    assertThat(sentFolder.getMessageCount()).isEqualTo(1);
    InternetAddress fromAddress = (InternetAddress) sentFolder.getMessages().get(0).getMimeMessage()
            .getFrom()[0];
    assertThat(fromAddress.toString()).isEqualTo("Jean Jaures <jaures@sfio.fr>");

}

From source file:net.spfbl.core.Core.java

public static synchronized boolean sendMessage(Message message, int timeout) throws Exception {
    if (message == null) {
        return false;
    } else if (isDirectSMTP()) {
        Server.logInfo("sending e-mail message.");
        Server.logSendMTP("authenticate: false.");
        Server.logSendMTP("start TLS: true.");
        Properties props = System.getProperties();
        props.put("mail.smtp.auth", "false");
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.timeout", Integer.toString(timeout));
        props.put("mail.smtp.connectiontimeout", "3000");
        InternetAddress[] recipients = (InternetAddress[]) message.getAllRecipients();
        Exception lastException = null;
        for (InternetAddress recipient : recipients) {
            String domain = Domain.normalizeHostname(recipient.getAddress(), false);
            for (String mx : Reverse.getMXSet(domain)) {
                mx = mx.substring(1);//from   w w w . j a  va  2s. c o  m
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", mx);
                props.put("mail.smtp.ssl.trust", mx);
                InternetAddress[] recipientAlone = new InternetAddress[1];
                recipientAlone[0] = (InternetAddress) recipient;
                Session session = Session.getDefaultInstance(props);
                SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
                try {
                    transport.setLocalHost(HOSTNAME);
                    Server.logSendMTP("connecting to " + mx + ":25.");
                    transport.connect(mx, 25, null, null);
                    Server.logSendMTP("sending '" + message.getSubject() + "' to " + recipient + ".");
                    transport.sendMessage(message, recipientAlone);
                    Server.logSendMTP("message '" + message.getSubject() + "' sent to " + recipient + ".");
                    Server.logSendMTP("last response: " + transport.getLastServerResponse());
                    lastException = null;
                    break;
                } catch (MailConnectException ex) {
                    Server.logSendMTP("connection failed.");
                    lastException = ex;
                } catch (SendFailedException ex) {
                    Server.logSendMTP("send failed.");
                    throw ex;
                } catch (MessagingException ex) {
                    if (ex.getMessage().contains(" TLS ")) {
                        Server.logSendMTP("cannot establish TLS connection.");
                        if (transport.isConnected()) {
                            transport.close();
                            Server.logSendMTP("connection closed.");
                        }
                        Server.logInfo("sending e-mail message without TLS.");
                        props.put("mail.smtp.starttls.enable", "false");
                        session = Session.getDefaultInstance(props);
                        transport = (SMTPTransport) session.getTransport("smtp");
                        try {
                            transport.setLocalHost(HOSTNAME);
                            Server.logSendMTP("connecting to " + mx + ":25.");
                            transport.connect(mx, 25, null, null);
                            Server.logSendMTP("sending '" + message.getSubject() + "' to " + recipient + ".");
                            transport.sendMessage(message, recipientAlone);
                            Server.logSendMTP(
                                    "message '" + message.getSubject() + "' sent to " + recipient + ".");
                            Server.logSendMTP("last response: " + transport.getLastServerResponse());
                            lastException = null;
                            break;
                        } catch (SendFailedException ex2) {
                            Server.logSendMTP("send failed.");
                            throw ex2;
                        } catch (Exception ex2) {
                            lastException = ex2;
                        }
                    } else {
                        lastException = ex;
                    }
                } catch (Exception ex) {
                    Server.logError(ex);
                    lastException = ex;
                } finally {
                    if (transport.isConnected()) {
                        transport.close();
                        Server.logSendMTP("connection closed.");
                    }
                }
            }
        }
        if (lastException == null) {
            return true;
        } else {
            throw lastException;
        }
    } else if (hasRelaySMTP()) {
        Server.logInfo("sending e-mail message.");
        Server.logSendMTP("authenticate: " + Boolean.toString(SMTP_IS_AUTH) + ".");
        Server.logSendMTP("start TLS: " + Boolean.toString(SMTP_STARTTLS) + ".");
        Properties props = System.getProperties();
        props.put("mail.smtp.auth", Boolean.toString(SMTP_IS_AUTH));
        props.put("mail.smtp.starttls.enable", Boolean.toString(SMTP_STARTTLS));
        props.put("mail.smtp.host", SMTP_HOST);
        props.put("mail.smtp.port", Short.toString(SMTP_PORT));
        props.put("mail.smtp.timeout", Integer.toString(timeout));
        props.put("mail.smtp.connectiontimeout", "3000");
        props.put("mail.smtp.ssl.trust", SMTP_HOST);
        Address[] recipients = message.getAllRecipients();
        TreeSet<String> recipientSet = new TreeSet<String>();
        for (Address recipient : recipients) {
            recipientSet.add(recipient.toString());
        }
        Session session = Session.getDefaultInstance(props);
        SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
        try {
            if (HOSTNAME != null) {
                transport.setLocalHost(HOSTNAME);
            }
            Server.logSendMTP("connecting to " + SMTP_HOST + ":" + SMTP_PORT + ".");
            transport.connect(SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD);
            Server.logSendMTP("sending '" + message.getSubject() + "' to " + recipientSet + ".");
            transport.sendMessage(message, recipients);
            Server.logSendMTP("message '" + message.getSubject() + "' sent to " + recipientSet + ".");
            return true;
        } catch (SendFailedException ex) {
            Server.logSendMTP("send failed.");
            throw ex;
        } catch (AuthenticationFailedException ex) {
            Server.logSendMTP("authentication failed.");
            return false;
        } catch (MailConnectException ex) {
            Server.logSendMTP("connection failed.");
            return false;
        } catch (MessagingException ex) {
            Server.logSendMTP("messaging failed.");
            return false;
        } catch (Exception ex) {
            Server.logError(ex);
            return false;
        } finally {
            if (transport.isConnected()) {
                transport.close();
                Server.logSendMTP("connection closed.");
            }
        }
    } else {
        return false;
    }
}

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

public String getParam() throws Exception {
    StringBuffer result = new StringBuffer();
    InternetAddress tmpFrom = new InternetAddress(this.fromEmail, this.fromFullname, charset);
    if (StringUtils.isEmpty(replyEmail)) {
        replyEmail = fromEmail;/*w  w w .  j a v a  2 s .  c o  m*/
    }
    if (StringUtils.isEmpty(replyFullname)) {
        replyFullname = fromFullname;
    }
    InternetAddress tmpReply = new InternetAddress(this.replyEmail, this.replyFullname, charset);

    result.append("from=\"");
    result.append(doEscape(tmpFrom.toString()));
    result.append("\", ");

    result.append("subject=\"");
    result.append(AgnUtils.propertySaveString(this.subject));
    result.append("\", ");

    result.append("charset=\"");
    result.append(AgnUtils.propertySaveString(this.charset));
    result.append("\", ");

    result.append("linefeed=\"");
    result.append(AgnUtils.propertySaveString(Integer.toString(this.linefeed)));
    result.append("\", ");

    result.append("mailformat=\"");
    result.append(AgnUtils.propertySaveString(Integer.toString(this.mailFormat)));
    result.append("\", ");

    result.append("reply=\"");
    result.append(tmpReply.toString());
    result.append("\", ");

    result.append("onepixlog=\"");
    result.append(AgnUtils.propertySaveString(this.onepixel));
    result.append("\", ");

    super.setParam(result.toString());
    return result.toString();
}

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

/**
 * Getter for property replyAdr.// w  w w .j ava 2  s.  c  o  m
 * @return Value of property replyAdr.
 */
public String getReplyAdr() throws Exception {
    InternetAddress tmpReply = new InternetAddress(this.replyEmail, this.replyFullname, "utf-8");
    return AgnUtils.propertySaveString(tmpReply.toString());
}

From source file:org.alfresco.repo.imap.ImapMessageTest.java

public void testEncodedFromToAddresses() throws Exception {
    // RFC1342//from  w w w .j a v  a2  s. com
    String addressString = "ars.kov@gmail.com";
    String personalString = "?? ";
    InternetAddress address = new InternetAddress(addressString, personalString, "UTF-8");

    // Following method returns the address with quoted personal aka <["?? "] <ars.kov@gmail.com>>
    // NOTE! This should be coincided with RFC822MetadataExtracter. Would 'addresses' be quoted or not? 
    // String decodedAddress = address.toUnicodeString();
    // So, just using decode, for now
    String decodedAddress = MimeUtility.decodeText(address.toString());

    // InternetAddress.toString(new Address[] {address}) - is used in the RFC822MetadataExtracter
    // So, compare with that
    assertFalse("Non ASCII characters in the address should be encoded",
            decodedAddress.equals(InternetAddress.toString(new Address[] { address })));

    MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));

    MimeMessageHelper messageHelper = new MimeMessageHelper(message, false, "UTF-8");

    messageHelper.setText("This is a sample message for ALF-5647");
    messageHelper.setSubject("This is a sample message for ALF-5647");
    messageHelper.setFrom(address);
    messageHelper.addTo(address);
    messageHelper.addCc(address);

    // Creating the message node in the repository
    String name = AlfrescoImapConst.MESSAGE_PREFIX + GUID.generate();
    FileInfo messageFile = fileFolderService.create(testImapFolderNodeRef, name, ContentModel.TYPE_CONTENT);
    // Writing a content.
    new IncomingImapMessage(messageFile, serviceRegistry, message);

    // Getting the transformed properties from the repository
    // cm:originator, cm:addressee, cm:addressees, imap:messageFrom, imap:messageTo, imap:messageCc
    Map<QName, Serializable> properties = nodeService.getProperties(messageFile.getNodeRef());

    String cmOriginator = (String) properties.get(ContentModel.PROP_ORIGINATOR);
    String cmAddressee = (String) properties.get(ContentModel.PROP_ADDRESSEE);
    @SuppressWarnings("unchecked")
    List<String> cmAddressees = (List<String>) properties.get(ContentModel.PROP_ADDRESSEES);
    String imapMessageFrom = (String) properties.get(ImapModel.PROP_MESSAGE_FROM);
    String imapMessageTo = (String) properties.get(ImapModel.PROP_MESSAGE_TO);
    String imapMessageCc = (String) properties.get(ImapModel.PROP_MESSAGE_CC);

    assertNotNull(cmOriginator);
    assertEquals(decodedAddress, cmOriginator);
    assertNotNull(cmAddressee);
    assertEquals(decodedAddress, cmAddressee);
    assertNotNull(cmAddressees);
    assertEquals(1, cmAddressees.size());
    assertEquals(decodedAddress, cmAddressees.get(0));
    assertNotNull(imapMessageFrom);
    assertEquals(decodedAddress, imapMessageFrom);
    assertNotNull(imapMessageTo);
    assertEquals(decodedAddress, imapMessageTo);
    assertNotNull(imapMessageCc);
    assertEquals(decodedAddress, imapMessageCc);
}

From source file:org.apache.solr.handler.dataimport.FsMailEntityProcessor.java

private void addAddressToList(Address[] adresses, List<String> to) throws AddressException {
    for (Address address : adresses) {
        to.add(address.toString());/* ww  w .j a va  2  s. co  m*/
        InternetAddress ia = (InternetAddress) address;
        if (ia.isGroup()) {
            InternetAddress[] group = ia.getGroup(false);
            for (InternetAddress member : group) {
                to.add(member.toString());
            }
        }
    }
}