Example usage for javax.mail.internet MimeMessage getAllRecipients

List of usage examples for javax.mail.internet MimeMessage getAllRecipients

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage getAllRecipients.

Prototype

@Override
public Address[] getAllRecipients() throws MessagingException 

Source Link

Document

Get all the recipient addresses for the message.

Usage

From source file:org.cherchgk.test.ui.RestorePasswordUITest.java

@Test
public void testSuccessfulRestorePassword() throws MessagingException, IOException {
    openRestorePasswordPage();//from w w  w . jav  a 2  s.  c om
    restorePassword("test-user@example.com");
    $(Selectors.withText(RestorePasswordAction.SUCCESS_MESSAGE)).shouldBe(Condition.visible);
    assertEquals(1, mailServer.getReceivedMessages().length);
    MimeMessage message = mailServer.getReceivedMessages()[0];
    assertEquals("test-user@example.com", message.getAllRecipients()[0].toString());

    // ?, ? ??   ?,     ? ?  ?
    String text = message.getContent().toString();
    Matcher matcher = Pattern.compile(".*<a href=\"(.*)\">.*").matcher(text);
    assertTrue(matcher.find());
    String setNewPasswordPageUrl = matcher.group(1);
    open(setNewPasswordPageUrl);

    SelenideElement passwordField = $(By.name("password"));
    SelenideElement password2Field = $(By.name("password2"));
    SelenideElement setNewPasswordButton = $(By.id("set-new-password-button"));
    passwordField.shouldBe(Condition.visible);
    password2Field.shouldBe(Condition.visible);
    setNewPasswordButton.shouldBe(Condition.visible);

    passwordField.setValue("");
    password2Field.setValue("321");
    setNewPasswordButton.click();
    $(Selectors.withText(SetNewPasswordAction.PASSWORD_CANNOT_BE_EMPTY)).shouldBe(Condition.visible);

    passwordField.setValue("123");
    password2Field.setValue("321");
    setNewPasswordButton.click();
    $(Selectors.withText(SetNewPasswordAction.PASSWORDS_MUST_BE_EQUAL)).shouldBe(Condition.visible);

    passwordField.setValue("123");
    password2Field.setValue("123");
    setNewPasswordButton.click();
    $(Selectors.withText(SetNewPasswordAction.PASSWORD_SUCCESSFULLY_CHANGED)).shouldBe(Condition.visible);

    open(setNewPasswordPageUrl);
    $(Selectors.withText(ShowSetNewPasswordPageAction.TOKEN_IS_INVALID)).shouldBe(Condition.visible);
    $(By.name("password")).shouldBe(Condition.not(Condition.exist));
    $(By.name("password2")).shouldBe(Condition.not(Condition.exist));
    $(By.id("set-new-password-button")).shouldBe(Condition.not(Condition.exist));
}

From source file:org.xwiki.administration.test.ui.ResetPasswordIT.java

protected Map<String, String> getMessageContent(MimeMessage message) throws Exception {
    Map<String, String> messageMap = new HashMap<String, String>();

    Address[] addresses = message.getAllRecipients();
    Assert.assertTrue(addresses.length == 1);
    messageMap.put("recipient", addresses[0].toString());

    messageMap.put("subjectLine", message.getSubject());

    Multipart mp = (Multipart) message.getContent();

    BodyPart plain = getPart(mp, "text/plain");
    if (plain != null) {
        messageMap.put("textPart", IOUtils.toString(plain.getInputStream()));
    }// w  w  w . ja v  a  2 s  .  co m
    BodyPart html = getPart(mp, "text/html");
    if (html != null) {
        messageMap.put("htmlPart", IOUtils.toString(html.getInputStream()));
    }

    return messageMap;
}

From source file:com.hmsinc.epicenter.surveillance.notification.MailingEventNotifierTest.java

@Test
public void shouldBeSentToOneProperRecepient() throws MessagingException {
    MimeMessage message = (MimeMessage) sentMessages.get(0);
    Address[] recipients = message.getAllRecipients();
    assertEquals(1, recipients.length);//  w  w  w . ja  v  a2 s .com
    assertEquals(USER_EMAIL, recipients[0].toString());
}

From source file:net.sourceforge.subsonic.backend.service.EmailSession.java

private void sendMessage(MimeMessage message) throws MessagingException {
    Transport transport = null;// ww w  .  j av  a2 s  .c  om
    try {
        transport = session.getTransport("smtps");
        transport.connect(USER, password);
        transport.sendMessage(message, message.getAllRecipients());
    } finally {
        if (transport != null) {
            transport.close();
        }
    }
}

From source file:isl.FIMS.utils.Utils.java

