Search in sources :

Example 6 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project pinot by linkedin.

the class EmailHelper method sendEmail.

/** Sends email according to the provided config. */
private static void sendEmail(SmtpConfiguration config, HtmlEmail email, String subject, String fromAddress, String toAddress) throws EmailException {
    if (config != null) {
        email.setSubject(subject);
        LOG.info("Sending email to {}", toAddress);
        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);
        for (String address : toAddress.split(EMAIL_ADDRESS_SEPARATOR)) {
            email.addTo(address);
        }
        email.send();
        LOG.info("Email sent with subject [{}] to address [{}]!", subject, toAddress);
    } else {
        LOG.error("No email config provided for email with subject [{}]!", subject);
    }
}
Also used : DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator)

Example 7 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project Lucee by lucee.

the class SMTPVerifier method _verify.

private static boolean _verify(String host, String username, String password, int port) throws MessagingException {
    boolean hasAuth = !StringUtil.isEmpty(username);
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    if (hasAuth)
        props.put("mail.smtp.auth", "true");
    if (hasAuth)
        props.put("mail.smtp.user", username);
    if (hasAuth)
        props.put("mail.transport.connect-timeout", "30");
    if (port > 0)
        props.put("mail.smtp.port", String.valueOf(port));
    Authenticator auth = null;
    if (hasAuth)
        auth = new DefaultAuthenticator(username, password);
    Session session = Session.getInstance(props, auth);
    Transport transport = session.getTransport("smtp");
    if (hasAuth)
        transport.connect(host, username, password);
    else
        transport.connect();
    boolean rtn = transport.isConnected();
    transport.close();
    return rtn;
}
Also used : DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) Properties(java.util.Properties) Transport(javax.mail.Transport) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 8 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project xxl-job by xuxueli.

the class MailUtil method sendMail.

