Java Code Examples for org.springframework.mail.SimpleMailMessage#setReplyTo()

The following examples show how to use org.springframework.mail.SimpleMailMessage#setReplyTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringMailSenderBackend.java    From haven-platform with Apache License 2.0 7 votes vote down vote up
@Override
public void send(MailMessage message) throws MailSenderException {
    SimpleMailMessage smm = new SimpleMailMessage();
    MailBody body = message.getBody();
    smm.setText(MailUtils.toPlainText(body));
    MailHead head = message.getHead();
    smm.setFrom(head.getFrom());
    smm.setReplyTo(head.getReplyTo());
    smm.setSubject(head.getSubject());
    smm.setTo(asArray(head.getTo()));
    smm.setCc(asArray(head.getCc()));
    smm.setBcc(asArray(head.getBcc()));
    smm.setSentDate(head.getSentDate());
    LOG.info("message to send {}", smm);
    mailSender.send(smm);
}
 
Example 2
Source File: MailServiceImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * @throws MailException
 *             general super class for all mail-exception when send failed
 * @throws MailParseException
 *             in case of failure when parsing the message
 * @throws MailAuthenticationException
 *             in case of authentication failure
 * @throws MailSendException
 *             in case of failure when sending the message
 */
@Override
public void sendSimpleMail(SimpleMailTO mailParameters) throws MailException {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setTo(mailParameters.getToMailAddress());
    msg.setFrom(mailParameters.getFromMailAddress());
    msg.setSubject(mailParameters.getSubject());
    msg.setText(mailParameters.getBodyText());
    if (mailParameters.hasCcMailAddress()) {
        msg.setCc(mailParameters.getCcMailAddress());
    }
    if (mailParameters.hasReplyTo()) {
        msg.setReplyTo(mailParameters.getReplyTo());
    }
    mailSender.send(msg);
}
 
Example 3
Source File: FeedbackController.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Creates a MimeMessage based on a FeedbackForm. */
@SuppressWarnings("java:S3457") // do not use platform specific line ending
private SimpleMailMessage createFeedbackMessage(FeedbackForm form) {
  SimpleMailMessage message = new SimpleMailMessage();
  message.setTo(userService.getSuEmailAddresses().toArray(new String[] {}));
  if (form.hasEmail()) {
    message.setCc(form.getEmail());
    message.setReplyTo(form.getEmail());
  } else {
    message.setReplyTo("[email protected]");
  }
  String appName = appSettings.getTitle();
  message.setSubject(String.format("[feedback-%s] %s", appName, form.getSubject()));
  message.setText(String.format("Feedback from %s:\n\n%s", form.getFrom(), form.getFeedback()));
  return message;
}
 
Example 4
Source File: MailMessageNotifier.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Send mail messages.
 *
 * @param simpleMessages
 */
@Override
public void send(GenericNotifyMessage msg) {
    String mailMsgType = msg.getParameterAsString(KEY_MAILMSG_TYPE, "simple");
    switch (mailMsgType) {
        case KEY_MAILMSG_VALUE_SIMPLE:
            SimpleMailMessage simpleMsg = new SimpleMailMessage();
            // Add "<>" symbol to send out?
            /*
             * Preset from account, otherwise it would be wrong: 501 mail from
             * address must be same as authorization user.
             */
            simpleMsg.setFrom(config.getUsername() + "<" + config.getUsername() + ">");
            simpleMsg.setTo(msg.getToObjects().stream().map(to -> to = to + "<" + to + ">").collect(toList()).toArray(new String[]{}));
            simpleMsg.setSubject(msg.getParameterAsString(KEY_MAILMSG_SUBJECT, "Super Devops Messages"));
            simpleMsg.setSentDate(msg.getParameter(KEY_MSG_SENDDATE, new Date()));
            simpleMsg.setBcc(safeList(msg.getParameter(KEY_MAILMSG_BCC)).toArray(new String[]{}));
            simpleMsg.setCc(safeList(msg.getParameter(KEY_MAILMSG_CC)).toArray(new String[]{}));
            simpleMsg.setReplyTo(msg.getParameter(KEY_MAILMSG_REPLYTO));
            simpleMsg.setText(config.getResolvedMessage(msg.getTemplateKey(), msg.getParameters()));

            mailSender.send(simpleMsg);
            break;
        case KEY_MAILMSG_VALUE_MIME: // TODO implements!!!
            log.warn("No implements MimeMailMessage!!!");
            break;
        default:
            throw new UnsupportedOperationException(format("No supported mail message type of %s", mailMsgType));
    }

}
 
