Java Code Examples for org.springframework.mail.javamail.JavaMailSenderImpl#setDefaultEncoding()

The following examples show how to use org.springframework.mail.javamail.JavaMailSenderImpl#setDefaultEncoding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmailExchanger.java    From open-cloud with MIT License 6 votes vote down vote up
private JavaMailSenderImpl buildMailSender(EmailConfig config) {
    if (config == null) {
        throw new RuntimeException("缺少默认邮件服务器配置");
    }
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost(config.getSmtpHost());
    sender.setUsername(config.getSmtpUsername());
    sender.setPassword(config.getSmtpPassword());
    sender.setDefaultEncoding("UTF-8");
    Properties props = new Properties();
    props.setProperty("mail.smtp.auth", "true");
    props.setProperty("mail.smtp.starttls.enable", "true");
    props.setProperty("mail.smtp.starttls.required", "true");
    sender.setJavaMailProperties(props);
    return sender;
}
 
Example 2
Source File: MailService.java    From cloud-portal with MIT License 6 votes vote down vote up
@PostConstruct
public void init() {

	// create new java mail sender
	mailSender = new JavaMailSenderImpl();

	// set mail sender configuration
	mailSender.setHost(host);
	mailSender.setPort(port);
	mailSender.setProtocol(protocol);
	mailSender.setUsername(username);
	mailSender.setPassword(password);
	
	// set mail encoding
	mailSender.setDefaultEncoding(StandardCharsets.UTF_8.name());

	// create java mail properties
	Properties mailProperties = new Properties();
	mailProperties.put("mail.smtp.auth", auth);
	mailProperties.put("mail.smtp.starttls.enable", starttls);
	mailProperties.put("mail.smtp.timeout", timeout);    
	mailProperties.put("mail.smtp.connectiontimeout", timeout);   

	// set java mail properties
	mailSender.setJavaMailProperties(mailProperties);
}
 
Example 3
Source File: MailAlarmService.java    From yugong with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void start() {
    super.start();
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setUsername(emailUsername);
    mailSender.setPassword(emailPassword);
    mailSender.setHost(emailHost);
    mailSender.setDefaultEncoding("UTF-8");
    Properties pros = new Properties();
    pros.put("mail.smtp.auth", true);
    pros.put("mail.smtp.timeout", 25000);
    pros.put("mail.smtp.port", stmpPort);
    pros.put("mail.smtp.socketFactory.port", stmpPort);
    pros.put("mail.smtp.socketFactory.fallback", false);
    if (sslSupport) {
        pros.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    }

    mailSender.setJavaMailProperties(pros);

    this.mailSender = mailSender;
}
 
Example 4
Source File: SmtpMailer.java    From alf.io with GNU General Public License v3.0 6 votes vote down vote up
private static JavaMailSender toMailSender(Map<ConfigurationKeys, ConfigurationManager.MaybeConfiguration> conf) {
    JavaMailSenderImpl r = new CustomJavaMailSenderImpl();
    r.setDefaultEncoding("UTF-8");

    r.setHost(conf.get(SMTP_HOST).getRequiredValue());
    r.setPort(Integer.parseInt(conf.get(SMTP_PORT).getRequiredValue()));
    r.setProtocol(conf.get(SMTP_PROTOCOL).getRequiredValue());
    r.setUsername(conf.get(SMTP_USERNAME).getValueOrDefault(null));
    r.setPassword(conf.get(SMTP_PASSWORD).getValueOrDefault(null));

    String properties = conf.get(SMTP_PROPERTIES).getValueOrDefault(null);

    if (properties != null) {
        try {
            Properties prop = PropertiesLoaderUtils.loadProperties(new EncodedResource(new ByteArrayResource(
                    properties.getBytes(StandardCharsets.UTF_8)), "UTF-8"));
            r.setJavaMailProperties(prop);
        } catch (IOException e) {
            log.warn("error while setting the mail sender properties", e);
        }
    }
    return r;
}
 
Example 5
Source File: MailServerInfo.java    From lemon with Apache License 2.0 6 votes vote down vote up
public void updateJavaMailSender() {
    javaMailSender = new JavaMailSenderImpl();

    javaMailSender.setHost(host);
    javaMailSender.setPort(port);

    if (smtpAuth) {
        javaMailSender.setUsername(username);
        javaMailSender.setPassword(password);
    }

    javaMailSender.setDefaultEncoding("UTF-8");

    javaMailSender.setJavaMailProperties(this.getProperties());
    logger.debug("host : {}", host);
    logger.debug("port : {}", port);
    logger.debug("username : {}", username);
    logger.debug("password : {}", password);
    logger.debug("getProperties : {}", getProperties());
}
 
