Search in sources :

Example 46 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project cia by Hack23.

the class EmailServiceImpl method getMailSender.

/**
 * Gets the mail sender.
 *
 * @return the mail sender
 */
private JavaMailSender getMailSender() {
    final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    final ApplicationConfiguration smtpHostConfig = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_HOST, SMTP_HOST, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_HOST, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_HOST, "localhost");
    final ApplicationConfiguration smtpPort = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_PORT, SMTP_PORT, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_PORT, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_PORT, "587");
    final ApplicationConfiguration smtpUsername = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_USERNAME, SMTP_USERNAME, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_USERNAME, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_USERNAME, "username");
    final ApplicationConfiguration smtpPassword = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_SECRET, SMTP_SECRET, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_SECRET, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_SECRET, "password");
    final ApplicationConfiguration smtpAuth = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_AUTH, SMTP_AUTH, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_AUTH, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_AUTH, "true");
    final ApplicationConfiguration smtpStartTlsEnable = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE, SMTP_STARTTLS_ENABLE, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_STARTTLS_ENABLE, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE, "true");
    javaMailSender.setHost(smtpHostConfig.getPropertyValue());
    javaMailSender.setPort(Integer.parseInt(smtpPort.getPropertyValue()));
    javaMailSender.setUsername(smtpUsername.getPropertyValue());
    javaMailSender.setPassword(smtpPassword.getPropertyValue());
    final Properties javaMailProperties = new Properties();
    javaMailProperties.setProperty(MAIL_SMTP_AUTH, smtpAuth.getPropertyValue());
    javaMailProperties.setProperty(MAIL_SMTP_STARTTLS_ENABLE, smtpStartTlsEnable.getPropertyValue());
    javaMailSender.setJavaMailProperties(javaMailProperties);
    return javaMailSender;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties) ApplicationConfiguration(com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration)

Example 47 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project JavaForFun by gumartinm.

the class EmailServiceTest method setUp.

@Before
public void setUp() throws Exception {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    senderImpl.setPort(3025);
    senderImpl.setHost("localhost");
    emailService = new EmailServiceImpl(senderImpl);
    testSmtp = new GreenMail(ServerSetupTest.SMTP);
    testSmtp.start();
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) EmailServiceImpl(de.spring.emails.services.impl.EmailServiceImpl) GreenMail(com.icegreen.greenmail.util.GreenMail) Before(org.junit.Before)

Example 48 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project pmph by BCSquad.

the class JavaMailSenderUtil method sendMail.

/**
 * <pre>
 * 功能描述:发送邮件
 * 使用示范:
 *
 * @param title 邮件标题
 * @param content 邮件标题内容
 * @param toMail 收件人
 * @return true/false
 * @throws Exception
 * </pre>
 */
public boolean sendMail(String title, String content, String[] toMail) throws Exception {
    Boolean isOk = false;
    // 创建邮件发送类
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    // 设置邮件
    MimeMessage mailMessage = null;
    try {
        mailMessage = JavaMailSenderConfig(senderImpl);
        // 设置邮件
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");
        // 收件人
        messageHelper.setTo(toMail);
        // 发件人
        messageHelper.setFrom(PropsUtil.getString(properties, "mail.smtp.username"));
        // 标题
        messageHelper.setSubject(title);
        // 内容,true 表示启动HTML格式的邮件
        messageHelper.setText(content, true);
        // 发送邮件
        senderImpl.send(mailMessage);
        isOk = true;
        logger.info("邮件发送成功...");
    } catch (MessagingException e) {
        logger.error("邮件发送时发生异常:{}", e.getMessage());
        logger.info("邮件进行重发");
        try {
            Thread.sleep(2000);
            senderImpl.send(mailMessage);
            isOk = true;
        } catch (InterruptedException ie) {
            logger.error("重发邮件时发生异常:{}", ie.getMessage());
        }
    }
    return isOk;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 49 with JavaMailSenderImpl

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

the class AdminSpringBootTestConfiguration method blMailSender.

/**
 * A dummy mail sender has been set to send emails for testing purposes only
 * To view the emails sent use "DevNull SMTP" (download separately) with the following setting:
 *   Port: 30000
 */
@Bean
public MailSender blMailSender() {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost("localhost");
    sender.setPort(30000);
    sender.setProtocol("smtp");
    Properties javaMailProps = new Properties();
    javaMailProps.setProperty("mail.smtp.starttls.enable", "true");
    javaMailProps.setProperty("mail.smtp.timeout", "25000");
    sender.setJavaMailProperties(javaMailProps);
    return sender;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties) MapFactoryBean(org.springframework.beans.factory.config.MapFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 50 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project cloudbreak by hortonworks.

the class EmailSenderHostServiceTypeTest method before.

@Before
public void before() throws IOException, TemplateException {
    greenMail = new GreenMail(new ServerSetup(3465, null, ServerSetup.PROTOCOL_SMTP));
    greenMail.setUser("demouser", "demopwd");
    greenMail.start();
    IdentityUser identityUser = new IdentityUser("sdf", "testuser", "testaccount", new ArrayList<>(), "familyname", "givenName", new Date());
    ReflectionTestUtils.setField(emailSenderService, "freemarkerConfiguration", freemarkerConfiguration());
    ReflectionTestUtils.setField(emailSenderService, "successClusterMailTemplatePath", "templates/cluster-installer-mail-success.ftl");
    ReflectionTestUtils.setField(emailSenderService, "failedClusterMailTemplatePath", "templates/cluster-installer-mail-fail.ftl");
    JavaMailSender mailSender = new JavaMailSenderImpl();
    ((JavaMailSenderImpl) mailSender).setHost("localhost");
    ((JavaMailSenderImpl) mailSender).setPort(3465);
    ((JavaMailSenderImpl) mailSender).setUsername("demouser2");
    ((JavaMailSenderImpl) mailSender).setPassword("demopwd2");
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.auth", false);
    props.put("mail.smtp.starttls.enable", false);
    props.put("mail.debug", false);
    ((JavaMailSenderImpl) mailSender).setJavaMailProperties(props);
    ReflectionTestUtils.setField(emailSenderService, "mailSender", mailSender);
    EmailMimeMessagePreparator mmp = new EmailMimeMessagePreparator();
    ReflectionTestUtils.setField(mmp, "msgFrom", "no-reply@hortonworks.com");
    ReflectionTestUtils.setField(emailSenderService, "emailMimeMessagePreparator", mmp);
    when(userDetailsService.getDetails(anyString(), any(UserFilterField.class))).thenReturn(identityUser);
}
Also used : IdentityUser(com.sequenceiq.cloudbreak.common.model.user.IdentityUser) JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) UserFilterField(com.sequenceiq.cloudbreak.common.service.user.UserFilterField) GreenMail(com.icegreen.greenmail.util.GreenMail) ServerSetup(com.icegreen.greenmail.util.ServerSetup) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) Properties(java.util.Properties) Date(java.util.Date) Before(org.junit.Before)

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