Example usage for org.springframework.mail SimpleMailMessage getTo

List of usage examples for org.springframework.mail SimpleMailMessage getTo

Introduction

In this page you can find the example usage for org.springframework.mail SimpleMailMessage getTo.

Prototype

@Nullable
    public String[] getTo() 

Source Link

Usage

From source file:ar.com.zauber.commons.spring.mail.AbstractMailSender.java

/** Creates a text representation for simpleMessages */
public static String toString(final SimpleMailMessage[] simpleMessages) {
    final StringBuilder sb = new StringBuilder();
    sb.append("This window show that the system tried to send an email.\n").append('\n')
            .append("The email wasnt sent. To do change the AppContext.\n");

    for (final SimpleMailMessage message : simpleMessages) {

        sb.append(" --------------8<--------------\n");

        if (message.getFrom() != null) {
            sb.append("From: ").append(message.getFrom());
        }/*w w w .j a v a2  s.  c o  m*/
        if (message.getTo() != null) {
            sb.append("\nTo: ").append(Arrays.asList(message.getTo()));
        }
        if (message.getCc() != null) {
            sb.append("\nCc: ").append(Arrays.asList(message.getCc()));
        }
        if (message.getBcc() != null) {
            sb.append("\nBcc: ").append(Arrays.asList(message.getBcc()));
        }
        if (message.getReplyTo() != null) {
            sb.append("\nReply-To: ").append(message.getReplyTo());
        }
        if (message.getSentDate() != null) {
            sb.append("\nDate: ").append(message.getSentDate());
        }
        if (message.getSubject() != null) {
            sb.append("\nSubject: ").append(message.getSubject());
        }

        sb.append("\n\n").append(message.getText());
    }
    return sb.toString();
}

From source file:com.springsource.greenhouse.account.WelcomeMailTransformerTest.java

@Test
public void welcomeMail() {
    WelcomeMailTransformer transformer = new WelcomeMailTransformer();

    Account account = new Account(1L, "Roy", "Clarkson", "rclarkson@vmware.com", "rclarkson",
            "http://foo.com/bar.jpg", new UriTemplate("http://greenhouse.springsource.org/members/{id}"));
    SimpleMailMessage welcomeMail = (SimpleMailMessage) transformer.welcomeMail(account);

    assertEquals("rclarkson@vmware.com", welcomeMail.getTo()[0]);
    assertEquals("Greenhouse <noreply@springsource.com>", welcomeMail.getFrom());
    assertEquals("Welcome to the Greenhouse!", welcomeMail.getSubject());
    String mailText = welcomeMail.getText();
    assertTrue(mailText.contains(//  www .j  a va 2 s  .  co m
            "View your member profile at:" + NEWLINE + "http://greenhouse.springsource.org/members/rclarkson"));
}

From source file:com.shirokumacafe.archetype.common.utilities.SimpleMailService.java

/**
 * ??./*from   ww w.java 2s .  c o  m*/
 */
public void sendMail(SimpleMailMessage msg) {
    try {
        mailSender.send(msg);
        if (logger.isInfoEnabled()) {
            logger.info("??{}", StringUtils.join(msg.getTo(), ","));
        }
    } catch (Exception e) {
        logger.error("??", e);
    }
}

From source file:cherry.foundation.mail.SimpleMessageStoreTest.java

@Test
public void testGetMessage() {

    SimpleMessageStore store = new SimpleMessageStore();
    long id0 = store.createMessage("launcherId", "messageName", LocalDateTime.now(), "from@addr",
            asList("to@addr"), asList("cc@addr"), asList("bcc@addr"), "subject", "body");
    assertEquals(0L, id0);//  w  w w .  j  a  v  a 2 s  . c  o m
    long id1 = store.createMessage("launcherId", "messageName", LocalDateTime.now().plusMinutes(1), "from@addr",
            asList("to@addr"), null, null, "subject", "body");
    assertEquals(1L, id1);

    SimpleMailMessage msg0 = store.getMessage(0L);
    assertNotNull(msg0);
    assertEquals("from@addr", msg0.getFrom());
    assertEquals(1, msg0.getTo().length);
    assertEquals("to@addr", msg0.getTo()[0]);
    assertEquals(1, msg0.getCc().length);
    assertEquals("cc@addr", msg0.getCc()[0]);
    assertEquals(1, msg0.getBcc().length);
    assertEquals("bcc@addr", msg0.getBcc()[0]);
    assertEquals("subject", msg0.getSubject());
    assertEquals("body", msg0.getText());

    assertNull(store.getMessage(10L));
}

From source file:org.jasig.openregistry.test.service.MockMailSender.java

@Override
public void send(SimpleMailMessage msg) throws MailException {
    // Write the message to the console
    System.out.println("MockMailSender: mock email message start === ");
    String to = "";
    for (String addr : msg.getTo()) {
        if (to.length() > 0) {
            to += ", ";
        }/*from   w w w . ja  v a2  s .  c o  m*/
        to += addr;
    }
    System.out.println("To: " + to);
    System.out.println("From: " + msg.getFrom());
    System.out.println("Subject: " + msg.getSubject());
    System.out.println("Contents: " + msg.getText());
    System.out.println("MockMailSender: mock email message end === ");
}

From source file:services.MailServiceTest.java

