Java Code Examples for org.apache.commons.mail.HtmlEmail#setSSLOnConnect()

The following examples show how to use org.apache.commons.mail.HtmlEmail#setSSLOnConnect() . 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: MailSenderDefaultImpl.java    From minsx-framework with Apache License 2.0 7 votes vote down vote up
public void finishInitial() {
    try {
        Integer sslPort = 465;
        simpleEmail = new SimpleEmail();
        simpleEmail.setHostName(this.host);
        simpleEmail.setSmtpPort(this.port);
        simpleEmail.setAuthentication(this.username, this.password);
        simpleEmail.setFrom(this.from);
        simpleEmail.setCharset("UTF-8");
        if (sslPort.equals(this.port)) {
            simpleEmail.setSSLOnConnect(true);
        }
        htmlEmail = new HtmlEmail();
        htmlEmail.setHostName(this.host);
        htmlEmail.setSmtpPort(this.port);
        htmlEmail.setAuthentication(this.username, this.password);
        htmlEmail.setFrom(this.from);
        htmlEmail.setCharset("UTF-8");
        if (sslPort.equals(this.port)) {
            htmlEmail.setSSLOnConnect(true);
        }
    } catch (EmailException e) {
        throw new MailSendException(String.format("Incorrect setting field of [from],cause: %s", e.getMessage()), e);
    }
}
 
Example 2
Source File: EmailHandler.java    From robozonky with Apache License 2.0 6 votes vote down vote up
private HtmlEmail createNewEmail(final SessionInfo session) throws EmailException {
    final HtmlEmail email = new HtmlEmail();
    email.setCharset(Defaults.CHARSET.displayName()); // otherwise the e-mail contents are mangled
    email.setHostName(getSmtpHostname());
    email.setSmtpPort(getSmtpPort());
    email.setStartTLSRequired(isStartTlsRequired());
    email.setSSLOnConnect(isSslOnConnectRequired());
    if (isAuthenticationRequired()) {
        final String username = getSmtpUsername();
        LOGGER.debug("Will contact SMTP server as '{}'.", username);
        email.setAuthentication(getSmtpUsername(), getSmtpPassword());
    } else {
        LOGGER.debug("Will contact SMTP server anonymously.");
    }
    email.setFrom(getSender(), "RoboZonky '" + session.getName() + "'");
    email.addTo(getRecipient());
    return email;
}
 
Example 3
Source File: ReportMailer.java    From AisAbnormal with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Send email with subject and message body.
 * @param subject the email subject.
 * @param body the email body.
 */
public void send(String subject, String body) {
    try {
        HtmlEmail email = new HtmlEmail();
        email.setHostName(configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_HOST, "localhost"));
        email.setSmtpPort(configuration.getInt(CONFKEY_REPORTS_MAILER_SMTP_PORT, 465));
        email.setAuthenticator(new DefaultAuthenticator(
            configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_USER, "anonymous"),
            configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_PASS, "guest")
        ));
        email.setStartTLSEnabled(false);
        email.setSSLOnConnect(configuration.getBoolean(CONFKEY_REPORTS_MAILER_SMTP_SSL, true));
        email.setFrom(configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_FROM, ""));
        email.setSubject(subject);
        email.setHtmlMsg(body);
        String[] receivers = configuration.getStringArray(CONFKEY_REPORTS_MAILER_SMTP_TO);
        for (String receiver : receivers) {
            email.addTo(receiver);
        }
        email.send();
        LOG.info("Report sent with email to " + email.getToAddresses().toString() + " (\"" + subject + "\")");
    } catch (EmailException e) {
        LOG.error(e.getMessage(), e);
    }
}
 
Example 4
Source File: MailUtil.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param toAddress		收件人邮箱
 * @param mailSubject	邮件主题
 * @param mailBody		邮件正文
 * @return
 */
