Example usage for javax.mail.internet InternetAddress parse

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

Introduction

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

Prototype

public static InternetAddress[] parse(String addresslist) throws AddressException 

Source Link

Document

Parse the given comma separated sequence of addresses into InternetAddress objects.

Usage

From source file:edu.washington.iam.tools.IamMailSender.java

private MimeMessage genMimeMessage(IamMailMessage msg) {
    MimeMessage mime = mailSender.createMimeMessage();
    try {/* ww  w  . j a v  a 2  s. co  m*/
        mime.setRecipients(RecipientType.TO, InternetAddress.parse(msg.getTo()));
        mime.setSubject(msg.makeSubstitutions(msg.getSubject()));
        mime.setReplyTo(InternetAddress.parse(replyTo));
        mime.setFrom(new InternetAddress(msg.getFrom()));
        mime.addHeader("X-Auto-Response-Suppress", "NDR, OOF, AutoReply");
        mime.addHeader("Precedence", "Special-Delivery, never-bounce");
        mime.setText(msg.makeSubstitutions(msg.getText()));
    } catch (MessagingException e) {
        log.error("iam mail build fails: " + e);
    }
    return mime;
}

From source file:org.xsocket.connection.SimpleSmtpClient.java

public void send(String contentType, String text, String from, String to)
        throws IOException, MessagingException {
    MimeMessage msg = new MimeMessage((Session) null);
    msg.setContent(text, contentType);// www  . j a  v  a  2 s . co  m
    msg.addFrom(InternetAddress.parse(from));
    msg.addRecipients(RecipientType.TO, InternetAddress.parse(to));

    send(msg);
}

From source file:com.spartasystems.holdmail.util.TestMailClient.java

public void sendEmail(String fromEmail, String toEmail, String subject, String textBody, String htmlBody) {
    try {/*from   w  ww .j a va  2  s. c  om*/
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);

        // Set the message
        createMultiMimePart(message, textBody, htmlBody);

        Transport.send(message);
    } catch (MessagingException e) {
        throw new HoldMailException("Failed to send email : " + e.getMessage(), e);
    }
}

From source file:org.xwiki.mail.internal.factory.usersandgroups.AddressUserDataExtractor.java

@Override
public Address extract(DocumentReference reference, XWikiDocument document, BaseObject userObject) {
    Address address = null;//from w  w w .java2s.c  om

    // Extract the email
    String email = userObject.getStringValue("email");
    if (!StringUtils.isBlank(email)) {
        // Convert to an address
        try {
            address = InternetAddress.parse(email)[0];
        } catch (AddressException e) {
            // Invalid address, skip it, but log a warning!
            LOGGER.warn(
                    "Found invalid email address [{}] for user [{}]. Email will not been sent to that user.",
                    email, reference);
        }
    } else {
        LOGGER.warn("User [{}] has no email defined. Email will not been sent to that user.", reference);
    }

    return address;
}

From source file:org.openhab.binding.mail.internal.MailBuilder.java

/**
 * Add one or more recipients//  ww  w .j a  v a2s.  c  o m
 *
 * @param recipients comma separated sequence of addresses (must follow RFC822 syntax)
 * @return a MailBuilder
 * @throws AddressException on invalid recipient address
 */
public MailBuilder withRecipients(String recipients) throws AddressException {
    this.recipients.addAll(Arrays.asList(InternetAddress.parse(recipients)));
    return this;
}

From source file:com.thinkbiganalytics.metadata.sla.SlaEmailService.java

/**
 * Send an email/*  w w w. ja  v  a  2  s  .  com*/
 *
 * @param to      the user(s) to send the email to
 * @param subject the subject of the email
 * @param body    the email body
 */
public void sendMail(String to, String subject, String body) {

    try {
        if (testConnection()) {
            MimeMessage message = mailSender.createMimeMessage();
            String fromAddress = StringUtils.defaultIfBlank(emailConfiguration.getFrom(),
                    emailConfiguration.getUsername());
            message.setFrom(new InternetAddress(fromAddress));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(body);
            mailSender.send(message);
            log.debug("Email send to {}", to);
        }
    } catch (MessagingException ex) {
        log.error("Exception while sending mail : {}", ex.getMessage());
        Throwables.propagate(ex);

    }
}