Example 5
Source File: MailServiceImpl.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * @throws MailException
 *             general super class for all mail-exception when send failed
 * @throws MailParseException
 *             in case of failure when parsing the message
 * @throws MailAuthenticationException
 *             in case of authentication failure
 * @throws MailSendException
 *             in case of failure when sending the message
 */
public void sendSimpleMail(SimpleMailTO mailParameters) throws MailException {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setTo(mailParameters.getToMailAddress());
    msg.setFrom(mailParameters.getFromMailAddress());
    msg.setSubject(mailParameters.getSubject());
    msg.setText(mailParameters.getBodyText());
    if (mailParameters.hasCcMailAddress()) {
        msg.setCc(mailParameters.getCcMailAddress());
    }
    if (mailParameters.hasReplyTo()) {
        msg.setReplyTo(mailParameters.getReplyTo());
    }
    mailSender.send(msg);
}
 
Example 6
Source File: FeedbackControllerTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void submit() throws Exception {
  List<String> adminEmails = Collections.singletonList("[email protected]");
  when(userService.getSuEmailAddresses()).thenReturn(adminEmails);
  when(appSettings.getRecaptchaIsEnabled()).thenReturn(true);
  when(reCaptchaService.validate("validCaptcha")).thenReturn(true);
  mockMvcFeedback
      .perform(
          MockMvcRequestBuilders.post(FeedbackController.URI)
              .param("name", "First Last")
              .param("subject", "Feedback form")
              .param("email", "[email protected]")
              .param("feedback", "Feedback.\nLine two.")
              .param("recaptcha", "validCaptcha"))
      .andExpect(status().isOk())
      .andExpect(view().name("view-feedback"))
      .andExpect(model().attribute("feedbackForm", hasProperty("submitted", equalTo(true))));

  SimpleMailMessage expected = new SimpleMailMessage();
  expected.setTo("[email protected]");
  expected.setCc("[email protected]");
  expected.setReplyTo("[email protected]");
  expected.setSubject("[feedback-app123] Feedback form");
  expected.setText("Feedback from First Last ([email protected]):\n\n" + "Feedback.\nLine two.");
  verify(mailSender, times(1)).send(expected);
  verify(reCaptchaService, times(1)).validate("validCaptcha");
}
 
Example 7
Source File: FeedbackControllerTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void submitAuthenticationErrorWhileSendingMail() throws Exception {
  List<String> adminEmails = Collections.singletonList("[email protected]");
  when(userService.getSuEmailAddresses()).thenReturn(adminEmails);
  when(appSettings.getRecaptchaIsEnabled()).thenReturn(true);
  when(reCaptchaService.validate("validCaptcha")).thenReturn(true);
  SimpleMailMessage expected = new SimpleMailMessage();
  expected.setTo("[email protected]");
  expected.setCc("[email protected]");
  expected.setReplyTo("[email protected]");
  expected.setSubject("[feedback-app123] Feedback form");
  expected.setText("Feedback from First Last ([email protected]):\n\n" + "Feedback.\nLine two.");
  doThrow(new MailAuthenticationException("ERRORRR!")).when(mailSender).send(expected);
  mockMvcFeedback
      .perform(
          MockMvcRequestBuilders.post(FeedbackController.URI)
              .param("name", "First Last")
              .param("subject", "Feedback form")
              .param("email", "[email protected]")
              .param("feedback", "Feedback.\nLine two.")
              .param("recaptcha", "validCaptcha"))
      .andExpect(status().isOk())
      .andExpect(view().name("view-feedback"))
      .andExpect(model().attribute("feedbackForm", hasProperty("submitted", equalTo(false))))
      .andExpect(
          model()
              .attribute(
                  "feedbackForm",
                  hasProperty(
                      "errorMessage",
                      equalTo(
                          "Unfortunately, we were unable to send the mail containing "
                              + "your feedback. Please contact the administrator."))));
  verify(reCaptchaService, times(1)).validate("validCaptcha");
}
 
