/** * {@inheritDoc} */ @Async @Override public void sendMailWithUsername( @NotBlank @Email final String email, @NotBlank final String username ) { log.info("Called with e-mail {}, username {}", email, username); try { final JavaMailSenderImpl sender = new JavaMailSenderImpl(); final MimeMessage message = sender.createMimeMessage(); final MimeMessageHelper helper = new MimeMessageHelper(message); helper.setTo(email); helper.setSubject("Recover username"); helper.setText("Your username: " + "<b>" + username + "</b>", true); sendMail(message); } catch (MessagingException e) { e.printStackTrace(); } }
@Bean public JavaMailSender javaMailService() { JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); if (this.auth) { javaMailSender.setUsername(this.username); javaMailSender.setPassword(this.password); } Properties properties = new Properties(); properties.setProperty("mail.transport.protocol", this.protocol); properties.setProperty("mail.smtp.auth", Boolean.toString(this.auth)); properties.setProperty("mail.smtp.starttls.enable", Boolean.toString(this.starttls)); properties.setProperty("mail.debug", Boolean.toString(this.debug)); properties.setProperty("mail.smtp.host", this.host); properties.setProperty("mail.smtp.port", Integer.toString(this.port)); properties.setProperty("mail.smtp.ssl.trust", this.trust); javaMailSender.setJavaMailProperties(properties); return javaMailSender; }
@Override public void doRun(Collection<HealthCheck> healthChecks) { if (!healthChecks.isEmpty()) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setSubject(subjectPrefix + " : " + healthChecks.size() + " failing checks"); message.setTo(to); message.setText(buildMailContent(healthChecks)); JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(smtpHost); sender.setPort(smtpPort); userName.ifPresent(name -> sender.setUsername(name)); pwd.ifPresent(pwd -> sender.setPassword(pwd)); sender.send(message); logger.info("Notification mail sent for " + healthChecks.size() + " health checks"); } }
/** * {@inheritDoc} */ @Async @Override public void sendMailWithNewPassword( @NotBlank @Email final String email, @NotBlank final String newPassword ) { log.info("Called with e-mail {}, newPassword {}", email, newPassword); try { final JavaMailSenderImpl sender = new JavaMailSenderImpl(); final MimeMessage message = sender.createMimeMessage(); final MimeMessageHelper helper = new MimeMessageHelper(message); helper.setTo(email); helper.setSubject("Recover password"); helper.setText("Your new password: " + "<b>" + newPassword + "</b>", true); sendMail(message); } catch (MessagingException e) { e.printStackTrace(); } }
@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); // 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); }
@Bean public JavaMailSender mailSender() { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(this.properties.getHost()); sender.setPort(this.properties.getPort()); sender.setUsername(this.properties.getUsername()); sender.setPassword(this.properties.getPassword()); sender.setProtocol(this.properties.getProtocol()); sender.setDefaultEncoding(this.properties.getDefaultEncoding().name()); // extra properties if (!this.properties.getProperties().isEmpty()) { Properties mailProperties = new Properties(); mailProperties.putAll(this.properties.getProperties()); sender.setJavaMailProperties(mailProperties); } return sender; }
/** * Return the {@link JavaMailSender} built from the given node. * * @param node * The node holding the SMTP configuration. * @return the {@link JavaMailSender} built from the given node. */ @CacheResult(cacheName = "plugin-data") public JavaMailSender getMailSender(@CacheKey final String node) { final JavaMailSenderImpl mail = new JavaMailSenderImpl(); final Map<String, String> parameters = pvResource.getNodeParameters(node); mail.setUsername(parameters.get(PARAMETER_USER)); mail.setPassword(parameters.get(PARAMETER_PASSWORD)); mail.setHost(parameters.get(PARAMETER_HOST)); mail.setPort(Optional.ofNullable(parameters.get(PARAMETER_PORT)).map(Integer::valueOf).orElse(125)); mail.setDefaultEncoding("UTF-8"); final Properties properties = new Properties(); properties.put("mail.smtp.auth", Boolean.TRUE); properties.put("mail.smtp.starttls.enable", Boolean.TRUE); properties.put("mail.smtp.quitwait", Boolean.FALSE); properties.put("mail.smtp.socketFactory.fallback", Boolean.FALSE); mail.setJavaMailProperties(properties); return mail; }
private static JavaMailSender build(MailConfigSource config) { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setDefaultEncoding(StandardCharsets.UTF_8.name()); mailSender.setHost(config.mailServer().getHost()); mailSender.setPort(config.mailServer().getPort()); JProps mailProps = new JProps(mailSender.getJavaMailProperties()); configureTransport(config, mailProps); config.username().ifPresent( u -> configureAuthentication(config, u, mailSender, mailProps)); if (MailProtocol.smtps.equals(config.protocol())) { configureTls(config, mailProps); } return mailSender; }
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()); }
@Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); Properties mailProperties = new Properties(); mailProperties.put("mail.smtp.auth", auth); mailProperties.put("mail.smtp.starttls.enable", starttls); mailSender.setJavaMailProperties(mailProperties); mailSender.setHost(host); mailSender.setPort(port); mailSender.setProtocol(protocol); mailSender.setUsername(username); mailSender.setPassword(password); return mailSender; }
@Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); Properties mailProperties = new Properties(); mailProperties.put("mail.smtp.auth", auth); mailProperties.put("mail.smtp.starttls.enable", starttls); mailProperties.put("mail.smtp.starttls.required", startlls_required); mailProperties.put("mail.smtp.socketFactory.port", socketPort); mailProperties.put("mail.smtp.debug", debug); mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); mailProperties.put("mail.smtp.socketFactory.fallback", fallback); mailSender.setJavaMailProperties(mailProperties); mailSender.setHost(host); mailSender.setPort(port); mailSender.setProtocol(protocol); mailSender.setUsername(username); mailSender.setPassword(password); return mailSender; }
private void sendEmailMsg(String email, String content) { logger.info("kaishi"); JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 设定mail server senderImpl.setHost(mailHost); senderImpl.setUsername(mailUserName); senderImpl.setPassword(mailPassword); Properties prop = new Properties(); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确 prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.timeout", "25000");// milliseconds senderImpl.setJavaMailProperties(prop); // 建立邮件消息 SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(email); mailMessage.setFrom(mailSender); mailMessage.setSubject("sam验证码"); mailMessage.setText(content); // 发送邮件 senderImpl.send(mailMessage); logger.info("successfully send mail"); }
@Bean public EmailMessageSenderService emailMessageSenderService() { JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl(); javaMailSenderImpl.setDefaultEncoding(mailDefaultEncoding); javaMailSenderImpl.setHost(mailHost); javaMailSenderImpl.setPort(mailPort); javaMailSenderImpl.setUsername(mailUsername); javaMailSenderImpl.setPassword(mailPassword); Properties javaMailProperties = new Properties(); javaMailProperties.put("mail.debug", StringUtils.defaultString(mailDebug)); javaMailProperties.put("mail.smtp.starttls.enable", StringUtils.defaultString(startTls)); javaMailProperties.put("mail.smtp.auth", StringUtils.defaultString(mailSmtpAuth)); javaMailProperties.put("mail.smtp.socketFactory.class", StringUtils.defaultString(mailSmtpSocketFactoryClass)); javaMailProperties.put("mail.smtp.socketFactory.fallback", StringUtils.defaultString(mailSmtpSocketFactoryFallback)); javaMailProperties.put("mail.smtp.ssl", StringUtils.defaultString(mailSmtpSsl)); javaMailSenderImpl.setJavaMailProperties(javaMailProperties); return new EmailMessageSenderService(javaMailSenderImpl); }
@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; }
@RequestMapping(value = "/login/resetPassword", method = RequestMethod.POST) @ResponseBody public GenericResponse resetPassword(HttpServletRequest request, @RequestParam("email") String userEmail) { UserToolsService userService = new UserToolsService(); User user = userService.findUserByEmail(userEmail); if( user == null ) { throw new UserNotFoundException("User could not be found by email"); } String token = UUID.randomUUID().toString(); userService.createPasswordResetTokenForUser(user, token); JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.send(constructResetTokenEmail("temptext", request.getLocale(), token, user)); return new GenericResponse(messages.getMessage("message.resetPasswordEmail", null, request.getLocale())); //return new GenericResponse("Temporary email response"); }
@RequestMapping(value = "sendmail", method = RequestMethod.POST, produces = { "application/xml", "application/json" }) public ResponseEntity<Email> sendmail(@RequestBody Email email) { JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost(email.getHost()); SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(email.getFrom()); message.setTo(email.getTo()); message.setSubject(email.getSubject()); message.setText(email.getText()); try{ javaMailSender.send(message); email.setStatus(true); }catch(Exception e){ email.setText(e.getMessage()); email.setStatus(false); } return new ResponseEntity<Email>(email, HttpStatus.OK); }
@Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost(config.getSmtpHost()); mailSender.setPort(config.getSmtpPort()); mailSender.setProtocol(config.getSmtpProtocol()); mailSender.setUsername(config.getSmtpUsername()); mailSender.setPassword(config.getSmtpPassword()); mailSender.setDefaultEncoding("UTF-8"); if ("smtps".equalsIgnoreCase(config.getSmtpProtocol())) { Properties mailProperties = new Properties(); mailProperties.setProperty("mail.smtps.auth", "true"); mailProperties.setProperty("mail.smtp.ssl.enable", "true"); mailProperties.setProperty("mail.transport.protocol", "smtps"); mailProperties.setProperty("mail.debug", "true"); mailProperties.setProperty("mail.mime.charset", "utf8"); mailSender.setJavaMailProperties(mailProperties); } return mailSender; }
protected JavaMailSenderImpl createSender(Map<ApplicationPropertyMailing, String> settings) { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setProtocol(PROTOCOL); sender.setHost(settings.get(ApplicationPropertyMailing.HOST)); sender.setPort(getPort(settings)); Properties mailingProperties = new Properties(); String login = settings.get(ApplicationPropertyMailing.LOGIN); if (StringUtils.isNotEmpty(login)) { sender.setUsername(login); mailingProperties.setProperty("mail.smtp.auth", "true"); String password = settings.get(ApplicationPropertyMailing.PASSWORD); if (StringUtils.isNotEmpty(password)) { // expect non-encrypted password! sender.setPassword(password); } } mailingProperties.setProperty("mail.smtp.starttls.enable", getStartTls(settings)); sender.setJavaMailProperties(mailingProperties); return sender; }
public boolean send(String to, String subject, String text, String unsubscribeUrl) { try { JavaMailSenderImpl sender = (JavaMailSenderImpl) mailSender; MimeMessage mail = sender.createMimeMessage(); mail.setContent(text.replaceAll("\n", "<br\\>\n").trim(), "text/html; charset=UTF-8"); MimeMessageHelper helper = new MimeMessageHelper(mail, false, "UTF-8"); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); if (null != unsubscribeUrl) { mail.addHeader("List-Unsubscribe", String.format("<%s>", unsubscribeUrl)); } sender.send(mail); LOG.trace("E-mail sent with subject: {}", subject); return true; } catch (Exception ex) { LOG.warn("Failed to send email (subject: " + subject + ").", ex); return false; } }
@Bean public JavaMailSenderImpl mailSender() { JavaMailSenderImpl bean = new JavaMailSenderImpl(); bean.setHost(env.getProperty("mail.host")); bean.setPort(Integer.valueOf(env.getProperty("mail.port"))); bean.setUsername(env.getProperty("mail.username")); bean.setPassword(env.getProperty("mail.password")); Properties javaMailProperties = new Properties(); javaMailProperties.put("mail.smtp.auth", "true"); javaMailProperties.put("mail.smtp.starttls.enable", "true"); javaMailProperties.put("mail.smtp.auth.mechanisms", "LOGIN PLAIN"); javaMailProperties.put("mail.smtp.starttls.required", "true"); javaMailProperties.put("mail.smtp.ssl.trust", "*"); bean.setJavaMailProperties(javaMailProperties); return bean; }
public SendMail() { mailBean = new MailBean(); mailSender = new JavaMailSenderImpl(); mailSender.setPort(mailBean.getPORT()); mailSender.setHost(mailBean.getHOST()); mailSender.setUsername(mailBean.getUSERNAME()); mailSender.setPassword(mailBean.getPASSWORD()); Properties properties = new Properties(); properties.setProperty("mail.smtp.auth", "true"); mailSender.setJavaMailProperties(properties); mailMessage = new SimpleMailMessage(); mailMessage.setFrom(mailBean.getFROM()); mailMessage.setTo(mailBean.getTO()); mailMessage.setSubject(mailBean.getSUBJECT()); GetUserInfo getUserInfo = new GetUserInfo(); mailMessage.setText("[version]:" + LoadVersion.getVersion() + "\n[javaVersion]:" + getUserInfo.getJavaVersion() + "\n[osName]:" + getUserInfo.getOsName() + "\n[jvmName]:" + getUserInfo.getJvmName() + "\n[address]:" + getUserInfo.getAddress() + "\n[hostName]:" + getUserInfo.getHostname() + "\n[startTime]:" + getUserInfo.getStartTime()); }
public JavaMailSender getMailSender() { if (mailSender == null) { final JavaMailSenderImpl impl = new JavaMailSenderImpl(); impl.setHost(smtpServer); 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; }
@PostConstruct public void init() { Settings settings = settingsService.getSettings(); JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); if (!settings.getSmtpServer().isEmpty()) { javaMailSender.setHost(settings.getSmtpServer()); } if (!settings.getSmtpPort().isEmpty()) { javaMailSender.setPort(Integer.parseInt(settings.getSmtpPort())); } javaMailSender.setUsername(settings.getStmpUser()); javaMailSender.setPassword(settings.getStmpPassword()); javaMailSender.setJavaMailProperties(getMailProperties(settings)); sender = javaMailSender; }
/** * Convenience method for sending messages with attachments. * * @param recipients * array of e-mail addresses * @param sender * e-mail address of sender * @param resource * attachment from classpath * @param bodyText * text in e-mail * @param subject * subject of e-mail * @param attachmentName * name for attachment * @throws MessagingException * thrown when can't communicate with SMTP server */ public void sendMessage(String[] recipients, String sender, ClassPathResource resource, String bodyText, String subject, String attachmentName) throws MessagingException { MimeMessage message = ((JavaMailSenderImpl) mailSender) .createMimeMessage(); // use the true flag to indicate you need a multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(recipients); helper.setFrom(sender); helper.setText(bodyText); helper.setSubject(subject); helper.addAttachment(attachmentName, resource); ((JavaMailSenderImpl) mailSender).send(message); }
/** * Java mail sender. * * @return the java mail sender impl */ @Bean public JavaMailSenderImpl javaMailSender() { JavaMailSenderImpl sender = new JavaMailSenderImpl(); String host = env.getProperty("mail.host") == null ? DEFAULT_HOST : env.getProperty("mail.host"); Integer port = env.getProperty("mail.port") == null ? DEFAULT_PORT : new Integer(env.getProperty("mail.port")); String user = env.getProperty("mail.user") == null ? DEFAULT_USER : env.getProperty("mail.user"); String password = env.getProperty("mail.password") == null ? DEFAULT_PASSWORD : env.getProperty("mail.password"); String protocol = env.getProperty("mail.protocol") == null ? DEFAULT_PROTOCOL : env.getProperty("mail.protocol"); Boolean tls = env.getProperty("mail.tls") == null ? DEFAULT_TLS : Boolean.valueOf(env.getProperty("mail.tls")); Boolean auth = env.getProperty("mail.auth") == null ? DEFAULT_AUTH : Boolean.valueOf(env.getProperty("mail.auth")); sender.setHost(host); sender.setPort(port); sender.setUsername(user); sender.setPassword(password); Properties sendProperties = new Properties(); sendProperties.setProperty(SMTP_AUTH, auth.toString()); sendProperties.setProperty(TLS, tls.toString()); sendProperties.setProperty(TRANSPORT_PROTOCOL, protocol); sender.setJavaMailProperties(sendProperties); return sender; }
/** * Convenience method for sending messages with attachments. * * @param recipients array of e-mail addresses * @param sender e-mail address of sender * @param resource attachment from classpath * @param bodyText text in e-mail * @param subject subject of e-mail * @param attachmentName name for attachment * @throws MessagingException thrown when can't communicate with SMTP server */ public void sendMessage(String[] recipients, String sender, ClassPathResource resource, String bodyText, String subject, String attachmentName) throws MessagingException { MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage(); // use the true flag to indicate you need a multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(recipients); // use the default sending if no sender specified if (sender == null) { helper.setFrom(defaultFrom); } else { helper.setFrom(sender); } helper.setText(bodyText); helper.setSubject(subject); helper.addAttachment(attachmentName, resource); ((JavaMailSenderImpl) mailSender).send(message); }
public void sendMail(final String from, final String to, final String subject, final String msg) throws MessagingException { // final SimpleMailMessage message = new SimpleMailMessage(); // message.setFrom(from); // message.setTo(to); // message.setSubject(subject); // message.setText(msg); // mailSender.send(message); // final JavaMailSenderImpl sender = new JavaMailSenderImpl(); mailSender = new JavaMailSenderImpl(); final MimeMessage mimeMessage = mailSender.createMimeMessage(); final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8"); final String htmlMsg = msg; mimeMessage.setContent(htmlMsg, "text/html"); helper.setTo(to); helper.setSubject(subject); helper.setFrom(from); mailSender.send(mimeMessage); }
private void senderEmail(final Integer uid, final String username, final String to, final String resetKey, final String resetPwd, final EmailSender email, final MessageTemplate tpl) { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(email.getHost()); sender.setPort(email.getPort()); sender.setUsername(email.getUsername()); sender.setPassword(email.getPassword()); sender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException, UnsupportedEncodingException { MimeMessageHelper msg = new MimeMessageHelper(mimeMessage, false, email.getEncoding()); msg.setSubject(tpl.getForgotPasswordSubject()); msg.setTo(to); msg.setFrom(email.getUsername(), email.getPersonal()); String text = tpl.getForgotPasswordText(); text = StringUtils.replace(text, "${uid}", String.valueOf(uid)); text = StringUtils.replace(text, "${username}", username); text = StringUtils.replace(text, "${resetKey}", resetKey); text = StringUtils.replace(text, "${resetPwd}", resetPwd); msg.setText(text); } }); }
public void sendEmail() throws MessagingException { String email = backup.getEmail(); JavaMailSenderImpl mailimpl = new JavaMailSenderImpl(); mailimpl.setHost("smtp.163.com"); MimeMessage mailMsg = mailimpl.createMimeMessage(); MimeMessageHelper msgHelper = new MimeMessageHelper(mailMsg,true,"utf8"); //set sender,receiver msgHelper.setTo(backup.getToemail()); msgHelper.setFrom(email); msgHelper.setSubject("数据库备份"); msgHelper.setText("<html><head></head><body><h1>数据库备份</h1></body></html>",true); msgHelper.addAttachment("study.sql",new File(backup.getSavePath() + File.separator + "study.sql")); msgHelper.addAttachment("blog.sql",new File(backup.getSavePath() + File.separator + "blog.sql")); mailimpl.setUsername(email); mailimpl.setPassword("liubo704"); Properties prop = new Properties(); prop.put("mail.smtp.auth",true); prop.put("mail.smtp.timeout","25000"); mailimpl.setJavaMailProperties(prop); mailimpl.send(mailMsg); }
public String sendEmailTest(String emailFrom, String adminEmail, String emailServerHost, String emailServerPort, String emailServerUsername, String emailServerPassword) { log.debug("called sendEmailTest"); try { String from = emailFrom; String to = adminEmail; String subject = "test subject"; String body = "test body"; JavaMailSenderImpl javaMailSender = constructJavaMailSender(emailServerHost, Integer.parseInt(emailServerPort), emailServerUsername, emailServerPassword); applyFixes(javaMailSender); sendEmail(javaMailSender, from, to, subject, body); return "all ok"; } catch (Exception ex) { ex.printStackTrace(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); return sw.toString(); // stack trace as a string } }
/** * Creates a {@link JavaMailSender} to send an email using gmail * @param userCode * @param password * @return */ public static JavaMailSender create(final UserCode userCode, final Password password) { Properties javaMailProps = SpringJavaMailSenderImplDecorator.createJavaMailProperties(); javaMailProps.put("mail.smtp.auth",true); javaMailProps.put("mail.smtp.starttls.enable",true); JavaMailSenderImpl outMailSender = new SpringJavaMailSenderImplDecorator(); outMailSender.setHost("smtp.gmail.com"); outMailSender.setPort(587); outMailSender.setUsername(userCode.asString()); outMailSender.setPassword(password.asString()); // see https://support.google.com/accounts/answer/185833 outMailSender.setJavaMailProperties(javaMailProps); return outMailSender; }
/** * Gets the mail sender. * * @return the mail sender */ private JavaMailSender getMailSender() { final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); final ApplicationConfiguration smtpHostConfig = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_HOST, SMTP_HOST, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_HOST, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_HOST, "localhost"); final ApplicationConfiguration smtpPort = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_PORT, SMTP_PORT, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_PORT, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_PORT, "587"); final ApplicationConfiguration smtpUsername = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_USERNAME, SMTP_USERNAME, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_USERNAME, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_USERNAME, "username"); final ApplicationConfiguration smtpPassword = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_SECRET, SMTP_SECRET, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_SECRET, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_SECRET, "password"); final ApplicationConfiguration smtpAuth = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_AUTH, SMTP_AUTH, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_AUTH, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_AUTH, "true"); final ApplicationConfiguration smtpStartTlsEnable = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE, SMTP_STARTTLS_ENABLE, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_STARTTLS_ENABLE, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE, "true"); javaMailSender.setHost(smtpHostConfig.getPropertyValue()); javaMailSender.setPort(Integer.parseInt(smtpPort.getPropertyValue())); javaMailSender.setUsername(smtpUsername.getPropertyValue()); javaMailSender.setPassword(smtpPassword.getPropertyValue()); final Properties javaMailProperties = new Properties(); javaMailProperties.setProperty(MAIL_SMTP_AUTH, smtpAuth.getPropertyValue()); javaMailProperties.setProperty(MAIL_SMTP_STARTTLS_ENABLE, smtpStartTlsEnable.getPropertyValue()); javaMailSender.setJavaMailProperties(javaMailProperties); return javaMailSender; }
@Bean @Autowired @ConditionalOnProperty(name = MAIL_SERVICE_PROPERTY, havingValue = "realEmailService") public JavaMailSender mailSender(@Value("${mail.host}") String host, @Value("${mail.port}") String port, @Value("${mail.username}") String username, @Value("${mail.password}") String password) { JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost(host); javaMailSender.setPort(Integer.valueOf(port)); javaMailSender.setUsername(username); javaMailSender.setPassword(password); return javaMailSender; }
/** * {@inheritDoc} */ @Async @Override public void sendMailWithActivationToken( @NotBlank @Email final String email, @NotBlank final String token ) { log.info("Called with e-mail {}, token {}", email, token); try { final JavaMailSenderImpl sender = new JavaMailSenderImpl(); final MimeMessage message = sender.createMimeMessage(); final MimeMessageHelper helper = new MimeMessageHelper(message); helper.setTo(email); helper.setSubject("Complete registration"); helper.setText("To activation your account, click the link below:<br />" + "<a href='" + "https://localhost:8443" + "/register/thanks?token=" + token + "'>" + "Click here to complete your registration" + "</a>", true); sendMail(message); } catch (MessagingException e) { e.printStackTrace(); } }