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

The following examples show how to use org.springframework.mail.javamail.JavaMailSenderImpl#setUsername() . 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: EmailNotificationService.java    From jsflight with Apache License 2.0 7 votes vote down vote up
@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;
}
 
Example 2
Source File: DefaultEmailNotifier.java    From jetlinks-community with Apache License 2.0 6 votes vote down vote up
public DefaultEmailNotifier(NotifierProperties properties, TemplateManager templateManager) {
    super(templateManager);
    notifierId = properties.getId();

    DefaultEmailProperties emailProperties = new JSONObject(properties.getConfiguration())
        .toJavaObject(DefaultEmailProperties.class);
    ValidatorUtils.tryValidate(emailProperties);

    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost(emailProperties.getHost());
    mailSender.setPort(emailProperties.getPort());
    mailSender.setUsername(emailProperties.getUsername());
    mailSender.setPassword(emailProperties.getPassword());
    mailSender.setJavaMailProperties(emailProperties.createJavaMailProperties());
    this.sender = emailProperties.getSender();
    this.javaMailSender = mailSender;
}
 
Example 3
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 4
Source File: SendMail.java    From shark with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 5
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 6
Source File: EmailConfiguration.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Bean
public JavaMailSender javaMailSender() {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    
    boolean isEmailEnabled = env.getProperty("email.enabled", Boolean.class, false);
    if (isEmailEnabled) {
        sender.setHost(env.getRequiredProperty("email.host"));
        sender.setPort(env.getRequiredProperty("email.port", Integer.class));
    } 
    
    Boolean useCredentials = env.getProperty("email.useCredentials", Boolean.class);
    if (Boolean.TRUE.equals(useCredentials)) {
        sender.setUsername(env.getProperty("email.username"));
        sender.setPassword(env.getProperty("email.password"));
    }
    
    Boolean emailTLS = env.getProperty("email.tls", Boolean.class);
    if (emailTLS != null) {
      sender.getJavaMailProperties().setProperty("mail.smtp.starttls.enable", emailTLS.toString());
    }
    
    return sender;
}
 
Example 7
Source File: MailConfig.java    From restfiddle with Apache License 2.0 6 votes vote down vote up
@Bean
   public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties mailProperties = new Properties();

mailSender.setProtocol(env.getProperty("mail.protocol"));
mailSender.setHost(env.getProperty("mail.host"));
mailSender.setPort(Integer.parseInt(env.getProperty("mail.port")));

mailSender.setUsername(env.getProperty("mail.username"));
mailSender.setPassword(env.getProperty("mail.password"));

mailProperties.put("mail.smtp.auth", env.getProperty("mail.smtp.auth"));
mailProperties.put("mail.smtp.starttls.enable", env.getProperty("mail.smtp.starttls.enable"));
mailProperties.put("mail.smtp.debug", env.getProperty("mail.smtp.debug"));

mailProperties.put("mail.smtp.socketFactory.port", "465");
mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

mailProperties.put("mail.smtps.ssl.trust", env.getProperty("mail.smtps.ssl.trust"));
mailProperties.put("mail.smtps.ssl.checkserveridentity", env.getProperty("mail.smtps.ssl.checkserveridentity"));

mailSender.setJavaMailProperties(mailProperties);

return mailSender;
   }
 
Example 8
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 9
Source File: UnifiedUserMngImpl.java    From Lottery with GNU General Public License v2.0 6 votes vote down vote up
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.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);
		}
	});
}
 
Example 10
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 11
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 12
Source File: EmailConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaMailSender mailSender() {
    final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(host);
    try {
        javaMailSender.setPort(Integer.valueOf(this.port));
    } catch (Exception e) {
    }
    javaMailSender.setUsername(username);
    javaMailSender.setPassword(password);
    javaMailSender.setProtocol(protocol);
    javaMailSender.setJavaMailProperties(loadProperties());
    return javaMailSender;
}
 
Example 13
Source File: SendEmailService.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private JavaMailSenderImpl constructJavaMailSender(String emailServerHost, Integer emailServerPort, String emailServerUsername, String emailServerPassword) {
	JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
	javaMailSenderImpl.setHost(emailServerHost);
	if (emailServerPort != null) {
		javaMailSenderImpl.setPort(emailServerPort);
	}
	if (emailServerUsername != null && !emailServerUsername.isEmpty()) {
		javaMailSenderImpl.setUsername(emailServerUsername);
	}
	if (emailServerPassword != null && !emailServerPassword.isEmpty()) {
		javaMailSenderImpl.setPassword(emailServerPassword);
	}
	return javaMailSenderImpl;
}
 