public static boolean sendMail(String toAddress, String mailSubject, String mailBody){

	try {
		// Create the email message
		HtmlEmail email = new HtmlEmail();

		//email.setDebug(true);		// 将会打印一些log
		//email.setTLS(true);		// 是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验
		//email.setSSL(true);

		email.setHostName(PropertiesUtil.getString("xxl.job.mail.host"));

		if ("true".equals(PropertiesUtil.getString("xxl.job.mail.ssl"))) {
			email.setSslSmtpPort(PropertiesUtil.getString("xxl.job.mail.port"));
			email.setSSLOnConnect(true);
		} else {
			email.setSmtpPort(Integer.valueOf(PropertiesUtil.getString("xxl.job.mail.port")));
		}

		email.setAuthenticator(new DefaultAuthenticator(PropertiesUtil.getString("xxl.job.mail.username"), 
				PropertiesUtil.getString("xxl.job.mail.password")));
		email.setCharset(Charset.defaultCharset().name());

		email.setFrom(PropertiesUtil.getString("xxl.job.mail.username"), PropertiesUtil.getString("xxl.job.mail.sendNick"));
		email.addTo(toAddress);
		email.setSubject(mailSubject);
		email.setMsg(mailBody);

		//email.attach(attachment);	// add the attachment

		email.send();				// send the email
		return true;
	} catch (EmailException e) {
		logger.error(e.getMessage(), e);

	}
	return false;
}
 
Example 5
Source File: EmailUtils.java    From frpMgr with MIT License 5 votes vote down vote up
/**
 * 发送邮件
 * @param toAddress 接收地址
 * @param subject 标题
 * @param content 内容
 * @return
 */
public static boolean send(String fromAddress, String fromPassword, String fromHostName,
		String sslOnConnect, String sslSmtpPort, String toAddress, String subject, String content) {
	try {
		HtmlEmail htmlEmail = new HtmlEmail();
		// 发送地址
		htmlEmail.setFrom(fromAddress);
		// 密码校验
		htmlEmail.setAuthentication(fromAddress, fromPassword);
		// 发送服务器协议
		htmlEmail.setHostName(fromHostName);

		// SSL
		if ("true".equals(sslOnConnect)) {
			htmlEmail.setSSLOnConnect(true);
			htmlEmail.setSslSmtpPort(sslSmtpPort);
		}

		// 接收地址
		htmlEmail.addTo(toAddress);

		// 标题
		htmlEmail.setSubject(subject);
		// 内容
		htmlEmail.setMsg(content);

		// 其他信息
		htmlEmail.setCharset("utf-8");
		
		// 发送
		htmlEmail.send();
		return true;
	} catch (Exception ex) {
		logger.error(ex.getMessage(), ex);
	}
	return false;
}
 
Example 6
Source File: MailUtil.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
/**
 * @param toAddress   收件人邮箱
 * @param mailSubject 邮件主题
 * @param mailBody    邮件正文
 * @return
 */
public static boolean sendMail(String toAddress, String mailSubject, String mailBody) {

    try {
        // Create the email message
        HtmlEmail email = new HtmlEmail();

        //email.setDebug(true);		// 将会打印一些log
        //email.setTLS(true);		// 是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验
        //email.setSSL(true);

        email.setHostName(XxlJobAdminConfig.getAdminConfig().getMailHost());

        if (XxlJobAdminConfig.getAdminConfig().isMailSSL()) {
            email.setSslSmtpPort(XxlJobAdminConfig.getAdminConfig().getMailPort());
            email.setSSLOnConnect(true);
        } else {
            email.setSmtpPort(Integer.valueOf(XxlJobAdminConfig.getAdminConfig().getMailPort()));
        }

        email.setAuthenticator(new DefaultAuthenticator(XxlJobAdminConfig.getAdminConfig().getMailUsername(), XxlJobAdminConfig.getAdminConfig().getMailPassword()));
        email.setCharset("UTF-8");

        email.setFrom(XxlJobAdminConfig.getAdminConfig().getMailUsername(), XxlJobAdminConfig.getAdminConfig().getMailSendNick());
        email.addTo(toAddress);
        email.setSubject(mailSubject);
        email.setMsg(mailBody);

        //email.attach(attachment);	// add the attachment

        email.send();                // send the email
        return true;
    } catch (EmailException e) {
        logger.error(e.getMessage(), e);

    }
    return false;
}
 
Example 7
Source File: MailUtil.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * @param toAddress   收件人邮箱
 * @param mailSubject 邮件主题
 * @param mailBody    邮件正文
 * @return
 */
