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

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

Introduction

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

Prototype

public void setPort(int port) 

Source Link

Document

Set the mail server port.

Usage

From source file:alfio.manager.system.SmtpMailer.java

private JavaMailSender toMailSender(Event event) {
    JavaMailSenderImpl r = new CustomJavaMailSenderImpl();
    r.setDefaultEncoding("UTF-8");

    r.setHost(configurationManager/*from  www.  java 2 s  .  c  om*/
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_HOST)));
    r.setPort(Integer.valueOf(configurationManager
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PORT))));
    r.setProtocol(configurationManager
            .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PROTOCOL)));
    r.setUsername(configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_USERNAME), null));
    r.setPassword(configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PASSWORD), null));

    String properties = configurationManager.getStringConfigValue(
            Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PROPERTIES), 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;
}

From source file:org.obiba.mica.config.MailConfiguration.java

@Bean
public JavaMailSenderImpl javaMailSender() {
    log.debug("Configuring mail server");
    String host = propertyResolver.getProperty(PROP_HOST, DEFAULT_PROP_HOST);
    int port = propertyResolver.getProperty(PROP_PORT, Integer.class, 0);
    String user = propertyResolver.getProperty(PROP_USER);
    String password = propertyResolver.getProperty(PROP_PASSWORD);
    String protocol = propertyResolver.getProperty(PROP_PROTO);
    Boolean tls = propertyResolver.getProperty(PROP_TLS, Boolean.class, false);
    Boolean auth = propertyResolver.getProperty(PROP_AUTH, Boolean.class, false);

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    if (host != null && !host.isEmpty()) {
        sender.setHost(host);/*from  w  w  w. j ava2 s.  c om*/
    } else {
        log.warn("Warning! Your SMTP server is not configured. We will try to use one on localhost.");
        log.debug("Did you configure your SMTP settings in your application.yml?");
        sender.setHost(DEFAULT_HOST);
    }
    sender.setPort(port);
    sender.setUsername(user);
    sender.setPassword(password);

    Properties sendProperties = new Properties();
    sendProperties.setProperty(PROP_SMTP_AUTH, auth.toString());
    sendProperties.setProperty(PROP_STARTTLS, tls.toString());
    sendProperties.setProperty(PROP_TRANSPORT_PROTO, protocol);
    sender.setJavaMailProperties(sendProperties);
    return sender;
}

From source file:br.com.valecard.config.MainConfig.java

@Bean
public JavaMailSender mailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

    Properties properties = new Properties();
    properties.setProperty("mail.smtp.auth", environment.getProperty("mail.smtp.auth"));
    properties.setProperty("mail.smtp.starttls.enable", environment.getProperty("mail.smtp.starttls.enable"));

    mailSender.setHost(environment.getProperty("mail.host"));
    mailSender.setPort(environment.getProperty("mail.port", Integer.class));
    mailSender.setUsername(environment.getProperty("mail.username"));
    mailSender.setPassword(environment.getProperty("mail.password"));

    mailSender.setJavaMailProperties(properties);

    return mailSender;
}

From source file:nu.yona.server.CoreConfiguration.java

@Bean
public JavaMailSender javaMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", yonaProperties.getEmail().getSmtp().isEnableAuth());
    mailProperties.put("mail.smtp.starttls.enable", yonaProperties.getEmail().getSmtp().isEnableStartTls());
    mailSender.setJavaMailProperties(mailProperties);
    mailSender.setHost(yonaProperties.getEmail().getSmtp().getHost());
    mailSender.setPort(yonaProperties.getEmail().getSmtp().getPort());
    mailSender.setProtocol(yonaProperties.getEmail().getSmtp().getProtocol());
    mailSender.setUsername(yonaProperties.getEmail().getSmtp().getUsername());
    mailSender.setPassword(yonaProperties.getEmail().getSmtp().getPassword());
    return mailSender;
}

From source file:com.thinkbiganalytics.metadata.sla.TestConfiguration.java

@Bean(name = "slaEmailSender")
public JavaMailSender javaMailSender(
        @Qualifier("slaEmailConfiguration") EmailConfiguration emailConfiguration) {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", emailConfiguration.getSmtpAuth());
    mailProperties.put("mail.smtp.starttls.enable", emailConfiguration.getStarttls());
    if (StringUtils.isNotBlank(emailConfiguration.getSmptAuthNtmlDomain())) {
        mailProperties.put("mail.smtp.auth.ntlm.domain", emailConfiguration.getSmptAuthNtmlDomain());
    }/*  w w  w.ja  v  a 2 s . c o  m*/
    mailProperties.put("mail.debug", "true");

    mailSender.setJavaMailProperties(mailProperties);
    mailSender.setHost(emailConfiguration.getHost());
    mailSender.setPort(emailConfiguration.getPort());
    mailSender.setProtocol(emailConfiguration.getProtocol());
    mailSender.setUsername(emailConfiguration.getUsername());
    mailSender.setPassword(emailConfiguration.getPassword());
    return mailSender;
}