Example 8
Source File: FeedbackControllerTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void submitErrorWhileSendingMail() throws Exception {
  List<String> adminEmails = Collections.singletonList("[email protected]");
  when(userService.getSuEmailAddresses()).thenReturn(adminEmails);
  when(appSettings.getRecaptchaIsEnabled()).thenReturn(true);
  when(reCaptchaService.validate("validCaptcha")).thenReturn(true);
  SimpleMailMessage expected = new SimpleMailMessage();
  expected.setTo("[email protected]");
  expected.setCc("[email protected]");
  expected.setReplyTo("[email protected]");
  expected.setSubject("[feedback-app123] Feedback form");
  expected.setText("Feedback from First Last ([email protected]):\n\n" + "Feedback.\nLine two.");
  doThrow(new MailSendException("ERRORRR!")).when(mailSender).send(expected);
  mockMvcFeedback
      .perform(
          MockMvcRequestBuilders.post(FeedbackController.URI)
              .param("name", "First Last")
              .param("subject", "Feedback form")
              .param("email", "[email protected]")
              .param("feedback", "Feedback.\nLine two.")
              .param("recaptcha", "validCaptcha"))
      .andExpect(status().isOk())
      .andExpect(view().name("view-feedback"))
      .andExpect(model().attribute("feedbackForm", hasProperty("submitted", equalTo(false))))
      .andExpect(
          model()
              .attribute(
                  "feedbackForm",
                  hasProperty(
                      "errorMessage",
                      equalTo(
                          "Unfortunately, we were unable to send the mail containing "
                              + "your feedback. Please contact the administrator."))));
  verify(reCaptchaService, times(1)).validate("validCaptcha");
}
 
Example 9
Source File: SpringBootMailIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
private SimpleMailMessage composeEmailMessage() {
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    mailMessage.setTo(userTo);
    mailMessage.setReplyTo(userFrom);
    mailMessage.setFrom(userFrom);
    mailMessage.setSubject(subject);
    mailMessage.setText(textMail);
    return mailMessage;
}
 
Example 10
Source File: JavaMailSenderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
	MockJavaMailSender sender = new MockJavaMailSender();
	sender.setHost("host");
	sender.setPort(30);
	sender.setUsername("username");
	sender.setPassword("password");

	SimpleMailMessage simpleMessage = new SimpleMailMessage();
	simpleMessage.setFrom("[email protected]");
	simpleMessage.setReplyTo("[email protected]");
	simpleMessage.setTo("[email protected]");
	simpleMessage.setCc("[email protected]", "[email protected]");
	simpleMessage.setBcc("[email protected]", "[email protected]");
	Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
	simpleMessage.setSentDate(sentDate);
	simpleMessage.setSubject("my subject");
	simpleMessage.setText("my text");
	sender.send(simpleMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals(30, sender.transport.getConnectedPort());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());

	assertEquals(1, sender.transport.getSentMessages().size());
	MimeMessage sentMessage = sender.transport.getSentMessage(0);
	List<Address> froms = Arrays.asList(sentMessage.getFrom());
	assertEquals(1, froms.size());
	assertEquals("[email protected]", ((InternetAddress) froms.get(0)).getAddress());
	List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
	assertEquals("[email protected]", ((InternetAddress) replyTos.get(0)).getAddress());
	List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
	assertEquals(1, tos.size());
	assertEquals("[email protected]", ((InternetAddress) tos.get(0)).getAddress());
	List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
	assertEquals(2, ccs.size());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(1)).getAddress());
	List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
	assertEquals(2, bccs.size());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(1)).getAddress());
	assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime());
	assertEquals("my subject", sentMessage.getSubject());
	assertEquals("my text", sentMessage.getContent());
}
 
