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

The following examples show how to use org.springframework.mail.javamail.JavaMailSenderImpl#setHost() . 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: 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 4
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 5
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 6
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 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: MailAlarmService.java    From yugong with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example 9
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 10
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 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: 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 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: MailComposerImplTest.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/**
 * Text template only
 */
@Test
public void testComposeMimeMessageInternalTextVersionOnly() throws MessagingException, IOException, ClassNotFoundException {


    final CacheManager cacheManager = mockery.mock(CacheManager.class);
    final Cache cache = mockery.mock(Cache.class);

    mockery.checking(new Expectations() {{
        allowing(cacheManager).getCache("contentService-templateSupport"); will(returnValue(cache));
    }});

    final TemplateProcessor templates = new MailComposerTemplateSupportGroovyImpl(new GroovyGStringTemplateSupportImpl(cacheManager));


    // of course you would use DI in any real-world cases
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost("localhost");
    MimeMessage message = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    String textTemplate = "Bender lives in theme park with blackjack poetess";
    String htmlTemplate = null;
    Map<String, byte[]> attachments = Collections.emptyMap();
    MailComposerImpl mailComposer = new MailComposerImpl(null, templates);
    mailComposer.composeMessage(helper, textTemplate, htmlTemplate, attachments, Collections.EMPTY_LIST, "SHOP10", "en", "test");
    assertTrue(helper.isMultipart());
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    helper.getMimeMessage().writeTo(byteArrayOutputStream);
    String str = byteArrayOutputStream.toString("UTF-8");
    assertNotNull(str);
    // html and text present in mail message
    assertTrue(str.contains("Bender lives in theme park with blackjack poetess"));
    mockery.assertIsSatisfied();
}
 
Example 15
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 16
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 17
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 18
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 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: NeteaseMailUtil.java    From charging_pile_cloud with MIT License 4 votes vote down vote up
/**
 * 发送网页图片邮箱
 *
 * @param to
 * @param title
 * @param text
 * @return
 */
private static boolean sendMailHtml(String to, String title, String text) {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    //SimpleMailMessage mailMessage = new SimpleMailMessage();

    //建立邮件消息,发送简单邮件和html邮件的区别
    MimeMessage mailMessage = senderImpl.createMimeMessage();

    Properties prop = new Properties();
    System.out.println("sendMail...util...");
    try {
        //注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,
        //multipart模式
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        prop.setProperty("mail.smtp.socketFactory.fallback", "false");

        prop.setProperty("mail.smtp.port", "465");
        prop.setProperty("mail.smtp.socketFactory.port", "465");

        // 设置传输协议
        prop.setProperty("mail.transport.protocol", "smtp");

        prop.put("mail.smtp.auth", isAuth);
        prop.put("mail.smtp.timeout", linkTimeout);
        senderImpl.setJavaMailProperties(prop);
        senderImpl.setUsername(emailUsername);
        senderImpl.setPassword(authorizationPwd);
        //设定mail server
        senderImpl.setHost(emailHost);
        //senderImpl.setPort(587);

        // 设置收件人,寄件人 用数组发送多个邮件
        // String[] array = new String[]    {"[email protected]","[email protected]"};
        // mailMessage.setTo(array);
        messageHelper.setTo(to);
        messageHelper.setFrom(sendEmail);
        messageHelper.setSubject(title);
        messageHelper.setText(messageHtml(text), true);

        System.out.println("发送信息:" + text);

        MimeMessage msg = senderImpl.createMimeMessage();
        msg.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(sendEmail));
        //发送邮件
        senderImpl.send(mailMessage);

        System.out.println("发送邮件成功");

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("发送邮件失败");
        return false;
    }
}