From source file:org.tiogasolutions.notify.processor.smtp.EmailMessage.java

public EmailMessage(String host, String port, List<String> addresses) throws EmailMessageException {

    this.host = host;
    this.port = port;

    for (String address : addresses) {
        try {/*from  w ww . j a  v a 2s  .  co m*/
            InternetAddress[] list = InternetAddress.parse(address);
            toAddresses.addAll(Arrays.asList(list));
        } catch (Exception ex) {
            throw new EmailMessageException("Exception parsing the email address: " + address);
        }
    }
}

From source file:uk.ac.ox.it.ords.api.project.services.impl.SendMailTLS.java

protected void sendMail(String subject, String messageText) throws Exception {

    ////  w w  w  .  ja  va2 s.com
    // Validate Mail server settings
    //
    if (!props.containsKey("mail.smtp.username") || !props.containsKey("mail.smtp.password")
            || !props.containsKey("mail.smtp.host")) {
        log.error("Unable to send emails as email server configuration is missing");
        throw new Exception("Unable to send emails as email server configuration is missing");
    }

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(props.get("mail.smtp.username").toString(),
                    props.get("mail.smtp.password").toString());
        }
    });

    try {
        Message message = new MimeMessage(session);
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
        message.setSubject(subject);
        message.setText(messageText);

        Transport.send(message);

        if (log.isDebugEnabled()) {
            log.debug(String.format("Sent email to %s", email));
            log.debug("with content: " + messageText);
        }
    } catch (MessagingException e) {
        log.error("Unable to send email to " + email, e);
        throw new Exception("Unable to send email to " + email, e);
    }
}

From source file:uk.ac.ox.it.ords.api.database.services.impl.SendMailTLS.java

protected void sendMail(String subject, String messageText) throws Exception {

    ///*from  www.j  av  a 2s.  com*/
    // Validate Mail server settings
    //
    if (!props.containsKey("mail.smtp.username") || !props.containsKey("mail.smtp.password")
            || !props.containsKey("mail.smtp.host")) {
        log.error("Unable to send emails as email server configuration is missing");
        //throw new Exception("Unable to send emails as email server configuration is missing");
        return; // 
    }

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(props.get("mail.smtp.username").toString(),
                    props.get("mail.smtp.password").toString());
        }
    });

    try {
        Message message = new MimeMessage(session);
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
        message.setSubject(subject);
        message.setText(messageText);

        Transport.send(message);

        if (log.isDebugEnabled()) {
            log.debug(String.format("Sent email to %s", email));
            log.debug("with content: " + messageText);
        }
    } catch (MessagingException e) {
        log.error("Unable to send email to " + email, e);
        throw new Exception("Unable to send email to " + email, e);
    }
}

From source file:de.hybris.basecommerce.SimpleSmtpServerUtilsTest.java

@Test
public void testSendSuccess() throws EmailException, AddressException {
    final String origMailPortNumber = Config.getParameter(Config.Params.MAIL_SMTP_PORT);
    final String origMailHost = Config.getParameter(Config.Params.MAIL_SMTP_SERVER);

    SimpleSmtpServer server = null;//from  w ww. j  a v a  2  s.co m
    try {
        server = SimpleSmtpServerUtils.startServer(TEST_START_PORT);
        Assert.assertFalse(server.isStopped());
        Assert.assertTrue(server.getPort() > 0);

        Config.setParameter(Config.Params.MAIL_SMTP_SERVER, "localhost");
        Config.setParameter(Config.Params.MAIL_SMTP_PORT, String.valueOf(server.getPort()));

        final Email email = MailUtils.getPreConfiguredEmail();

        email.setFrom("foo.bar@hybris.com");
        email.setTo(Arrays.asList(InternetAddress.parse("foo.bar@hybris.com")));
        email.setSubject("TEST TEST TEST");
        email.setContent("FOO", Email.TEXT_PLAIN);

        email.send();
    } finally {
        Config.setParameter(Config.Params.MAIL_SMTP_SERVER, origMailHost);
        Config.setParameter(Config.Params.MAIL_SMTP_PORT, origMailPortNumber);

        if (server != null) {
            server.stop();
        }
    }
}