/**
 * @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());
        email.setSmtpPort(Integer.valueOf(XxlJobAdminConfig.getAdminConfig().getMailPort()));
        // email.setSslSmtpPort(port);
        email.setAuthenticator(new DefaultAuthenticator(XxlJobAdminConfig.getAdminConfig().getMailUsername(), XxlJobAdminConfig.getAdminConfig().getMailPassword()));
        email.setCharset(Charset.defaultCharset().name());
        email.setFrom(XxlJobAdminConfig.getAdminConfig().getMailUsername(), XxlJobAdminConfig.getAdminConfig().getMailSendNick());
        email.addTo(toAddress);
        email.setSubject(mailSubject);
        email.setMsg(mailBody);
        // email.attach(attachment);	// add the attachment
        // send the email
        email.send();
        return true;
    } catch (EmailException e) {
        logger.error(e.getMessage(), e);
    }
    return false;
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator)

Example 9 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project estatio by estatio.

the class EmailServiceThrowingException method send.

// endregion
// region > send
@Programmatic
public boolean send(final List<String> toList, final List<String> ccList, final List<String> bccList, final String subject, final String body, final DataSource... attachments) {
    try {
        final ImageHtmlEmail email = new ImageHtmlEmail();
        email.setAuthenticator(new DefaultAuthenticator(senderEmailAddress, senderEmailPassword));
        email.setHostName(getSenderEmailHostName());
        email.setSmtpPort(senderEmailPort);
        email.setStartTLSEnabled(getSenderEmailTlsEnabled());
        email.setDataSourceResolver(new DataSourceClassPathResolver("/", true));
        // 
        // change from default impl.
        // 
        email.setSocketTimeout(2000);
        email.setSocketConnectionTimeout(2000);
        final Properties properties = email.getMailSession().getProperties();
        // TODO ISIS-987: check whether all these are required and extract as configuration settings
        properties.put("mail.smtps.auth", "true");
        properties.put("mail.debug", "true");
        properties.put("mail.smtps.port", "" + senderEmailPort);
        properties.put("mail.smtps.socketFactory.port", "" + senderEmailPort);
        properties.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtps.socketFactory.fallback", "false");
        email.setFrom(senderEmailAddress);
        email.setSubject(subject);
        email.setHtmlMsg(body);
        email.setCharset("windows-1252");
        if (attachments != null && attachments.length > 0) {
            for (DataSource attachment : attachments) {
                email.attach(attachment, attachment.getName(), "");
            }
        }
        if (notEmpty(toList)) {
            email.addTo(toList.toArray(new String[toList.size()]));
        }
        if (notEmpty(ccList)) {
            email.addCc(ccList.toArray(new String[ccList.size()]));
        }
        if (notEmpty(bccList)) {
            email.addBcc(bccList.toArray(new String[bccList.size()]));
        }
        email.send();
    } catch (EmailException ex) {
        // 
        // change from default impl.
        // 
        LOG.error("An error occurred while trying to send an email about user email verification", ex);
        throw new RuntimeException(ex);
    }
    return true;
}
Also used : DataSourceClassPathResolver(org.apache.commons.mail.resolver.DataSourceClassPathResolver) ImageHtmlEmail(org.apache.commons.mail.ImageHtmlEmail) EmailException(org.apache.commons.mail.EmailException) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) Properties(java.util.Properties) DataSource(javax.activation.DataSource) Programmatic(org.apache.isis.applib.annotation.Programmatic)

Example 10 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project Java-Tutorial by gpcodervn.

the class SendHtmlEmail method main.

public static void main(String[] args) throws EmailException, MalformedURLException {
    // Tạo đối tượng Email
    ImageHtmlEmail email = new ImageHtmlEmail();
    // Cấu hình thông tin Email Server
    email.setHostName(MailConfig.HOST_NAME);
    email.setSmtpPort(MailConfig.SSL_PORT);
    email.setAuthenticator(new DefaultAuthenticator(MailConfig.APP_EMAIL, MailConfig.APP_PASSWORD));
    email.setSSLOnConnect(true);
    // Người gửi
    email.setFrom(MailConfig.APP_EMAIL);
    // Người nhận
    email.addTo(MailConfig.RECEIVE_EMAIL);
    // Tiêu đề
    email.setSubject("Testing Subject");
    // Định nghĩa URL cơ sở để xác định đúng vị trí nguồn dữ liệu (img,..)
    // (Trong trường hợp nó có đường dẫn tương đối, ví dụ thẻ img như bên dưới)
    URL url = new URL("https://gpcoder.com");
    email.setDataSourceResolver(new DataSourceUrlResolver(url));
    // Nội dung email
    String htmlContent = "<h1>Welcome to <a href=\"gpcoder.com\">GP Coder</a></h1>" + "<img src=\"wp-content/uploads/2017/10/Facebook_Icon_GP_2-300x180.png\" " + "	width=\"300\" " + "	height=\"180\" " + "	border=\"0\" " + "	alt=\"gpcoder.com\" />";
    email.setHtmlMsg(htmlContent);
    // Nội dung thay thế:
    // Trong trường hợp chương trình đọc email của người nhận ko hỗ trợ HTML
    email.setTextMsg("Your email client does not support HTML messages");
    // send message
    email.send();
    System.out.println("Message sent successfully");
}
Also used : DataSourceUrlResolver(org.apache.commons.mail.resolver.DataSourceUrlResolver) ImageHtmlEmail(org.apache.commons.mail.ImageHtmlEmail) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) URL(java.net.URL)

Aggregations

DefaultAuthenticator (org.apache.commons.mail.DefaultAuthenticator)16 Email (org.apache.commons.mail.Email)7 EmailException (org.apache.commons.mail.EmailException)6 HtmlEmail (org.apache.commons.mail.HtmlEmail)6 SimpleEmail (org.apache.commons.mail.SimpleEmail)4 URL (java.net.URL)3 EmailAttachment (org.apache.commons.mail.EmailAttachment)3 ImageHtmlEmail (org.apache.commons.mail.ImageHtmlEmail)3 MultiPartEmail (org.apache.commons.mail.MultiPartEmail)3 Properties (java.util.Properties)2 MalformedURLException (java.net.MalformedURLException)1 FXML (javafx.fxml.FXML)1 DataSource (javax.activation.DataSource)1 Authenticator (javax.mail.Authenticator)1 Session (javax.mail.Session)1 Transport (javax.mail.Transport)1 DataSourceClassPathResolver (org.apache.commons.mail.resolver.DataSourceClassPathResolver)1 DataSourceUrlResolver (org.apache.commons.mail.resolver.DataSourceUrlResolver)1 Programmatic (org.apache.isis.applib.annotation.Programmatic)1 ActionDoc (org.openhab.core.scriptengine.action.ActionDoc)1