Search in sources :

Example 56 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project spring-boot by spring-projects.

the class MailSenderAutoConfigurationTests method defaultEncodingWithJndi.

@Test
void defaultEncodingWithJndi() {
    configureJndiSession("foo");
    this.contextRunner.withPropertyValues("spring.mail.jndi-name:foo", "spring.mail.default-encoding:UTF-16").run((context) -> {
        assertThat(context).hasSingleBean(JavaMailSenderImpl.class);
        JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class);
        assertThat(mailSender.getDefaultEncoding()).isEqualTo("UTF-16");
    });
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Test(org.junit.jupiter.api.Test)

Example 57 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project spring-boot by spring-projects.

the class MailSenderJndiConfiguration method mailSender.

@Bean
JavaMailSenderImpl mailSender(Session session) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setDefaultEncoding(this.properties.getDefaultEncoding().name());
    sender.setSession(session);
    return sender;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 58 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project spring-boot by spring-projects.

the class MailSenderPropertiesConfiguration method mailSender.

@Bean
@ConditionalOnMissingBean(JavaMailSender.class)
JavaMailSenderImpl mailSender(MailProperties properties) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    applyProperties(properties, sender);
    return sender;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 59 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project OpenClinica by OpenClinica.

the class SecureController method sendEmail.

public Boolean sendEmail(String to, String from, String subject, String body, Boolean htmlEmail, String successMessage, String failMessage, Boolean sendMessage) throws Exception {
    Boolean messageSent = true;
    try {
        JavaMailSenderImpl mailSender = (JavaMailSenderImpl) SpringServletAccess.getApplicationContext(context).getBean("mailSender");
        // @pgawade 09-Feb-2012 #issue 13201 - setting the "mail.smtp.localhost" property to localhost when java API is not able to
        // retrieve the host name
        Properties javaMailProperties = mailSender.getJavaMailProperties();
        if (null != javaMailProperties) {
            if (javaMailProperties.get("mail.smtp.localhost") == null || ((String) javaMailProperties.get("mail.smtp.localhost")).equalsIgnoreCase("")) {
                javaMailProperties.put("mail.smtp.localhost", "localhost");
            }
        }
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, htmlEmail);
        helper.setFrom(from);
        helper.setTo(processMultipleImailAddresses(to.trim()));
        helper.setSubject(subject);
        helper.setText(body, true);
        mailSender.send(mimeMessage);
        if (successMessage != null && sendMessage) {
            addPageMessage(successMessage);
        }
        logger.debug("Email sent successfully on {}", new Date());
    } catch (MailException me) {
        me.printStackTrace();
        if (failMessage != null && sendMessage) {
            addPageMessage(failMessage);
        }
        logger.debug("Email could not be sent on {} due to: {}", new Date(), me.toString());
        messageSent = false;
    }
    return messageSent;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailException(org.springframework.mail.MailException) Properties(java.util.Properties) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date)

Example 60 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project waltz by khartec.

the class DIConfiguration method mailSender.

@Bean
public JavaMailSender mailSender() {
    if (smtpHost == null) {
        return new DummyJavaMailSender();
    } else {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(smtpHost);
        mailSender.setPort(smtpPort);
        return mailSender;
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) DummyJavaMailSender(org.finos.waltz.service.email.DummyJavaMailSender)

Aggregations

JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)62 Properties (java.util.Properties)25 Bean (org.springframework.context.annotation.Bean)16 MimeMessage (javax.mail.internet.MimeMessage)11 MessagingException (javax.mail.MessagingException)10 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)10 Test (org.junit.jupiter.api.Test)8 IOException (java.io.IOException)4 Date (java.util.Date)4 ByteArrayResource (org.springframework.core.io.ByteArrayResource)4 Test (org.junit.Test)3 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)3 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)3 GreenMail (com.icegreen.greenmail.util.GreenMail)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2