From source file:de.iteratec.iteraplan.businesslogic.service.notifications.JavaMailSenderFactory.java

/**
 * Creates the instance of {@link JavaMailSenderImpl}. The returned sender is configured and
 * ready to use. If the notifications are not activated, {@code null} will be returned.
 * /*from   w w  w  . j  ava2s  .  c o  m*/
 * @return configured instance of {@link JavaMailSenderImpl} or {@code null} if notifications are not activated
 */
public JavaMailSender create() {
    if (!activated) {
        return null;
    }

    if (StringUtils.isBlank(getHost())) {
        throw new IteraplanTechnicalException(IteraplanErrorMessages.NOTIFICATION_CONFIGURATION_INCOMPLETE);
    }

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Configuring mail sender framework with hostname " + getHost() + ", port " + getPort()
                + ". SSL connections enabled: " + isSsl() + ", STARTTLS enabled: " + isStartTls()
                + ", username " + getUsername() + ", password " + (getPassword() != null ? "***" : "null"));
    }
    sender.setHost(getHost());
    sender.setUsername(getUsername());
    sender.setPassword(getPassword());

    if (StringUtils.isNotBlank(getPort())) {
        sender.setPort(Integer.parseInt(getPort()));
    }

    Properties properties = new Properties();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(
                "Turned on mail sender framework's DEBUG logging. It will be written to the console, respectively catalina.out");
        properties.put("mail.debug", "true");
    }

    if (isStartTls()) {
        properties.put("mail.smtp.starttls.enable", "true");
    }

    if (isSsl()) {
        properties.put("mail.smtp.ssl.enable", "true");
    }
    sender.setJavaMailProperties(properties);

    return sender;
}

From source file:com.thinkbiganalytics.metadata.sla.config.EmailServiceLevelAgreementSpringConfiguration.java

@Bean(name = "slaEmailSender")
public JavaMailSender javaMailSender(
        @Qualifier("slaEmailConfiguration") EmailConfiguration emailConfiguration) {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

    Properties mailProperties = mailSender.getJavaMailProperties();
    mailProperties.put("mail.smtp.auth", StringUtils.defaultIfBlank(emailConfiguration.getSmtpAuth(), "false"));
    mailProperties.put("mail.smtp.starttls.enable",
            StringUtils.defaultIfBlank(emailConfiguration.getStarttls(), "false"));

    if (StringUtils.isNotBlank(emailConfiguration.getStarttlsRequired())) {
        mailProperties.put("mail.smtp.starttls.required", emailConfiguration.getStarttlsRequired());
    }//  w ww .  ja  v  a2  s .  c  o m
    if (StringUtils.isNotBlank(emailConfiguration.getSmptAuthNtmlDomain())) {
        mailProperties.put("mail.smtp.auth.ntlm.domain", emailConfiguration.getSmptAuthNtmlDomain());
    }

    mailProperties.put("mail.smtp.connectiontimeout",
            StringUtils.defaultIfBlank(emailConfiguration.getSmtpConnectionTimeout(), "5000"));
    mailProperties.put("mail.smtp.timeout",
            StringUtils.defaultIfBlank(emailConfiguration.getSmtpTimeout(), "5000"));
    mailProperties.put("mail.smtp.writetimeout",
            StringUtils.defaultIfBlank(emailConfiguration.getSmtpWriteTimeout(), "5000"));

    if (StringUtils.isNotBlank(emailConfiguration.getSslEnable())) {
        mailProperties.put("mail.smtp.ssl.enable", emailConfiguration.getSslEnable());
    }
    if (StringUtils.isNotBlank(emailConfiguration.getDebug())) {
        mailProperties.put("mail.debug", emailConfiguration.getDebug());
    }

    mailSender.setHost(emailConfiguration.getHost());
    mailSender.setPort(emailConfiguration.getPort());
    mailSender.setProtocol(emailConfiguration.getProtocol());
    mailSender.setUsername(emailConfiguration.getUsername());
    mailSender.setPassword(emailConfiguration.getPassword());

    return mailSender;
}

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  a 2 s .c om
         * ?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:nl.strohalm.cyclos.entities.settings.MailSettings.java

public JavaMailSender getMailSender() {
    if (mailSender == null) {
        final JavaMailSenderImpl impl = new JavaMailSenderImpl();
        impl.setHost(smtpServer);//www.  ja v a  2 s  .  c  o m
        impl.setPort(smtpPort);
        final Properties properties = new Properties();
        if (StringUtils.isNotEmpty(smtpUsername)) {
            // Use authentication
            properties.setProperty("mail.smtp.auth", "true");
            impl.setUsername(smtpUsername);
            impl.setPassword(smtpPassword);
        }
        if (smtpUseTLS) {
            properties.setProperty("mail.smtp.starttls.enable", "true");
        }
        impl.setJavaMailProperties(properties);
        mailSender = impl;
    }
    return mailSender;
}

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);//from   w w  w .  j  av  a2s  . c  o m
    sender.setPort(SMTP_PORT);
}