Example usage for org.springframework.mail.javamail JavaMailSenderImpl setDefaultEncoding

List of usage examples for org.springframework.mail.javamail JavaMailSenderImpl setDefaultEncoding

Introduction

In this page you can find the example usage for org.springframework.mail.javamail JavaMailSenderImpl setDefaultEncoding.

Prototype

public void setDefaultEncoding(@Nullable String defaultEncoding) 

Source Link

Document

Set the default encoding to use for MimeMessage MimeMessages created by this instance.

Usage

From source file:com.glaf.mail.config.JavaMailSenderConfiguration.java

@Bean(name = "javaMailSender")
public JavaMailSenderImpl buildJavaMailSender() {
    MailConfig cfg = new MailConfig();
    String filename = SystemProperties.getConfigRootPath() + Constants.MAIL_CONFIG;
    Properties properties = PropertiesUtils.loadFilePathResource(filename);
    cfg.setEncoding(properties.getProperty("mail.defaultEncoding", "GBK"));
    cfg.setHost(properties.getProperty("mail.host", "127.0.0.1"));
    cfg.setUsername(properties.getProperty("mail.username"));
    cfg.setPassword(properties.getProperty("mail.password"));
    if (StringUtils.equals(properties.getProperty("mail.auth"), "true")) {
        cfg.setAuth(true);/*from   w w w. j a  v a  2 s  .  c  o m*/
    }

    int port = JavaMailSenderImpl.DEFAULT_PORT;
    if (properties.getProperty("mail.port") != null) {
        port = Integer.parseInt(properties.getProperty("mail.port"));
    }

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setJavaMailProperties(properties);
    sender.setHost(properties.getProperty("mail.host"));
    sender.setPort(port);
    sender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
    sender.setUsername(properties.getProperty("mail.username"));
    sender.setPassword(properties.getProperty("mail.password"));
    sender.setDefaultEncoding(properties.getProperty("mail.defaultEncoding", "GBK"));

    return sender;
}

From source file:com.github.dactiv.fear.service.service.message.MessageService.java

/**
 *  spring ??????./* w w  w  .j  av a 2  s  .com*/
 * @throws Exception
 */
@Override
public void afterPropertiesSet() throws Exception {

    for (Resource r : resources) {

        JavaMailSenderImpl jms = new JavaMailSenderImpl();

        Properties properties = new Properties();
        properties.load(r.getInputStream());

        jms.setUsername(properties.getProperty("mail.username"));
        jms.setPassword(properties.getProperty("mail.password"));
        jms.setHost(properties.getProperty("mail.host"));
        jms.setDefaultEncoding(properties.getProperty("mail.default.encoding", DEFAULT_MAIL_ENCODING));
        jms.setJavaMailProperties(properties);

        javaMailSenders.add(jms);
    }

}

From source file:com.glaf.mail.business.MailSendThread.java

