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

The following examples show how to use org.springframework.mail.javamail.JavaMailSenderImpl#setPort() . 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: 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 2
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 3
Source File: EmailServiceImpl.java    From cia with Apache License 2.0 6 votes vote down vote up
/**
 * 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, DEFAULT_SMTP_PORT);
	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(getSmtpPort(smtpPort));
	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;
}
 
Example 4
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 5
Source File: MailService.java    From cloud-portal with MIT License 6 votes vote down vote up
@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);
	
	// set mail encoding
	mailSender.setDefaultEncoding(StandardCharsets.UTF_8.name());

	// 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);
}
 
Example 6
Source File: MailUtil.java    From boubei-tss with Apache License 2.0 6 votes vote down vote up
public static JavaMailSenderImpl getMailSender(String _ms) {
	// 读取邮件服务器配置
	String[] configs = getEmailInfos(_ms);
	int port = EasyUtils.obj2Int( ParamConfig.getAttribute(PX.MAIL_SERVER_PORT +_ms , "25") );
	
	JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
	mailSender.setHost( configs[0] );
	mailSender.setPort( port );
	
	if( configs.length == 5 ) {
		mailSender.setUsername( configs[3] );
		mailSender.setPassword( InfoEncoder.simpleDecode(configs[4], 12) ); 
		mailSender.getJavaMailProperties().put("mail.smtp.auth", true);
	}
	
	return mailSender;
}
 
Example 7
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 8
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 9
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 10
Source File: MavenArtifactNotifierCoreNotificationConfig.java    From artifact-listener with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaMailSenderImpl mailSender(@Value("${notification.smtp.host}") String host,
		@Value("${notification.smtp.port}") int port) {
	JavaMailSenderImpl javaMailSend = new JavaMailSenderImpl();
	javaMailSend.setHost(host);
	javaMailSend.setPort(port);
	return javaMailSend;
}
 
Example 11
Source File: EmailConfiguration.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaMailSenderImpl mailSender() {
    final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(host);
    if (StringUtils.isNumeric(port)) {
        javaMailSender.setPort(Integer.valueOf(this.port));
    }
    javaMailSender.setUsername(username);
    javaMailSender.setPassword(password);
    javaMailSender.setProtocol(protocol);
    javaMailSender.setJavaMailProperties(loadProperties());
    return javaMailSender;
}
 
Example 12
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 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: EmailServiceImpl.java    From DimpleBlog with Apache License 2.0 5 votes vote down vote up
@Override
public void sendHtmlMail(String to, String title, String content) {
    EmailSetting emailSetting = configService.selectConfigByConfigKey(ConfigKey.CONFIG_KEY_EMAIL_SETTING, EmailSetting.class);
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setUsername(emailSetting.getUser());
    mailSender.setHost(emailSetting.getHost());
    mailSender.setDefaultEncoding("utf-8");
    mailSender.setPassword(emailSetting.getPassword());
    mailSender.setPort(emailSetting.getPort());

    Properties properties = new Properties();
    properties.put("mail.smtp.host", emailSetting.getHost());
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理
    properties.put("mail.smtp.port", emailSetting.getPort());
    properties.put("mail.smtp.socketFactory.port", emailSetting.getPort());
    properties.put("mail.smtp.ssl.enable", true);
    Session session = Session.getInstance(properties);

    MimeMessage mimeMessage = mailSender.createMimeMessage();
    try {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom(emailSetting.getFromEmail());
        mimeMessageHelper.setTo(to);
        if (StringUtils.isNotEmpty(emailSetting.getStationmasterEmail())) {
            mimeMessageHelper.setBcc(emailSetting.getStationmasterEmail());
        }
        mimeMessageHelper.setSubject(title);
        mimeMessageHelper.setText(content, true);
        mailSender.setSession(session);
        mailSender.send(mimeMessage);
    } catch (MessagingException e) {
       log.error(e.getMessage(), e);
    }
}
 
Example 15
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 16
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 17
Source File: CoreMailConfiguration.java    From abixen-platform with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Bean
public JavaMailSender javaMailService() {
    log.debug("javaMailService()");
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

    javaMailSender.setHost(platformMailConfigurationProperties.getHost());
    javaMailSender.setPort(platformMailConfigurationProperties.getPort());
    javaMailSender.setUsername(platformMailConfigurationProperties.getUser().getUsername());
    javaMailSender.setPassword(platformMailConfigurationProperties.getUser().getPassword());

    javaMailSender.setJavaMailProperties(getMailProperties());

    return javaMailSender;
}
 
Example 18
Source File: MailSenderImpl.java    From cerebro with GNU Affero General Public License v3.0 4 votes vote down vote up
private void initMailSender(){
    mailSender = new JavaMailSenderImpl();
    mailSender.setHost(properties.getProperty("server"));
    mailSender.setPort(Integer.parseInt(properties.getProperty("port")));
    mailSender.setDefaultEncoding("UTF-8");
}
 
Example 19
Source File: MailServiceImpl.java    From nimrod with MIT License 4 votes vote down vote up
@Override
public void initialize() {
    String host = (String) dictionaryService.get("MAIL", "HOST");
    String protocol = (String) dictionaryService.get("MAIL", "PROTOCOL");
    String port = (String) dictionaryService.get("MAIL", "PORT");
    String username = (String) dictionaryService.get("MAIL", "USERNAME");
    String password = (String) dictionaryService.get("MAIL", "PASSWORD");
    String defaultEncoding = (String) dictionaryService.get("MAIL", "DEFAULT_ENCODING");
    String smtpAuth = (String) dictionaryService.get("MAIL", "SMTP_AUTH");
    String startTlsEnable = (String) dictionaryService.get("MAIL", "STARTTLS_ENABLE");
    String startTlsRequired = (String) dictionaryService.get("MAIL", "STARTTLS_REQUIRED");

    javaMailSender = new JavaMailSenderImpl();
    if (host != null) {
        javaMailSender.setHost(host);
    }
    if (protocol != null) {
        javaMailSender.setProtocol(protocol);
    }
    if (port != null) {
        javaMailSender.setPort(Integer.parseInt(port));
    }
    if (username != null) {
        javaMailSender.setUsername(username);
    }
    if (password != null) {
        javaMailSender.setPassword(password);
    }
    if (password != null) {
        javaMailSender.setPassword(password);
    }
    if (defaultEncoding != null) {
        javaMailSender.setDefaultEncoding(defaultEncoding);
    }

    Properties properties = new Properties();
    if (smtpAuth != null) {
        properties.setProperty("mail.smtp.auth", smtpAuth);
    }
    if (startTlsEnable != null) {
        properties.setProperty("mail.starttls.enable", startTlsEnable);
    }
    if (startTlsRequired != null) {
        properties.setProperty("mail.starttls.required", startTlsRequired);
    }
    javaMailSender.setJavaMailProperties(properties);
}
 
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;
}