public static boolean sendEmail(String to, String subject, String context) {
    boolean isSend = false;

    try {//from   ww w  .j  a  va 2 s .  c  om

        String host = "smtp.gmail.com";
        String user = emailAdress;
        String password = emailPass;

        String port = "587";
        String from = "no-reply-" + systemName + "@gmail.com";
        Properties props = System.getProperties();

        props.put("mail.smtp.user", user);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        //  props.put("mail.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.EnableSSL.enable", "true");
        //     props.put("mail.smtp.socketFactory.port", port);
        //    props.put("mail.smtp.socketFactory.class",
        //            "javax.net.ssl.SSLSocketFactory");
        //    props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props, null);
        //session.setDebug(true);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));

        // To get the array of addresses
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        message.setSubject(subject, "UTF-8");
        message.setContent(context, "text/html; charset=UTF-8");
        Transport transport = session.getTransport("smtp");
        try {
            transport.connect(host, user, password);
            transport.sendMessage(message, message.getAllRecipients());
        } finally {
            transport.close();
        }
        isSend = true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return isSend;

}

From source file:com.enjoyxstudy.selenium.autoexec.mail.MailSender.java

/**
 * @param mimeMessage// www. ja  va 2 s.c  om
 * @throws MessagingException
 */
private void _send(MimeMessage mimeMessage) throws MessagingException {

    Transport transport = null;

    try {

        transport = session.getTransport();
        transport.connect();

        mimeMessage.setSentDate(new Date());
        mimeMessage.saveChanges();

        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
    } finally {
        if (transport != null && transport.isConnected()) {
            transport.close();
        }
    }
}

From source file:com.medsavant.mailer.Mail.java

public synchronized static boolean sendEmail(String to, String subject, String text, File attachment) {
    try {//from w  w w  . j a  v  a  2s  . c  o m

        if (src == null || pw == null || host == null || port == -1) {
            return false;
        }

        if (to.isEmpty()) {
            return false;
        }

        LOG.info("Sending email to " + to + " with subject " + subject);

        // create some properties and get the default Session
        Properties props = new Properties();
        props.put("mail.smtp.user", src);
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.starttls.enable", starttls);
        props.put("mail.smtp.auth", auth);
        props.put("mail.smtp.socketFactory.port", port);
        props.put("mail.smtp.socketFactory.class", socketFactoryClass);
        props.put("mail.smtp.socketFactory.fallback", fallback);
        Session session = Session.getInstance(props, null);
        // create a message
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(src, srcName));
        InternetAddress[] address = InternetAddress.parse(to);
        msg.setRecipients(Message.RecipientType.BCC, address);
        msg.setSubject(subject);
        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();

        mbp1.setContent(text, "text/html");

        // create the Multipart and add its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);

        if (attachment != null) {
            // create the second message part
            MimeBodyPart mbp2 = new MimeBodyPart();
            // attach the file to the message
            FileDataSource fds = new FileDataSource(attachment);
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());
            mp.addBodyPart(mbp2);
        }

        // add the Multipart to the message
        msg.setContent(mp);
        // set the Date: header
        msg.setSentDate(new Date());
        // send the message
        Transport transport = session.getTransport("smtp");
        transport.connect(host, src, pw);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();

        LOG.info("Mail sent");

        return true;

    } catch (Exception ex) {
        ex.printStackTrace();
        LOG.error(ex);
        return false;
    }

}

From source file:org.fireflow.service.email.send.MailSenderImpl.java

public void sendEMail(MailMessage mailMessage) throws ServiceInvocationException {

    //1?Session//from   www  .  j a v  a 2 s  .c  o m
    Properties javaMailProperties = new Properties();

    javaMailProperties.put("mail.transport.protocol", mailServiceDef.getProtocol());
    javaMailProperties.put("mail.smtp.host", mailServiceDef.getSmtpServer());
    javaMailProperties.put("mail.smtp.auth", mailServiceDef.isNeedAuth() ? "true" : "false");
    if (mailServiceDef.isUseSSL()) {
        javaMailProperties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        javaMailProperties.setProperty("mail.smtp.socketFactory.fallback", "false");
        javaMailProperties.setProperty("mail.smtp.socketFactory.port",
                Integer.toString(mailServiceDef.getSmtpPort()));
    }

    Session mailSession = Session.getInstance(javaMailProperties);

    //2?MimeMessage
    MimeMessage mimeMsg = null;
    try {
        mimeMsg = createMimeMessage(mailSession, mailMessage);

        //3???
        Transport transport = mailSession.getTransport();
        transport.connect(mailServiceDef.getUserName(), mailServiceDef.getPassword());

        transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
    } catch (AddressException e) {
        throw new ServiceInvocationException(e);
    } catch (MessagingException e) {
        throw new ServiceInvocationException(e);
    }
}

From source file:easyproject.service.Mail.java

public void sendMail() {
    Properties props = new Properties();

    props.put("mail.debug", "true");
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", servidorSMTP);
    props.put("mail.smtp.port", puerto);

    Session session = Session.getInstance(props, null);

    try {//  www. jav a  2s  .  c o m
        MimeMessage message = new MimeMessage(session);

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(destino));
        message.setSubject(asunto);
        message.setSentDate(new Date());
        message.setContent(mensaje, "text/html; charset=utf-8");

        Transport tr = session.getTransport("smtp");
        tr.connect(servidorSMTP, usuario, password);
        message.saveChanges();
        tr.sendMessage(message, message.getAllRecipients());
        tr.close();

    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

From source file:org.opencastproject.kernel.mail.SmtpService.java

/**
 * Sends <code>message</code> using the configured transport.
 * /*from w  w w  . j  ava2s .  com*/
 * @param message
 *          the message
 * @throws MessagingException
 *           if sending the message failed
 */
public void send(MimeMessage message) throws MessagingException {
    Transport t = getSession().getTransport(mailTransport);
    try {
        if (mailUser != null)
            t.connect(mailUser, mailPassword);
        else
            t.connect();
        t.sendMessage(message, message.getAllRecipients());
    } finally {
        t.close();
    }
}