Example 11
Source File: JavaMailSenderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
	MockJavaMailSender sender = new MockJavaMailSender();
	sender.setHost("host");
	sender.setPort(30);
	sender.setUsername("username");
	sender.setPassword("password");

	SimpleMailMessage simpleMessage = new SimpleMailMessage();
	simpleMessage.setFrom("[email protected]");
	simpleMessage.setReplyTo("[email protected]");
	simpleMessage.setTo("[email protected]");
	simpleMessage.setCc("[email protected]", "[email protected]");
	simpleMessage.setBcc("[email protected]", "[email protected]");
	Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
	simpleMessage.setSentDate(sentDate);
	simpleMessage.setSubject("my subject");
	simpleMessage.setText("my text");
	sender.send(simpleMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals(30, sender.transport.getConnectedPort());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());

	assertEquals(1, sender.transport.getSentMessages().size());
	MimeMessage sentMessage = sender.transport.getSentMessage(0);
	List<Address> froms = Arrays.asList(sentMessage.getFrom());
	assertEquals(1, froms.size());
	assertEquals("[email protected]", ((InternetAddress) froms.get(0)).getAddress());
	List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
	assertEquals("[email protected]", ((InternetAddress) replyTos.get(0)).getAddress());
	List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
	assertEquals(1, tos.size());
	assertEquals("[email protected]", ((InternetAddress) tos.get(0)).getAddress());
	List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
	assertEquals(2, ccs.size());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(1)).getAddress());
	List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
	assertEquals(2, bccs.size());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(1)).getAddress());
	assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime());
	assertEquals("my subject", sentMessage.getSubject());
	assertEquals("my text", sentMessage.getContent());
}
 
Example 12
Source File: JavaMailSenderTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
	MockJavaMailSender sender = new MockJavaMailSender();
	sender.setHost("host");
	sender.setPort(30);
	sender.setUsername("username");
	sender.setPassword("password");

	SimpleMailMessage simpleMessage = new SimpleMailMessage();
	simpleMessage.setFrom("[email protected]");
	simpleMessage.setReplyTo("[email protected]");
	simpleMessage.setTo("[email protected]");
	simpleMessage.setCc(new String[] {"[email protected]", "[email protected]"});
	simpleMessage.setBcc(new String[] {"[email protected]", "[email protected]"});
	Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
	simpleMessage.setSentDate(sentDate);
	simpleMessage.setSubject("my subject");
	simpleMessage.setText("my text");
	sender.send(simpleMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals(30, sender.transport.getConnectedPort());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());

	assertEquals(1, sender.transport.getSentMessages().size());
	MimeMessage sentMessage = sender.transport.getSentMessage(0);
	List<Address> froms = Arrays.asList(sentMessage.getFrom());
	assertEquals(1, froms.size());
	assertEquals("[email protected]", ((InternetAddress) froms.get(0)).getAddress());
	List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
	assertEquals("[email protected]", ((InternetAddress) replyTos.get(0)).getAddress());
	List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
	assertEquals(1, tos.size());
	assertEquals("[email protected]", ((InternetAddress) tos.get(0)).getAddress());
	List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
	assertEquals(2, ccs.size());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(1)).getAddress());
	List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
	assertEquals(2, bccs.size());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(1)).getAddress());
	assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime());
	assertEquals("my subject", sentMessage.getSubject());
	assertEquals("my text", sentMessage.getContent());
}