Search in sources :

Example 36 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project cerberus-source by cerberustesting.

the class EmailService method sendHtmlMail.

@Override
public void sendHtmlMail(Email cerberusEmail) throws Exception {
    HtmlEmail email = new HtmlEmail();
    email.setSmtpPort(cerberusEmail.getSmtpPort());
    email.setHostName(cerberusEmail.getHost());
    email.setFrom(cerberusEmail.getFrom());
    email.setSubject(cerberusEmail.getSubject());
    email.setHtmlMsg(cerberusEmail.getBody());
    if (cerberusEmail.isSetTls()) {
        email = (HtmlEmail) email.setStartTLSEnabled(true);
    }
    // email.setTLS(cerberusEmail.isSetTls());
    email.setDebug(true);
    if (!StringUtils.isNullOrEmpty(cerberusEmail.getUserName()) || !StringUtils.isNullOrEmpty(cerberusEmail.getPassword())) {
        email.setAuthentication(cerberusEmail.getUserName(), cerberusEmail.getPassword());
    }
    String[] destinataire = cerberusEmail.getTo().split(";");
    for (int i = 0; i < destinataire.length; i++) {
        String name;
        String emailaddress;
        if (destinataire[i].contains("<")) {
            String[] destinatairedata = destinataire[i].split("<");
            name = destinatairedata[0].trim();
            emailaddress = destinatairedata[1].replace(">", "").trim();
        } else {
            name = "";
            emailaddress = destinataire[i];
        }
        email.addTo(emailaddress, name);
    }
    if (!StringUtil.isNullOrEmpty(cerberusEmail.getCc())) {
        String[] copy = cerberusEmail.getCc().split(";");
        for (int i = 0; i < copy.length; i++) {
            String namecc;
            String emailaddresscc;
            if (copy[i].contains("<")) {
                String[] copydata = copy[i].split("<");
                namecc = copydata[0].trim();
                emailaddresscc = copydata[1].replace(">", "").trim();
            } else {
                namecc = "";
                emailaddresscc = copy[i];
            }
            email.addCc(emailaddresscc, namecc);
        }
    }
    email.send();
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail)

Example 37 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project acs-aem-commons by Adobe-Consulting-Services.

the class EmailServiceImpl method sendEmail.

@Override
public List<InternetAddress> sendEmail(String templatePath, Map<String, String> emailParams, Map<String, DataSource> attachments, InternetAddress... recipients) {
    List<InternetAddress> failureList = new ArrayList<InternetAddress>();
    if (recipients == null || recipients.length <= 0) {
        throw new IllegalArgumentException(MSG_INVALID_RECIPIENTS);
    }
    final MailTemplate mailTemplate = this.getMailTemplate(templatePath);
    final Class<? extends Email> mailType;
    if (attachments != null && attachments.size() > 0) {
        mailType = HtmlEmail.class;
    } else {
        mailType = this.getMailType(templatePath);
    }
    final MessageGateway<Email> messageGateway = messageGatewayService.getGateway(mailType);
    for (final InternetAddress address : recipients) {
        try {
            // Get a new email per recipient to avoid duplicate attachments
            Email email = getEmail(mailTemplate, mailType, emailParams);
            email.setTo(Collections.singleton(address));
            if (attachments != null && attachments.size() > 0) {
                for (Map.Entry<String, DataSource> entry : attachments.entrySet()) {
                    ((HtmlEmail) email).attach(entry.getValue(), entry.getKey(), null);
                }
            }
            messageGateway.send(email);
        } catch (Exception e) {
            failureList.add(address);
            log.error("Error sending email to [ " + address + " ]", e);
        }
    }
    return failureList;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Email(org.apache.commons.mail.Email) HtmlEmail(org.apache.commons.mail.HtmlEmail) SimpleEmail(org.apache.commons.mail.SimpleEmail) ArrayList(java.util.ArrayList) HtmlEmail(org.apache.commons.mail.HtmlEmail) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException) LoginException(org.apache.sling.api.resource.LoginException) EmailException(org.apache.commons.mail.EmailException) DataSource(javax.activation.DataSource) MailTemplate(com.day.cq.commons.mail.MailTemplate) Map(java.util.Map)

Example 38 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail 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 39 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project ngtesting-platform by aaronchen2k.

the class MailServiceImpl method sendTemplateMail.

@Override
public void sendTemplateMail(String subject, String templateName, String toEmail, Map<String, String> map) {
    Template template = null;
    Configuration freeMarkerConfig = null;
    HtmlEmail mail = new HtmlEmail();
    try {
        String dir = getFilePath();
        freeMarkerConfig = new Configuration();
        freeMarkerConfig.setDirectoryForTemplateLoading(new File(dir));
        String file = getFileName(templateName);
        template = freeMarkerConfig.getTemplate(file, new Locale("Zh_cn"), "UTF-8");
        String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        mail.setMsg(htmlText);
        send(subject, htmlText, toEmail);
        log.error("至" + toEmail + "的邮件发送成功");
    } catch (Exception e) {
        log.debug("邮件发送错误:" + e.getMessage());
    }
}
Also used : Locale(java.util.Locale) Configuration(freemarker.template.Configuration) HtmlEmail(org.apache.commons.mail.HtmlEmail) File(java.io.File) MessagingException(javax.mail.MessagingException) Template(freemarker.template.Template)

Example 40 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project AuthMeReloaded by AuthMe.

the class TestEmailSender method sendTestEmail.

private boolean sendTestEmail(String email) {
    HtmlEmail htmlEmail;
    try {
        htmlEmail = sendMailSsl.initializeMail(email);
    } catch (EmailException e) {
        logger.logException("Failed to create email for sample email:", e);
        return false;
    }
    htmlEmail.setSubject("AuthMe test email");
    String message = "Hello there!<br />This is a sample email sent to you from a Minecraft server (" + server.getName() + ") via /authme debug mail. If you're seeing this, sending emails should be fine.";
    return sendMailSsl.sendEmail(message, htmlEmail);
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException)

Aggregations

HtmlEmail (org.apache.commons.mail.HtmlEmail)47 EmailException (org.apache.commons.mail.EmailException)24 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 Email (org.apache.commons.mail.Email)7 MultiPartEmail (org.apache.commons.mail.MultiPartEmail)6 IOException (java.io.IOException)5 InternetAddress (javax.mail.internet.InternetAddress)5 DefaultAuthenticator (org.apache.commons.mail.DefaultAuthenticator)5 Configuration (freemarker.template.Configuration)4 Template (freemarker.template.Template)4 File (java.io.File)4 List (java.util.List)4 Map (java.util.Map)4 EmailAttachment (org.apache.commons.mail.EmailAttachment)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 JobExecutionException (org.quartz.JobExecutionException)4 RenderResult (cn.bran.japid.template.RenderResult)3 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)3