Example 14
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 15
Source File: MailPlugin.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private void initMailSender(MailPluginConfiguration configuration) {
  JavaMailSenderImpl mail = new JavaMailSenderImpl();
  mail.setHost(configuration.getHost());
  mail.setPort(configuration.getPort());
  mail.setUsername(configuration.getUsername());
  mail.setPassword(configuration.getPassword());
  if (configuration.getOtherProperties() != null) {
    Properties mailProperties = new Properties();
    configuration.getOtherProperties().forEach(p -> mailProperties.put(p.getKey(), p.getValue()));
    mail.setJavaMailProperties(mailProperties);
  }
  mailSender = mail;
}
 
Example 16
Source File: DefaultMailService.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private JavaMailSenderImpl createMailSender(JsonNode jsonConfig) {
  JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
  mailSender.setHost(jsonConfig.get("smtpHost").asText());
  mailSender.setPort(parsePort(jsonConfig.get("smtpPort").asText()));
  mailSender.setUsername(jsonConfig.get("username").asText());
  mailSender.setPassword(jsonConfig.get("password").asText());
  mailSender.setJavaMailProperties(createJavaMailProperties(jsonConfig));
  return mailSender;
}
 
Example 17
Source File: DistributionSettingsPanel.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
protected void afterChange(Form form, AjaxRequestTarget target) {
	Settings settings = (Settings) form.getModelObject();
	JavaMailSenderImpl mailSender = (JavaMailSenderImpl) NextServerApplication.get().getSpringBean("mailSender");
   		
	if (!oldMailIp.equals(settings.getMailServer().getIp()) || 
    	!oldMailPort.equals(settings.getMailServer().getPort())) {    		
            mailSender.setHost(settings.getMailServer().getIp());
            mailSender.setPort(settings.getMailServer().getPort());
    }
   	    	
  		mailSender.setPassword(settings.getMailServer().getPassword());
  		mailSender.setUsername(settings.getMailServer().getUsername());
	mailSender.getJavaMailProperties().put("mail.smtp.starttls.enable", settings.getMailServer().getEnableTls());
}
 
Example 18
Source File: JavaMailUtilsTest.java    From seppb with MIT License 5 votes vote down vote up
@Before
public void before() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setPort(25);
    javaMailSender.setHost("mail.purang.com");
    javaMailSender.setPassword("Purang123");
    javaMailSender.setUsername("[email protected]");
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.auth", String.valueOf(true));
    properties.setProperty("mail.smtp.starttls.enable", String.valueOf(true));
    properties.setProperty("mail.smtp.starttls.required", String.valueOf(true));
    mailDTO.setTo(new String[]{"[email protected]"});
    mailDTO.setSubject("test");
    mailDTO.setHtml(false);
    mailDTO.setFailRetry(true);
    mailDTO.setContent("test mail");
    mailDTO.setFailTo(new String[]{"chenlang@purang"});
    mailDTO.setFailSubject("failed mail");
    mailDTO.setFailRetryTime(10 * 1000);
    mailDTO.setFailRetryCount(3);
    mailDTO.setFrom("[email protected]");
    PowerMockito.mockStatic(CommonFactory.class);
    PowerMockito.when(CommonFactory.getApplicationContext()).thenReturn(ac);
    javaMailSender.setJavaMailProperties(properties);
    when(ac.getBean(JavaMailSender.class)).thenReturn(javaMailSender);

}
 
Example 19
Source File: EmailAlert.java    From redis-manager with Apache License 2.0 4 votes vote down vote up
private JavaMailSender getJavaMailSender(AlertChannel alertChannel) {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    String smtpHost = alertChannel.getSmtpHost();
    String[] hostAndPort = smtpHost.split(SignUtil.COLON);
    mailSender.setHost(hostAndPort[0]);
    if (hostAndPort.length > 1) {
        mailSender.setPort(Integer.parseInt(hostAndPort[0]));
    }
    mailSender.setUsername(alertChannel.getEmailUserName());
    mailSender.setPassword(alertChannel.getEmailPassword());
    Properties props = mailSender.getJavaMailProperties();
    props.put("mail.transport.protocol", "smtp");
    return mailSender;
}
 
Example 20
Source File: MailConfig.java    From QuizZz with MIT License 3 votes vote down vote up
@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;
}