Search in sources :

Example 46 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project SpringStepByStep by JavaProgrammerLB.

the class JavaMailDemo method sendEmailWithHtml.

// @Test
public void sendEmailWithHtml() {
    try {
        // Create the email message
        HtmlEmail email = new HtmlEmail();
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setSSLOnConnect(true);
        email.setFrom("username", "LIUBEI");
        email.addTo("admin@yitianyigexiangfa.com", "ADMIN");
        email.setAuthentication("username", "password");
        // embed the image and get the content id
        URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
        String cid = email.embed(url, "Apache logo");
        // set the html message
        email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>");
        // set the alternative message
        email.setTextMsg("Your email client does not support HTML messages");
        // send the email
        email.send();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (EmailException e) {
        e.printStackTrace();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ImageHtmlEmail(org.apache.commons.mail.ImageHtmlEmail) HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException) URL(java.net.URL)

Example 47 with HtmlEmail

use of org.apache.commons.mail.HtmlEmail in project dal by ctripcorp.

the class GenTaskResource method taskApproveOperationForEmail.

@GET
@Path("taskApproveOperationForEmail")
@Produces(MediaType.APPLICATION_JSON)
public String taskApproveOperationForEmail(@Context HttpServletRequest request, @QueryParam("taskId") int taskId, @QueryParam("taskType") String taskType, @QueryParam("approveFlag") int approveFlag, @QueryParam("approveMsg") String approveMsg) throws Exception {
    try {
        String userNo = RequestUtil.getUserNo(request);
        LoginUser user = BeanGetter.getDaoOfLoginUser().getUserByNo(userNo);
        if (user == null) {
            return "please login fisrt.";
        }
        ApproveTask task = haveApprovePermision(user.getId(), taskId, taskType);
        if (task == null) {
            return "Dao have been approved.";
        }
        List<GenTaskByTableViewSp> tableViewSpTasks = new ArrayList<>();
        List<GenTaskBySqlBuilder> autoTasks = new ArrayList<>();
        List<GenTaskByFreeSql> sqlTasks = new ArrayList<>();
        if ("table_view_sp".equalsIgnoreCase(taskType)) {
            BeanGetter.getDaoByTableViewSp().updateTask(taskId, approveFlag, approveMsg);
            BeanGetter.getApproveTaskDao().deleteApproveTaskByTaskIdAndType(taskId, taskType);
            tableViewSpTasks.add(BeanGetter.getDaoByTableViewSp().getTasksByTaskId(taskId));
        } else if ("auto".equalsIgnoreCase(taskType)) {
            BeanGetter.getDaoBySqlBuilder().updateTask(taskId, approveFlag, approveMsg);
            BeanGetter.getApproveTaskDao().deleteApproveTaskByTaskIdAndType(taskId, taskType);
            autoTasks.add(BeanGetter.getDaoBySqlBuilder().getTasksByTaskId(taskId));
        } else if ("sql".equalsIgnoreCase(taskType)) {
            BeanGetter.getDaoByFreeSql().updateTask(taskId, approveFlag, approveMsg);
            BeanGetter.getApproveTaskDao().deleteApproveTaskByTaskIdAndType(taskId, taskType);
            sqlTasks.add(BeanGetter.getDaoByFreeSql().getTasksByTaskId(taskId));
        }
        java.util.Collections.sort(tableViewSpTasks);
        java.util.Collections.sort(autoTasks);
        java.util.Collections.sort(sqlTasks);
        LoginUser noticeUsr = BeanGetter.getDaoOfLoginUser().getUserById(task.getCreate_user_id());
        VelocityContext context = GenUtils.buildDefaultVelocityContext();
        context.put("standardDao", tableViewSpTasks);
        context.put("autoDao", autoTasks);
        context.put("sqlDao", sqlTasks);
        String msg = "你好," + noticeUsr.getUserName() + ":<br/>&nbsp;&nbsp;你提交的DAO已审批,审批";
        if (approveFlag == 2) {
            msg += "通过。";
        } else {
            msg += "未通过。";
        }
        if (approveMsg != null) {
            msg += "<br/>&nbsp;&nbsp;审批意见:" + approveMsg;
        }
        context.put("msg", msg);
        String mailMsg = GenUtils.mergeVelocityContext(context, "templates/approval/approveResult.tpl");
        HtmlEmail email = new HtmlEmail();
        email.setHostName(Configuration.get("email_host_name"));
        email.setAuthentication(Configuration.get("email_user_name"), Configuration.get("email_password"));
        try {
            email.addTo(noticeUsr.getUserEmail());
            email.setFrom(user.getUserEmail(), user.getUserName());
            email.setSubject("Codegen DAO 审批结果通知");
            email.setHtmlMsg(mailMsg);
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
        return "Success Approved.";
    } catch (Throwable e) {
        LoggerManager.getInstance().error(e);
        throw e;
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) 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