@Ignore
@Test//from   w w  w  . j a  v  a  2  s.  c  o m
public void sendEmailTest() {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setTo("info@pncomp.com");
    msg.setFrom("ziaziek@poczta.fm");
    msg.setSubject("AAA");
    msg.setText("TEST");
    Assert.assertEquals("info@pncomp.com", msg.getTo()[0]);
    Assert.assertEquals("AAA", msg.getSubject());
    try {
        mailSender.send(msg);
    } catch (MailException ex) {
        fail(ex.getMessage());
    }

}

From source file:ar.com.zauber.commons.spring.mail.JdbcMailSender.java

/** @see MailSender#send(SimpleMailMessage)  */
public final void send(final SimpleMailMessage msg) throws MailException {
    final String sql = "INSERT INTO " + tablename + "(sender, rcpt, subject, body) VALUES(?, ?, ?, ?)";
    final StringBuilder sbTo = new StringBuilder();
    for (final String to : msg.getTo()) {
        sbTo.append(to);//ww w  . j  a  va2 s .  c  o m
        sbTo.append(", ");
    }

    jdbcTemplate.update(sql, new Object[] { msg.getFrom(), sbTo.toString(), msg.getSubject(), msg.getText() });
}

From source file:net.triptech.metahive.service.EmailSenderService.java

/**
 * Send an email message using the configured Spring sender. On success
 * record the sent message in the datastore for reporting purposes
 *
 * @param email the email//w  w  w  .  j  a v a 2  s  .c  om
 * @param attachments the attachments
 * @throws ServiceException the service exception
 */
public final void send(final SimpleMailMessage email, TreeMap<String, Object> attachments)
        throws ServiceException {

    // Check to see whether the required fields are set (to, from, message)
    if (email.getTo() == null) {
        throw new ServiceException("Error sending email: Recipient " + "address required");
    }
    if (StringUtils.isBlank(email.getFrom())) {
        throw new ServiceException("Error sending email: Email requires " + "a from address");
    }
    if (StringUtils.isBlank(email.getText())) {
        throw new ServiceException("Error sending email: No email " + "message specified");
    }
    if (mailSender == null) {
        throw new ServiceException("The JavaMail sender has not " + "been configured");
    }

    // Prepare the email message
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = null;
    boolean htmlMessage = false;
    if (StringUtils.containsIgnoreCase(email.getText(), "<html")) {
        htmlMessage = true;
        try {
            helper = new MimeMessageHelper(message, true, "UTF-8");
        } catch (MessagingException me) {
            throw new ServiceException("Error preparing email for sending: " + me.getMessage());
        }
    } else {
        helper = new MimeMessageHelper(message);
    }

    try {
        helper.setTo(email.getTo());
        helper.setFrom(email.getFrom());
        helper.setSubject(email.getSubject());

        if (email.getCc() != null) {
            helper.setCc(email.getCc());
        }
        if (email.getBcc() != null) {
            helper.setBcc(email.getBcc());
        }

        if (htmlMessage) {
            String plainText = email.getText();
            try {
                ConvertHtmlToText htmlToText = new ConvertHtmlToText();
                plainText = htmlToText.convert(email.getText());
            } catch (Exception e) {
                logger.error("Error converting HTML to plain text: " + e.getMessage());
            }
            helper.setText(plainText, email.getText());
        } else {
            helper.setText(email.getText());
        }

        if (email.getSentDate() != null) {
            helper.setSentDate(email.getSentDate());
        } else {
            helper.setSentDate(Calendar.getInstance().getTime());
        }

    } catch (MessagingException me) {
        throw new ServiceException("Error preparing email for sending: " + me.getMessage());
    }

    // Append any attachments (if an HTML email)
    if (htmlMessage && attachments != null) {
        for (String id : attachments.keySet()) {
            Object reference = attachments.get(id);

            if (reference instanceof File) {
                try {
                    FileSystemResource res = new FileSystemResource((File) reference);
                    helper.addInline(id, res);
                } catch (MessagingException me) {
                    logger.error("Error appending File attachment: " + me.getMessage());
                }
            }
            if (reference instanceof URL) {
                try {
                    UrlResource res = new UrlResource((URL) reference);
                    helper.addInline(id, res);
                } catch (MessagingException me) {
                    logger.error("Error appending URL attachment: " + me.getMessage());
                }
            }
        }
    }

    // Send the email message
    try {
        mailSender.send(message);
    } catch (MailException me) {
        logger.error("Error sending email: " + me.getMessage());
        throw new ServiceException("Error sending email: " + me.getMessage());
    }
}

From source file:org.cloudbyexample.dc.service.si.notification.VelocityEmailSender.java

@Override
public void send(final SimpleMailMessage msg, final String templatePath, final Map<String, Object> vars) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        @Override/*from w w w. java 2  s  .  c  o  m*/
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(msg.getTo());
            message.setFrom(msg.getFrom());
            message.setSubject(msg.getSubject());

            String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templatePath, "UTF-8",
                    vars);

            logger.debug("body={}", body);

            message.setText(body, true);
        }
    };

    mailSender.send(preparator);

    logger.info("Sent e-mail to '{}'.  template='{}'", msg.getTo(), templatePath);
}

From source file:ar.com.zauber.commons.spring.mail.RepositoryMailMessage.java

/** constructor de copia */
public RepositoryMailMessage(final SimpleMailMessage m) {
    setBcc(m.getBcc());//from  w w w.java  2  s  .c om
    setCc(m.getCc());
    setFrom(m.getFrom());
    setReplyTo(m.getReplyTo());
    setSentDate(m.getSentDate());
    setSubject(m.getSubject());
    setText(m.getText());
    setTo(m.getTo());
}