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();
}
}
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/> 你提交的DAO已审批,审批";
if (approveFlag == 2) {
msg += "通过。";
} else {
msg += "未通过。";
}
if (approveMsg != null) {
msg += "<br/> 审批意见:" + 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;
}
}
Aggregations