Search in sources :

Example 1 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project openhab1-addons by openhab.

the class Mail method sendMail.

/**
     * Sends an email with attachment(s) via SMTP
     *
     * @param to the email address of the recipient
     * @param subject the subject of the email
     * @param message the body of the email
     * @param attachmentUrlList a list of URL strings of the contents to send as attachments
     *
     * @return <code>true</code>, if sending the email has been successful and
     *         <code>false</code> in all other cases.
     */
@ActionDoc(text = "Sends an email with attachment via SMTP")
public static boolean sendMail(@ParamDoc(name = "to") String to, @ParamDoc(name = "subject") String subject, @ParamDoc(name = "message") String message, @ParamDoc(name = "attachmentUrlList") List<String> attachmentUrlList) {
    boolean success = false;
    if (MailActionService.isProperlyConfigured) {
        Email email = new SimpleEmail();
        if (attachmentUrlList != null && !attachmentUrlList.isEmpty()) {
            email = new MultiPartEmail();
            for (String attachmentUrl : attachmentUrlList) {
                // Create the attachment
                try {
                    EmailAttachment attachment = new EmailAttachment();
                    attachment.setURL(new URL(attachmentUrl));
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);
                    String fileName = attachmentUrl.replaceFirst(".*/([^/?]+).*", "$1");
                    attachment.setName(isNotBlank(fileName) ? fileName : "Attachment");
                    ((MultiPartEmail) email).attach(attachment);
                } catch (MalformedURLException e) {
                    logger.error("Invalid attachment url.", e);
                } catch (EmailException e) {
                    logger.error("Error adding attachment to email.", e);
                }
            }
        }
        email.setHostName(hostname);
        email.setSmtpPort(port);
        email.setStartTLSEnabled(startTLSEnabled);
        email.setSSLOnConnect(sslOnConnect);
        if (isNotBlank(username)) {
            if (popBeforeSmtp) {
                email.setPopBeforeSmtp(true, hostname, username, password);
            } else {
                email.setAuthenticator(new DefaultAuthenticator(username, password));
            }
        }
        try {
            if (isNotBlank(charset)) {
                email.setCharset(charset);
            }
            email.setFrom(from);
            String[] toList = to.split(";");
            for (String toAddress : toList) {
                email.addTo(toAddress);
            }
            if (!isEmpty(subject)) {
                email.setSubject(subject);
            }
            if (!isEmpty(message)) {
                email.setMsg(message);
            }
            email.send();
            logger.debug("Sent email to '{}' with subject '{}'.", to, subject);
            success = true;
        } catch (EmailException e) {
            logger.error("Could not send e-mail to '" + to + "'.", e);
        }
    } else {
        logger.error("Cannot send e-mail because of missing configuration settings. The current settings are: " + "Host: '{}', port '{}', from '{}', startTLSEnabled: {}, sslOnConnect: {}, username: '{}', password '{}'", new Object[] { hostname, String.valueOf(port), from, String.valueOf(startTLSEnabled), String.valueOf(sslOnConnect), username, password });
    }
    return success;
}
Also used : MalformedURLException(java.net.MalformedURLException) Email(org.apache.commons.mail.Email) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) SimpleEmail(org.apache.commons.mail.SimpleEmail) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) EmailAttachment(org.apache.commons.mail.EmailAttachment) EmailException(org.apache.commons.mail.EmailException) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) SimpleEmail(org.apache.commons.mail.SimpleEmail) URL(java.net.URL) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 2 with DefaultAuthenticator

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

the class SendMailTLS 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.TSL_PORT);
    email.setAuthenticator(new DefaultAuthenticator(MailConfig.APP_EMAIL, MailConfig.APP_PASSWORD));
    email.setTLS(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 3 with DefaultAuthenticator

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

the class MailUtils method sendPicMail.

public static boolean sendPicMail(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(dto.getContent(), "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 4 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project shepher by XiaoMi.

the class GeneralMailSender method send.

protected void send(String mailAddress, String title, String content) {
    if (StringUtils.isBlank(mailAddress)) {
        return;
    }
    try {
        Email email = new HtmlEmail();
        email.setHostName(hostname);
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        email.setSmtpPort(port);
        email.setFrom(from, fromname);
        email.setSubject(title);
        email.setMsg(content);
        email.addTo(mailAddress.split(mailAddressEndSeparator));
        email.send();
    } catch (Exception e) {
        logger.error("Send Mail Error", e);
    }
}
Also used : Email(org.apache.commons.mail.Email) HtmlEmail(org.apache.commons.mail.HtmlEmail) HtmlEmail(org.apache.commons.mail.HtmlEmail) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator)

Example 5 with DefaultAuthenticator

use of org.apache.commons.mail.DefaultAuthenticator in project Jartop by TheRedSpy15.

the class EmailClient method send.

@FXML
private void send() throws EmailException {
    // Credentials
    final String addressSender = Core.getUserData().getEmailAddress();
    final String password = Core.getUserData().getEmailPassword();
    final String host = Core.getUserData().getHostName();
    final short port = Core.getUserData().getSmtpPort();
    final boolean useSSL = Core.getUserData().isSslConnection();
    // Email details
    final String addressReceiver = addressField.getText();
    final String subject = subjectField.getText();
    final String message = messageArea.getText();
    // Sending
    mail.setHostName(host);
    mail.setSmtpPort(port);
    mail.setAuthenticator(new DefaultAuthenticator(addressSender, password));
    mail.setSSLOnConnect(useSSL);
    mail.setFrom(addressSender);
    mail.setSubject(subject);
    mail.setMsg(message);
    mail.addTo(addressReceiver);
    // bcc & cc
    if (!bccField.getText().trim().equals(""))
        mail.addBcc(bccField.getText());
    if (!ccField.getText().trim().equals(""))
        mail.addCc(ccField.getText());
    if (attachments != null) {
        for (EmailAttachment i : attachments) {
            mail.attach(i);
        }
    }
    mail.send();
    Desktop.getAppWindow().close();
}
Also used : EmailAttachment(org.apache.commons.mail.EmailAttachment) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) FXML(javafx.fxml.FXML)

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