public static boolean sendMail(String toAddress, String mailSubject, String mailBody) {

    try {
        // Create the email message
        HtmlEmail email = new HtmlEmail();

        //email.setDebug(true);		// 将会打印一些log
        //email.setTLS(true);		// 是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验
        //email.setSSL(true);

        email.setHostName(XxlJobAdminConfig.getAdminConfig().getMailHost());

        if (XxlJobAdminConfig.getAdminConfig().isMailSSL()) {
            email.setSslSmtpPort(XxlJobAdminConfig.getAdminConfig().getMailPort());
            email.setSSLOnConnect(true);
        } else {
            email.setSmtpPort(Integer.valueOf(XxlJobAdminConfig.getAdminConfig().getMailPort()));
        }

        email.setAuthenticator(new DefaultAuthenticator(XxlJobAdminConfig.getAdminConfig().getMailUsername(), XxlJobAdminConfig.getAdminConfig().getMailPassword()));
        email.setCharset("UTF-8");

        email.setFrom(XxlJobAdminConfig.getAdminConfig().getMailUsername(), XxlJobAdminConfig.getAdminConfig().getMailSendNick());
        email.addTo(toAddress);
        email.setSubject(mailSubject);
        email.setMsg(mailBody);

        //email.attach(attachment);	// add the attachment

        email.send();                // send the email
        return true;
    } catch (EmailException e) {
        logger.error(e.getMessage(), e);

    }
    return false;
}
 
Example 8
Source File: InsecureSmtpSsl.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Email email = new SimpleEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSSLOnConnect(false); //OK

    Email email2 = new SimpleEmail();
    email2.setHostName("smtp2.googlemail.com");        
    email2.setSSLOnConnect(true); //BAD
    //email2.setSmtpPort(465);
    //email2.setAuthenticator(new DefaultAuthenticator("username", "password"));
    //email2.setFrom("[email protected]");
    //email2.setSubject("TestMail");
    //email2.setMsg("This is a test mail ... :-)");
    //email2.addTo("[email protected]");
    //email2.send();

    MultiPartEmail emailMulti = new MultiPartEmail();
    emailMulti.setHostName("mail.myserver.com");
    emailMulti.setSSLOnConnect(true); //BAD

    HtmlEmail htmlEmail = new HtmlEmail();
    htmlEmail.setHostName("mail.myserver.com");
    htmlEmail.setSSLOnConnect(true); //BAD

    ImageHtmlEmail imageEmail = new ImageHtmlEmail();
    imageEmail.setHostName("mail.myserver.com");
    imageEmail.setSSLOnConnect(true);
    imageEmail.setSSLCheckServerIdentity(true); //OK
    
    ImageHtmlEmail imageEmail2 = new ImageHtmlEmail();
    imageEmail2.setHostName("mail2.myserver.com");
    imageEmail2.setSSLCheckServerIdentity(true); //OK - reversed order
    imageEmail2.setSSLOnConnect(true);

    ImageHtmlEmail imageEmail3 = new ImageHtmlEmail();
    imageEmail3.setHostName("mail3.myserver.com");
    imageEmail3.setSSLOnConnect(true); //BAD
}
 
Example 9
Source File: DetectionEmailAlerter.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/** Sends email according to the provided config. */
private void sendEmail(HtmlEmail email) throws EmailException {
  SmtpConfiguration config = this.smtpConfig;
  email.setHostName(config.getSmtpHost());
  email.setSmtpPort(config.getSmtpPort());
  if (config.getSmtpUser() != null && config.getSmtpPassword() != null) {
    email.setAuthenticator(new DefaultAuthenticator(config.getSmtpUser(), config.getSmtpPassword()));
    email.setSSLOnConnect(true);
    email.setSslSmtpPort(Integer.toString(config.getSmtpPort()));
  }
  email.send();

  int recipientCount = email.getToAddresses().size() + email.getCcAddresses().size() + email.getBccAddresses().size();
  LOG.info("Email sent with subject '{}' to {} recipients", email.getSubject(), recipientCount);
}
 
