List of usage examples for org.springframework.mail.javamail JavaMailSenderImpl setProtocol
public void setProtocol(@Nullable String protocol)
From source file:com.github.javarch.support.config.EMailConfig.java
/** * http://support.google.com/mail/bin/answer.py?hl=en&answer=13287 * @return// w ww. j a va2 s .c om */ @Bean public MailSender mailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setDefaultEncoding("UTF-8"); mailSender.setHost(env.getRequiredProperty("mail.host")); mailSender.setPassword(env.getRequiredProperty("mail.password")); mailSender.setUsername(env.getRequiredProperty("mail.username")); mailSender.setPort(env.getProperty("mail.port", Integer.class, 25)); mailSender.setProtocol(env.getRequiredProperty("mail.transport.protocol")); mailSender.setJavaMailProperties(mailProperties()); return mailSender; }
From source file:alfio.manager.system.SmtpMailer.java
private JavaMailSender toMailSender(Event event) { JavaMailSenderImpl r = new CustomJavaMailSenderImpl(); r.setDefaultEncoding("UTF-8"); r.setHost(configurationManager// w w w . ja va 2 s . co m .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: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()); }// ww w. ja va 2 s . c om 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: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()); }//from w w w .j a v a 2 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())) { /**//www. j a v a2s .c o 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.craftercms.commons.mail.impl.EmailFactoryImplTest.java
private JavaMailSenderImpl createMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("localhost"); mailSender.setPort(25);// ww w .j av a 2 s. c o m mailSender.setProtocol("smtp"); mailSender.setDefaultEncoding(ENCODING); return mailSender; }
From source file:org.haiku.haikudepotserver.config.AppConfig.java
@Bean public MailSender mailSender(@Value("${smtp.host}") String host, @Value("${smtp.port:25}") Integer port, @Value("${smtp.auth:false}") Boolean smtpAuth, @Value("${smtp.starttls:false}") Boolean startTls, @Value("${smtp.username:}") String username, @Value("${smtp.password:}") String password) { Properties properties = new Properties(); properties.put("mail.smtp.auth", smtpAuth); properties.put("mail.smtp.starttls.enable", startTls); JavaMailSenderImpl result = new JavaMailSenderImpl(); result.setHost(host);/* w ww. j a v a2 s . com*/ result.setPort(port); result.setProtocol("smtp"); result.setJavaMailProperties(properties); if (StringUtils.isNotBlank(username)) { result.setUsername(username); } if (StringUtils.isNotBlank(password)) { result.setPassword(password); } return result; }
From source file:org.kuali.rice.core.mail.MailSenderFactoryBean.java
@Override protected Object createInstance() throws Exception { // Retrieve "mail.*" properties from the configuration system and construct a Properties object Properties properties = new Properties(); Properties configProps = ConfigContext.getCurrentContextConfig().getProperties(); LOG.debug("createInstance(): collecting mail properties."); for (Object keyObj : configProps.keySet()) { if (keyObj instanceof String) { String key = (String) keyObj; if (key.startsWith(MAIL_PREFIX)) { properties.put(key, configProps.get(key)); }//ww w . j a v a 2 s . c o m } } // Construct an appropriate Java Mail Session // If username and password properties are found, construct a Session with SMTP authentication String username = properties.getProperty(USERNAME_PROPERTY); String password = properties.getProperty(PASSWORD_PROPERTY); if (username != null && password != null) { mailSession = Session.getInstance(properties, new SimpleAuthenticator(username, password)); LOG.info("createInstance(): Initializing mail session using SMTP authentication."); } else { mailSession = Session.getInstance(properties); LOG.info("createInstance(): Initializing mail session. No SMTP authentication."); } // Construct and return a Spring Java Mail Sender JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); LOG.debug("createInstance(): setting SMTP host."); mailSender.setHost(properties.getProperty(HOST_PROPERTY)); if (properties.getProperty(PORT_PROPERTY) != null) { LOG.debug("createInstance(): setting SMTP port."); int smtpPort = Integer.parseInt(properties.getProperty(PORT_PROPERTY).trim()); mailSender.setPort(smtpPort); } String protocol = properties.getProperty(PROTOCOL_PROPERTY); if (StringUtils.isNotBlank(protocol)) { LOG.debug("createInstance(): setting mail transport protocol = " + protocol); mailSender.setProtocol(protocol); } mailSender.setSession(mailSession); LOG.info("createInstance(): Mail Sender Factory Bean initialized."); return mailSender; }
From source file:stroom.security.spring.SecurityConfiguration.java
@Bean(name = "mailSender") @Scope(StroomScope.PROTOTYPE)// w w w . j a v a 2 s.co m public MailSender mailSender() { final String host = StroomProperties.getProperty("stroom.mail.host"); final int port = StroomProperties.getIntProperty("stroom.mail.port", 587); final String protocol = StroomProperties.getProperty("stroom.mail.protocol", "smtp"); final String userName = StroomProperties.getProperty("stroom.mail.userName"); final String password = StroomProperties.getProperty("stroom.mail.password"); String propertiesFile = StroomProperties.getProperty("stroom.mail.propertiesFile"); final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost(host); javaMailSender.setPort(port); javaMailSender.setProtocol(protocol); if (!StringUtils.isEmpty(userName)) { javaMailSender.setUsername(userName); } if (!StringUtils.isEmpty(password)) { javaMailSender.setPassword(password); } if (!StringUtils.isEmpty(propertiesFile)) { propertiesFile = propertiesFile.replaceAll("~", System.getProperty("user.home")); final File file = new File(propertiesFile); if (file.isFile()) { try (FileInputStream fis = new FileInputStream(file)) { final Properties properties = new Properties(); properties.load(fis); javaMailSender.setJavaMailProperties(properties); } catch (final IOException e) { LOGGER.warn("Unable to load mail properties '" + propertiesFile + "'"); } } else { LOGGER.warn("Mail properties not found at '" + propertiesFile + "'"); } } return javaMailSender; }