use of jakarta.mail.MessagingException 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.MessagingException 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.MessagingException in project spring-framework by spring-projects.
the class JavaMailSenderTests method failedMimeMessage.
@Test
public void failedMimeMessage() 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"));
mimeMessage1.setSubject("fail");
MimeMessage mimeMessage2 = sender.createMimeMessage();
mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress("she@mail.org"));
try {
sender.send(mimeMessage1, mimeMessage2);
} catch (MailSendException ex) {
ex.printStackTrace();
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(mimeMessage2);
assertThat(ex.getFailedMessages().size()).isEqualTo(1);
assertThat(ex.getFailedMessages().keySet().iterator().next()).isEqualTo(mimeMessage1);
Object subEx = ex.getFailedMessages().values().iterator().next();
boolean condition = subEx instanceof MessagingException;
assertThat(condition).isTrue();
assertThat(((MessagingException) subEx).getMessage()).isEqualTo("failed");
}
}
use of jakarta.mail.MessagingException in project spring-framework by spring-projects.
the class JavaMailSenderTests method failedSimpleMessage.
@Test
public void failedSimpleMessage() throws MessagingException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setUsername("username");
sender.setPassword("password");
SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
simpleMessage1.setTo("he@mail.org");
simpleMessage1.setSubject("fail");
SimpleMailMessage simpleMessage2 = new SimpleMailMessage();
simpleMessage2.setTo("she@mail.org");
try {
sender.send(simpleMessage1, simpleMessage2);
} catch (MailSendException ex) {
ex.printStackTrace();
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).getAllRecipients()[0]).isEqualTo(new InternetAddress("she@mail.org"));
assertThat(ex.getFailedMessages().size()).isEqualTo(1);
assertThat(ex.getFailedMessages().keySet().iterator().next()).isEqualTo(simpleMessage1);
Object subEx = ex.getFailedMessages().values().iterator().next();
boolean condition = subEx instanceof MessagingException;
assertThat(condition).isTrue();
assertThat(((MessagingException) subEx).getMessage()).isEqualTo("failed");
}
}
use of jakarta.mail.MessagingException in project spring-boot by spring-projects.
the class MailHealthIndicatorTests method smtpIsDown.
@Test
void smtpIsDown() throws MessagingException {
willThrow(new MessagingException("A test exception")).given(this.mailSender).testConnection();
Health health = this.indicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails().get("location")).isEqualTo("smtp.acme.org:25");
Object errorMessage = health.getDetails().get("error");
assertThat(errorMessage).isNotNull();
assertThat(errorMessage.toString().contains("A test exception")).isTrue();
}
Aggregations