Example 10
Source File: EmailHelper.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/** Sends email according to the provided config. */
private static void sendEmail(SmtpConfiguration config, HtmlEmail email, String subject,
    String fromAddress, DetectionAlertFilterRecipients recipients) throws EmailException {
  if (config != null) {
    email.setSubject(subject);
    LOG.info("Sending email to {}", recipients.toString());
    email.setHostName(config.getSmtpHost());
    email.setSmtpPort(config.getSmtpPort());
    if (config.getSmtpUser() != null && config.getSmtpPassword() != null) {
      email.setAuthenticator(
          new DefaultAuthenticator(config.getSmtpUser(), config.getSmtpPassword()));
      email.setSSLOnConnect(true);
    }
    email.setFrom(fromAddress);
    email.setTo(AlertUtils.toAddress(recipients.getTo()));
    if (CollectionUtils.isNotEmpty(recipients.getCc())) {
      email.setCc(AlertUtils.toAddress(recipients.getCc()));
    }
    if (CollectionUtils.isNotEmpty(recipients.getBcc())) {
      email.setBcc(AlertUtils.toAddress(recipients.getBcc()));
    }
    email.send();
    LOG.info("Email sent with subject [{}] to address [{}]!", subject, recipients.toString());
  } else {
    LOG.error("No email config provided for email with subject [{}]!", subject);
  }
}
 
Example 11
Source File: SendMailSsl.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets properties to the given HtmlEmail object based on the port from which it will be sent.
 *
 * @param email the email object to configure
 * @param port the configured outgoing port
 */
private void setPropertiesForPort(HtmlEmail email, int port) throws EmailException {
    switch (port) {
        case 587:
            String oAuth2Token = settings.getProperty(EmailSettings.OAUTH2_TOKEN);
            if (!oAuth2Token.isEmpty()) {
                if (Security.getProvider("Google OAuth2 Provider") == null) {
                    Security.addProvider(new OAuth2Provider());
                }
                Properties mailProperties = email.getMailSession().getProperties();
                mailProperties.setProperty("mail.smtp.ssl.enable", "true");
                mailProperties.setProperty("mail.smtp.auth.mechanisms", "XOAUTH2");
                mailProperties.setProperty("mail.smtp.sasl.enable", "true");
                mailProperties.setProperty("mail.smtp.sasl.mechanisms", "XOAUTH2");
                mailProperties.setProperty("mail.smtp.auth.login.disable", "true");
                mailProperties.setProperty("mail.smtp.auth.plain.disable", "true");
                mailProperties.setProperty(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oAuth2Token);
                email.setMailSession(Session.getInstance(mailProperties));
            } else {
                email.setStartTLSEnabled(true);
                email.setStartTLSRequired(true);
            }
            break;
        case 25:
            if (settings.getProperty(EmailSettings.PORT25_USE_TLS)) {
                email.setStartTLSEnabled(true);
                email.setSSLCheckServerIdentity(true);
            }
            break;
        case 465:
            email.setSslSmtpPort(Integer.toString(port));
            email.setSSLOnConnect(true);
            break;
        default:
            email.setStartTLSEnabled(true);
            email.setSSLOnConnect(true);
            email.setSSLCheckServerIdentity(true);
    }
}
 
Example 12
Source File: EmailHelper.java    From dts-shop with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 发送HTML格式的邮件
 *
 * @param host
 *            Email服务器主机
 * @param port
 *            Email服务器端口
 * @param userName
 *            登录账号
 * @param password
 *            登录密码
 * @param sslEnabled
 *            是否启用SSL
 * @param toAddress
 *            邮件接受者地址
 * @param fromAddress
 *            邮件发送者地址
 * @param fromName
 *            邮件发送者名称
 * @param subject
 *            邮件主题
 * @param htmlContent
 *            邮件内容,HTML格式
 * @return 邮件发送的MessageId,若为<code>null</code>,则发送失败
 */
public static String sendHtml(String host, int port, String userName, String password, boolean sslEnabled,
		String fromAddress, String fromName, String[] toAddress, String subject, String htmlContent) {
	HtmlEmail email = new HtmlEmail();
	email.setHostName(host);
	email.setSmtpPort(port);
	email.setAuthenticator(new DefaultAuthenticator(userName, password));
	email.setCharset("UTF-8");
	if (sslEnabled) {
		email.setSslSmtpPort(String.valueOf(port));
		email.setSSLOnConnect(true);
	}
	try {
		email.setFrom(fromAddress, fromName);
		email.addTo(toAddress);
		email.setSubject(subject);
		email.setHtmlMsg(htmlContent);
		return email.send();
	} catch (EmailException e) {
		return null;
	}
}