public void run() {
    logger.debug("---------------send mail----------------------------");
    if (mailItem != null && MailTools.isMailAddress(mailItem.getMailTo())) {
        /**/*from w w w. j a v a2s  .co  m*/
         * ?5????
         */
        if (mailItem.getRetryTimes() > 5) {
            return;
        }
        /**
         * ????????
         */
        if (mailItem.getSendStatus() == 1) {
            return;
        }
        /**
         * ????
         */
        if (mailItem.getLastModified() < (System.currentTimeMillis() - DateUtils.DAY * 30)) {
            return;
        }

        MailAccount mailAccount = null;
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        List<MailAccount> accounts = task.getAccounts();
        if (accounts.size() > 1) {
            java.util.Random r = new java.util.Random();
            mailAccount = accounts.get(Math.abs(r.nextInt(accounts.size())));
        } else {
            mailAccount = accounts.get(0);
        }

        logger.debug("send account:" + mailAccount);

        String content = task.getContent();

        if (StringUtils.isEmpty(task.getCallbackUrl())) {
            String serviceUrl = SystemConfig.getServiceUrl();
            String callbackUrl = serviceUrl + "/website/mail/receive/view";
            task.setCallbackUrl(callbackUrl);
        }

        if (StringUtils.isNotEmpty(task.getCallbackUrl())) {
            MxMailHelper mailHelper = new MxMailHelper();
            String messageId = "{taskId:\"" + task.getId() + "\",itemId:\"" + mailItem.getId() + "\"}";
            messageId = RequestUtils.encodeString(messageId);
            String url = task.getCallbackUrl() + "?messageId=" + messageId;
            content = mailHelper.embedCallbackScript(content, url);
        }

        MailMessage mailMessage = new MailMessage();
        mailMessage.setFrom(mailAccount.getMailAddress());
        mailMessage.setTo(mailItem.getMailTo());
        mailMessage.setContent(content);
        mailMessage.setSubject(task.getSubject());
        mailMessage.setSaveMessage(false);

        javaMailSender.setHost(mailAccount.getSmtpServer());
        javaMailSender.setPort(mailAccount.getSendPort() > 0 ? mailAccount.getSendPort() : 25);
        javaMailSender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
        javaMailSender.setUsername(mailAccount.getUsername());
        if (StringUtils.isNotEmpty(mailAccount.getPassword())) {
            String pwd = RequestUtils.decodeString(mailAccount.getPassword());
            javaMailSender.setPassword(pwd);
            // logger.debug("send account pwd:"+pwd);
        }
        javaMailSender.setDefaultEncoding("GBK");

        try {
            mailItem.setSendDate(new Date());
            mailItem.setRetryTimes(mailItem.getRetryTimes() + 1);
            MailSender mailSender = ContextFactory.getBean("mailSender");
            mailSender.send(javaMailSender, mailMessage);
            mailItem.setSendStatus(1);
        } catch (Throwable ex) {
            mailItem.setSendStatus(-1);
            if (LogUtils.isDebug()) {
                logger.debug(ex);
                ex.printStackTrace();
            }
            if (ex instanceof javax.mail.internet.ParseException) {
                mailItem.setSendStatus(-10);
            } else if (ex instanceof javax.mail.AuthenticationFailedException) {
                mailItem.setSendStatus(-20);
            } else if (ex instanceof javax.mail.internet.AddressException) {
                mailItem.setSendStatus(-30);
            } else if (ex instanceof javax.mail.SendFailedException) {
                mailItem.setSendStatus(-40);
            } else if (ex instanceof java.net.UnknownHostException) {
                mailItem.setSendStatus(-50);
            } else if (ex instanceof java.net.SocketException) {
                mailItem.setSendStatus(-60);
            } else if (ex instanceof java.io.IOException) {
                mailItem.setSendStatus(-70);
            } else if (ex instanceof java.net.ConnectException) {
                mailItem.setSendStatus(-80);
            } else if (ex instanceof javax.mail.MessagingException) {
                mailItem.setSendStatus(-90);
                if (ex.getMessage().indexOf("response: 554") != -1) {
                    mailItem.setSendStatus(-99);
                }
            }
        } finally {
            mailDataFacede.updateMail(task.getId(), mailItem);
        }
    }
}

From source file:org.apache.syncope.core.logic.NotificationTest.java

@Before
public void setupSMTP() throws Exception {
    JavaMailSenderImpl sender = (JavaMailSenderImpl) mailSender;
    sender.setDefaultEncoding(SyncopeConstants.DEFAULT_ENCODING);
    sender.setHost(SMTP_HOST);/* w  ww .  j a va2 s. c  o  m*/
    sender.setPort(SMTP_PORT);
}

From source file:org.apache.syncope.core.notification.NotificationTest.java

@Before
public void setupSMTP() throws Exception {
    JavaMailSenderImpl sender = (JavaMailSenderImpl) mailSender;
    sender.setDefaultEncoding(SyncopeConstants.DEFAULT_ENCODING);
    sender.setHost(smtpHost);/*from  w  w w .  java 2  s . co m*/
    sender.setPort(smtpPort);
    sender.setUsername(mailAddress);
    sender.setPassword(mailPassword);
}

From source file:org.craftercms.commons.mail.impl.EmailFactoryImplTest.java

private JavaMailSenderImpl createMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost("localhost");
    mailSender.setPort(25);/*from  ww  w . j a  va2  s  .  c o  m*/
    mailSender.setProtocol("smtp");
    mailSender.setDefaultEncoding(ENCODING);

    return mailSender;
}