Search in sources :

Example 1 with EmailSetting

use of com.dimple.project.system.domain.EmailSetting in project DimpleBlog by martin-chips.

the class EmailServiceImpl method sendHtmlMail.

@Override
public void sendHtmlMail(String to, String title, String content) {
    EmailSetting emailSetting = configService.selectConfigByConfigKey(ConfigKey.CONFIG_KEY_EMAIL_SETTING, EmailSetting.class);
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setUsername(emailSetting.getUser());
    mailSender.setHost(emailSetting.getHost());
    mailSender.setDefaultEncoding("utf-8");
    mailSender.setPassword(emailSetting.getPassword());
    mailSender.setPort(emailSetting.getPort());
    Properties properties = new Properties();
    properties.put("mail.smtp.host", emailSetting.getHost());
    properties.put("mail.smtp.auth", "true");
    // 只处理SSL的连接,对于非SSL的连接不做处理
    properties.put("mail.smtp.socketFactory.fallback", "false");
    properties.put("mail.smtp.port", emailSetting.getPort());
    properties.put("mail.smtp.socketFactory.port", emailSetting.getPort());
    properties.put("mail.smtp.ssl.enable", true);
    Session session = Session.getInstance(properties);
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    try {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom(emailSetting.getFromEmail());
        mimeMessageHelper.setTo(to);
        if (StringUtils.isNotEmpty(emailSetting.getStationmasterEmail())) {
            mimeMessageHelper.setBcc(emailSetting.getStationmasterEmail());
        }
        mimeMessageHelper.setSubject(title);
        mimeMessageHelper.setText(content, true);
        mailSender.setSession(session);
        mailSender.send(mimeMessage);
    } catch (MessagingException e) {
        log.error(e.getMessage(), e);
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) EmailSetting(com.dimple.project.system.domain.EmailSetting) Properties(java.util.Properties) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Session(javax.mail.Session)

Example 2 with EmailSetting

use of com.dimple.project.system.domain.EmailSetting in project DimpleBlog by martin-chips.

the class SettingController method emailSetting.

@GetMapping("/emailSetting")
@PreAuthorize("@permissionService.hasPermission('system:setting:emailSetting:query')")
public AjaxResult emailSetting() {
    Config config = configService.selectConfigByKey(ConfigKey.CONFIG_KEY_EMAIL_SETTING);
    // convert to site setting
    if (config != null) {
        EmailSetting emailSetting = JSON.parseObject(config.getConfigValue(), EmailSetting.class);
        emailSetting.setPassword("*************************");
        return AjaxResult.success(emailSetting);
    }
    return AjaxResult.success(new EmailSetting());
}
Also used : Config(com.dimple.project.system.domain.Config) EmailSetting(com.dimple.project.system.domain.EmailSetting) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

EmailSetting (com.dimple.project.system.domain.EmailSetting)2 Config (com.dimple.project.system.domain.Config)1 Properties (java.util.Properties)1 MessagingException (javax.mail.MessagingException)1 Session (javax.mail.Session)1 MimeMessage (javax.mail.internet.MimeMessage)1 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1