Example 6
Source File: JavaMailSenderFactory.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public JavaMailSenderImpl createMailSender(MailSettings mailSettings) {
  LOG.trace("createMailSender");
  JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
  mailSender.setHost(mailSettings.getHost());
  mailSender.setPort(mailSettings.getPort());
  mailSender.setProtocol(mailSettings.getProtocol());
  mailSender.setUsername(mailSettings.getUsername());
  mailSender.setPassword(mailSettings.getPassword());
  mailSender.setDefaultEncoding(mailSettings.getDefaultEncoding().name());
  Properties properties = new Properties(defaultProperties);
  defaultProperties.setProperty(MAIL_SMTP_STARTTLS_ENABLE, mailSettings.isStartTlsEnabled());
  defaultProperties.setProperty(MAIL_SMTP_QUITWAIT, mailSettings.isQuitWait());
  defaultProperties.setProperty(MAIL_SMTP_AUTH, mailSettings.isAuthenticationRequired());
  defaultProperties.setProperty(MAIL_SMTP_FROM_ADDRESS, mailSettings.getFromAddress());
  properties.putAll(mailSettings.getJavaMailProperties());
  LOG.debug("Mail properties: {}; defaults: {}", properties, defaultProperties);
  mailSender.setJavaMailProperties(properties);
  return mailSender;
}
 
Example 7
Source File: EmailServiceImpl.java    From DimpleBlog with Apache License 2.0 5 votes vote down vote up
@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");
    properties.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理
    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);
    }
}
 
Example 8
Source File: MailSenderFactory.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
private void setProperties(@NonNull JavaMailSenderImpl mailSender, @NonNull MailProperties mailProperties) {
    mailSender.setHost(mailProperties.getHost());
    mailSender.setPort(mailProperties.getPort());
    mailSender.setUsername(mailProperties.getUsername());
    mailSender.setPassword(mailProperties.getPassword());
    mailSender.setProtocol(mailProperties.getProtocol());
    mailSender.setDefaultEncoding(mailProperties.getDefaultEncoding().name());

    if (!CollectionUtils.isEmpty(mailProperties.getProperties())) {
        Properties properties = new Properties();
        properties.putAll(mailProperties.getProperties());
        mailSender.setJavaMailProperties(properties);
    }
}
 
Example 9
Source File: ServiceConfig.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaMailSenderImpl javaMailSender(){
     JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
     mailSender.setHost(mailhost);
     mailSender.setPort(port);
     mailSender.setUsername(mailusername);
     mailSender.setPassword(mailpassword);
     mailSender.setDefaultEncoding("UTF-8");
     return mailSender;
 }
 
Example 10
Source File: MailServiceImpl.java    From nimrod with MIT License 4 votes vote down vote up
@Override
public void initialize() {
    String host = (String) dictionaryService.get("MAIL", "HOST");
    String protocol = (String) dictionaryService.get("MAIL", "PROTOCOL");
    String port = (String) dictionaryService.get("MAIL", "PORT");
    String username = (String) dictionaryService.get("MAIL", "USERNAME");
    String password = (String) dictionaryService.get("MAIL", "PASSWORD");
    String defaultEncoding = (String) dictionaryService.get("MAIL", "DEFAULT_ENCODING");
    String smtpAuth = (String) dictionaryService.get("MAIL", "SMTP_AUTH");
    String startTlsEnable = (String) dictionaryService.get("MAIL", "STARTTLS_ENABLE");
    String startTlsRequired = (String) dictionaryService.get("MAIL", "STARTTLS_REQUIRED");

    javaMailSender = new JavaMailSenderImpl();
    if (host != null) {
        javaMailSender.setHost(host);
    }
    if (protocol != null) {
        javaMailSender.setProtocol(protocol);
    }
    if (port != null) {
        javaMailSender.setPort(Integer.parseInt(port));
    }
    if (username != null) {
        javaMailSender.setUsername(username);
    }
    if (password != null) {
        javaMailSender.setPassword(password);
    }
    if (password != null) {
        javaMailSender.setPassword(password);
    }
    if (defaultEncoding != null) {
        javaMailSender.setDefaultEncoding(defaultEncoding);
    }

    Properties properties = new Properties();
    if (smtpAuth != null) {
        properties.setProperty("mail.smtp.auth", smtpAuth);
    }
    if (startTlsEnable != null) {
        properties.setProperty("mail.starttls.enable", startTlsEnable);
    }
    if (startTlsRequired != null) {
        properties.setProperty("mail.starttls.required", startTlsRequired);
    }
    javaMailSender.setJavaMailProperties(properties);
}
 
Example 11
Source File: MailSenderImpl.java    From cerebro with GNU Affero General Public License v3.0 4 votes vote down vote up
private void initMailSender(){
    mailSender = new JavaMailSenderImpl();
    mailSender.setHost(properties.getProperty("server"));
    mailSender.setPort(Integer.parseInt(properties.getProperty("port")));
    mailSender.setDefaultEncoding("UTF-8");
}