Search in sources :

Example 11 with DefaultAuthenticator

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

the class SendMailSSL method main.

public static void main(String[] args) throws EmailException {
    // Tạo đối tượng Email
    Email email = new SimpleEmail();
    // 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");
    // Nội dung email
    email.setMsg("Welcome to gpcoder.com");
    // send message
    email.send();
    System.out.println("Message sent successfully");
}
Also used : Email(org.apache.commons.mail.Email) SimpleEmail(org.apache.commons.mail.SimpleEmail) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) SimpleEmail(org.apache.commons.mail.SimpleEmail)

Example 12 with DefaultAuthenticator

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

the class SendAttachment method main.

public static void main(String[] args) throws EmailException, MalformedURLException {
    // Tạo một đối tượng đính kèm
    String filename = "data/myfile.txt";
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(filename);
    attachment.setURL(new URL("https://gpcoder.com"));
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Gp coder");
    attachment.setName("gpcoder");
    // Tạo đối tượng Email
    MultiPartEmail email = new MultiPartEmail();
    // Cấu hình
    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");
    // Nội dung email
    email.setMsg("Welcome to gpcoder.com");
    // Thêm đính kèm
    email.attach(attachment);
    // Gửi email
    email.send();
    System.out.println("Sent!");
}
Also used : EmailAttachment(org.apache.commons.mail.EmailAttachment) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) URL(java.net.URL)

Example 13 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project dubidubi by lzzzz4.

the class MailUtils method sendMail.

public static boolean sendMail(MailDTO dto) {
    boolean isSucess = false;
    Email email = new HtmlEmail();
    email.setHostName("smtp.qq.com");
    email.setSmtpPort(465);
    email.setAuthenticator(new DefaultAuthenticator("1622472966@qq.com", "lyvihlbjmafcbdab"));
    email.setSSLOnConnect(true);
    try {
        // 发件人
        email.setFrom("1622472966@qq.com");
        // 邮箱头
        email.setSubject(dto.getTitle());
        // 邮箱身体
        email.setContent("<h2>hhhh公司的验证码</h2><br><p><strong>验证码为" + dto.getContent() + "</strong></p><br>谢谢您的姿瓷", "text/html;charset=utf-8");
        // email.setContent("<img
        // src='http://img.99mm.net/small/2017/2389.jpg'></img>",
        // "text/html;charset=utf-8");
        email.addTo(dto.getMail());
        email.send();
        isSucess = true;
    } catch (EmailException e) {
        e.printStackTrace();
    }
    return isSucess;
}
Also used : Email(org.apache.commons.mail.Email) HtmlEmail(org.apache.commons.mail.HtmlEmail) HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator)

Example 14 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project dhis2-core by dhis2.

the class EmailMessageSender method getHtmlEmail.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private HtmlEmail getHtmlEmail(String hostName, int port, String username, String password, boolean tls, String sender) throws EmailException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(hostName);
    email.setFrom(sender, getEmailName());
    email.setSmtpPort(port);
    email.setStartTLSEnabled(tls);
    if (username != null && password != null) {
        email.setAuthenticator(new DefaultAuthenticator(username, password));
    }
    return email;
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator)

Example 15 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project graylog2-server by Graylog2.

the class EmailFactory method applyConfig.

private <T extends Email> T applyConfig(T email) throws EmailException {
    email.setCharset(EmailConstants.UTF_8);
    email.setHostName(configuration.getHostname());
    email.setSmtpPort(configuration.getPort());
    if (configuration.isUseSsl()) {
        email.setSslSmtpPort(Integer.toString(configuration.getPort()));
    }
    if (configuration.isUseAuth()) {
        email.setAuthenticator(new DefaultAuthenticator(Strings.nullToEmpty(configuration.getUsername()), Strings.nullToEmpty(configuration.getPassword())));
    }
    email.setSSLOnConnect(configuration.isUseSsl());
    email.setStartTLSEnabled(configuration.isUseTls());
    email.setFrom(configuration.getFromEmail());
    return email;
}
Also used : DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator)

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