Search in sources :

Example 1 with MessagingException

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);
            }
        }
    }
}
Also used : MimeMessage(jakarta.mail.internet.MimeMessage) MessagingException(jakarta.mail.MessagingException) Transport(jakarta.mail.Transport) Properties(java.util.Properties) MessagingException(jakarta.mail.MessagingException)

Example 2 with MessagingException

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);
    }
}
Also used : MailPreparationException(org.springframework.mail.MailPreparationException) MimeMessage(jakarta.mail.internet.MimeMessage) MessagingException(jakarta.mail.MessagingException) ArrayList(java.util.ArrayList) MailParseException(org.springframework.mail.MailParseException) MailException(org.springframework.mail.MailException) MailParseException(org.springframework.mail.MailParseException) NoSuchProviderException(jakarta.mail.NoSuchProviderException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MessagingException(jakarta.mail.MessagingException) MailSendException(org.springframework.mail.MailSendException) MailPreparationException(org.springframework.mail.MailPreparationException) MailException(org.springframework.mail.MailException) AuthenticationFailedException(jakarta.mail.AuthenticationFailedException)

Example 3 with MessagingException

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");
    }
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) MailSendException(org.springframework.mail.MailSendException) MimeMessage(jakarta.mail.internet.MimeMessage) MessagingException(jakarta.mail.MessagingException) Test(org.junit.jupiter.api.Test)

Example 4 with MessagingException

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");
    }
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailSendException(org.springframework.mail.MailSendException) MessagingException(jakarta.mail.MessagingException) Test(org.junit.jupiter.api.Test)

Example 5 with MessagingException

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();
}
Also used : MessagingException(jakarta.mail.MessagingException) Health(org.springframework.boot.actuate.health.Health) Test(org.junit.jupiter.api.Test)

Aggregations

MessagingException (jakarta.mail.MessagingException)5 MimeMessage (jakarta.mail.internet.MimeMessage)3 Test (org.junit.jupiter.api.Test)3 MailSendException (org.springframework.mail.MailSendException)3 InternetAddress (jakarta.mail.internet.InternetAddress)2 AuthenticationFailedException (jakarta.mail.AuthenticationFailedException)1 NoSuchProviderException (jakarta.mail.NoSuchProviderException)1 Transport (jakarta.mail.Transport)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Health (org.springframework.boot.actuate.health.Health)1 MailAuthenticationException (org.springframework.mail.MailAuthenticationException)1 MailException (org.springframework.mail.MailException)1 MailParseException (org.springframework.mail.MailParseException)1 MailPreparationException (org.springframework.mail.MailPreparationException)1 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)1