use of jakarta.mail.internet.MimeMessage in project gocd by gocd.
the class GoSmtpMailSender method send.
@Override
public ValidationBean send(String subject, String body, String to) {
Transport transport = null;
try {
LOGGER.debug("Sending email [{}] to [{}]", subject, to);
Properties props = mailProperties();
MailSession session = MailSession.getInstance().createWith(props, mailHost.getUsername(), mailHost.getPassword());
transport = session.getTransport();
transport.connect(mailHost.getHostName(), mailHost.getPort(), StringUtils.trimToNull(mailHost.getUsername()), StringUtils.trimToNull(mailHost.getPassword()));
MimeMessage msg = session.createMessage(mailHost.getFrom(), to, subject, body);
transport.sendMessage(msg, msg.getRecipients(TO));
return ValidationBean.valid();
} catch (Exception e) {
LOGGER.error("Sending failed for email [{}] to [{}]", subject, to, e);
return ValidationBean.notValid(ERROR_MESSAGE);
} finally {
if (transport != null) {
try {
transport.close();
} catch (MessagingException e) {
LOGGER.error("Failed to close transport", e);
}
}
}
}
use of jakarta.mail.internet.MimeMessage in project spring-framework by spring-projects.
the class JavaMailSenderImpl method send.
@Override
public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
try {
List<MimeMessage> mimeMessages = new ArrayList<>(mimeMessagePreparators.length);
for (MimeMessagePreparator preparator : mimeMessagePreparators) {
MimeMessage mimeMessage = createMimeMessage();
preparator.prepare(mimeMessage);
mimeMessages.add(mimeMessage);
}
send(mimeMessages.toArray(new MimeMessage[0]));
} catch (MailException ex) {
throw ex;
} catch (MessagingException ex) {
throw new MailParseException(ex);
} catch (Exception ex) {
throw new MailPreparationException(ex);
}
}
use of jakarta.mail.internet.MimeMessage in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithSimpleMessages.
@Test
public void javaMailSenderWithSimpleMessages() throws MessagingException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setUsername("username");
sender.setPassword("password");
SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
simpleMessage1.setTo("he@mail.org");
SimpleMailMessage simpleMessage2 = new SimpleMailMessage();
simpleMessage2.setTo("she@mail.org");
sender.send(simpleMessage1, simpleMessage2);
assertThat(sender.transport.getConnectedHost()).isEqualTo("host");
assertThat(sender.transport.getConnectedUsername()).isEqualTo("username");
assertThat(sender.transport.getConnectedPassword()).isEqualTo("password");
assertThat(sender.transport.isCloseCalled()).isTrue();
assertThat(sender.transport.getSentMessages().size()).isEqualTo(2);
MimeMessage sentMessage1 = sender.transport.getSentMessage(0);
List<Address> tos1 = Arrays.asList(sentMessage1.getRecipients(Message.RecipientType.TO));
assertThat(tos1.size()).isEqualTo(1);
assertThat(((InternetAddress) tos1.get(0)).getAddress()).isEqualTo("he@mail.org");
MimeMessage sentMessage2 = sender.transport.getSentMessage(1);
List<Address> tos2 = Arrays.asList(sentMessage2.getRecipients(Message.RecipientType.TO));
assertThat(tos2.size()).isEqualTo(1);
assertThat(((InternetAddress) tos2.get(0)).getAddress()).isEqualTo("she@mail.org");
}
use of jakarta.mail.internet.MimeMessage in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithMimeMessages.
@Test
public void javaMailSenderWithMimeMessages() throws MessagingException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setUsername("username");
sender.setPassword("password");
MimeMessage mimeMessage1 = sender.createMimeMessage();
mimeMessage1.setRecipient(Message.RecipientType.TO, new InternetAddress("he@mail.org"));
MimeMessage mimeMessage2 = sender.createMimeMessage();
mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress("she@mail.org"));
sender.send(mimeMessage1, mimeMessage2);
assertThat(sender.transport.getConnectedHost()).isEqualTo("host");
assertThat(sender.transport.getConnectedUsername()).isEqualTo("username");
assertThat(sender.transport.getConnectedPassword()).isEqualTo("password");
assertThat(sender.transport.isCloseCalled()).isTrue();
assertThat(sender.transport.getSentMessages().size()).isEqualTo(2);
assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage1);
assertThat(sender.transport.getSentMessage(1)).isEqualTo(mimeMessage2);
}
use of jakarta.mail.internet.MimeMessage in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithMimeMessage.
@Test
public void javaMailSenderWithMimeMessage() throws MessagingException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setUsername("username");
sender.setPassword("password");
MimeMessage mimeMessage = sender.createMimeMessage();
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org"));
sender.send(mimeMessage);
assertThat(sender.transport.getConnectedHost()).isEqualTo("host");
assertThat(sender.transport.getConnectedUsername()).isEqualTo("username");
assertThat(sender.transport.getConnectedPassword()).isEqualTo("password");
assertThat(sender.transport.isCloseCalled()).isTrue();
assertThat(sender.transport.getSentMessages().size()).isEqualTo(1);
assertThat(sender.transport.getSentMessage(0)).isEqualTo(mimeMessage);
}
Aggregations