Search in sources :

Example 56 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project spring-framework by spring-projects.

the class JavaMailSenderTests method javaMailSenderWithParseExceptionOnSimpleMessage.

@Test
public void javaMailSenderWithParseExceptionOnSimpleMessage() {
    MockJavaMailSender sender = new MockJavaMailSender();
    SimpleMailMessage simpleMessage = new SimpleMailMessage();
    simpleMessage.setFrom("");
    try {
        sender.send(simpleMessage);
    } catch (MailParseException ex) {
        // expected
        boolean condition = ex.getCause() instanceof AddressException;
        assertThat(condition).isTrue();
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) AddressException(jakarta.mail.internet.AddressException) MailParseException(org.springframework.mail.MailParseException) Test(org.junit.jupiter.api.Test)

Example 57 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project paascloud-master by paascloud.

the class OptSendMailServiceImpl method sendSimpleMail.

@Override
public int sendSimpleMail(String subject, String text, Set<String> to) {
    log.info("sendSimpleMail - 发送简单邮件. subject={}, text={}, to={}", subject, text, to);
    int result = 1;
    try {
        SimpleMailMessage message = MailEntity.createSimpleMailMessage(subject, text, to);
        message.setFrom(from);
        taskExecutor.execute(() -> mailSender.send(message));
    } catch (Exception e) {
        log.info("sendSimpleMail [FAIL] ex={}", e.getMessage(), e);
        result = 0;
    }
    return result;
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) TemplateException(freemarker.template.TemplateException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) OpcBizException(com.paascloud.provider.exceptions.OpcBizException)

Example 58 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project uhgroupings by uhawaii-system-its-ti-iam.

the class EmailServiceTest method sendFeedbackWithMailExceptionThrown.

@Test
public void sendFeedbackWithMailExceptionThrown() {
    JavaMailSender senderWithException = new MockJavaMailSender() {

        @Override
        public void send(SimpleMailMessage mailMessage) throws MailException {
            wasSent = false;
            throw new MailSendException("Exception");
        }
    };
    EmailService emailServiceWithException = new EmailService(senderWithException);
    emailServiceWithException.setEnabled(true);
    Feedback feedback = createBaseFeedback();
    emailServiceWithException.send(feedback);
    assertThat(wasSent, is(false));
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailSendException(org.springframework.mail.MailSendException) Feedback(edu.hawaii.its.groupings.type.Feedback) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 59 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project uhgroupings by uhawaii-system-its-ti-iam.

the class EmailServiceTest method setUp.

@Before
public void setUp() {
    JavaMailSender sender = new MockJavaMailSender() {

        @Override
        public void send(SimpleMailMessage mailMessage) throws MailException {
            wasSent = true;
            messageSent = mailMessage;
        }
    };
    emailService = new EmailService(sender);
    emailService.setEnabled(true);
    wasSent = false;
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) Before(org.junit.Before)

Example 60 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project uhgroupings by uhawaii-system-its-ti-iam.

the class EmailService method send.

public void send(Feedback feedback) {
    logger.info("Feedback received in EmailService: " + feedback);
    if (isEnabled) {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo(to);
        msg.setFrom(from);
        String text = "";
        String header = "UH Groupings service feedback [" + feedback.getType() + "]";
        text += "Submitted name: " + feedback.getName() + "\n\n";
        text += "Submitted email: <" + feedback.getEmail() + ">\n\n";
        text += "Feedback type: " + feedback.getType() + "\n\n";
        text += "--------------------------" + "\n\n";
        text += "Feedback: " + feedback.getMessage() + "\n\n";
        if (!feedback.getExceptionMessage().isEmpty()) {
            text += "Stack Trace: " + feedback.getExceptionMessage();
        }
        msg.setText(text);
        msg.setSubject(header);
        try {
            javaMailSender.send(msg);
        } catch (MailException ex) {
            logger.error("Error", ex);
        }
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException)

Aggregations

SimpleMailMessage (org.springframework.mail.SimpleMailMessage)106 MailException (org.springframework.mail.MailException)17 Test (org.junit.Test)13 Test (org.testng.annotations.Test)7 Test (org.junit.jupiter.api.Test)6 User (org.molgenis.data.security.auth.User)5 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)5 IOException (java.io.IOException)4 MessagingException (javax.mail.MessagingException)4 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 MailSendException (org.springframework.mail.MailSendException)4 InternetAddress (jakarta.mail.internet.InternetAddress)3 MimeMessage (jakarta.mail.internet.MimeMessage)3 Date (java.util.Date)3 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)3 MolgenisUserException (org.molgenis.security.user.MolgenisUserException)3 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 User (fr.codechill.spring.model.User)2