use of org.apache.commons.mail.HtmlEmail in project dal by ctripcorp.
the class GenTaskResource method approveTask.
@POST
@Path("approveTask")
@Produces(MediaType.APPLICATION_JSON)
public Status approveTask(@Context HttpServletRequest request, @FormParam("taskId") String taskId, @FormParam("taskType") String taskType, @FormParam("userId") int userId) throws Exception {
try {
Status status = Status.ERROR();
LoginUser approver = BeanGetter.getDaoOfLoginUser().getUserById(userId);
if (approver == null) {
return status;
}
int len = request.getRequestURI().length();
String host = request.getRequestURL().toString();
host = host.substring(0, len);
String approveUrl = host + "rest/task/taskApproveOperationForEmail?";
String myApprovelTaskUrl = host + "eventmanage.jsp";
String[] taskIds = taskId.split(",");
String[] taskTypes = taskType.split(",");
List<GenTaskByTableViewSp> tableViewSpTasks = new ArrayList<>();
List<GenTaskBySqlBuilder> autoTasks = new ArrayList<>();
List<GenTaskByFreeSql> sqlTasks = new ArrayList<>();
String userNo = RequestUtil.getUserNo(request);
LoginUser user = BeanGetter.getDaoOfLoginUser().getUserByNo(userNo);
ApproveTask at = new ApproveTask();
at.setApprove_user_id(approver.getId());
at.setCreate_user_id(user.getId());
at.setCreate_time(new Timestamp(System.currentTimeMillis()));
for (int i = 0; i < taskIds.length; i++) {
int id = Integer.parseInt(taskIds[i]);
String type = taskTypes[i].trim();
if ("table_view_sp".equalsIgnoreCase(type)) {
GenTaskByTableViewSp task = BeanGetter.getDaoByTableViewSp().getTasksByTaskId(id);
tableViewSpTasks.add(task);
at.setTask_id(task.getId());
at.setTask_type("table_view_sp");
BeanGetter.getApproveTaskDao().insertApproveTask(at);
} else if ("auto".equalsIgnoreCase(type)) {
GenTaskBySqlBuilder task = BeanGetter.getDaoBySqlBuilder().getTasksByTaskId(id);
autoTasks.add(task);
at.setTask_id(task.getId());
at.setTask_type("auto");
BeanGetter.getApproveTaskDao().insertApproveTask(at);
} else if ("sql".equalsIgnoreCase(type)) {
GenTaskByFreeSql task = BeanGetter.getDaoByFreeSql().getTasksByTaskId(id);
sqlTasks.add(task);
at.setTask_id(task.getId());
at.setTask_type("sql");
BeanGetter.getApproveTaskDao().insertApproveTask(at);
}
}
java.util.Collections.sort(tableViewSpTasks);
java.util.Collections.sort(autoTasks);
java.util.Collections.sort(sqlTasks);
VelocityContext context = GenUtils.buildDefaultVelocityContext();
context.put("standardDao", tableViewSpTasks);
context.put("autoDao", autoTasks);
context.put("sqlDao", sqlTasks);
context.put("approveUrl", approveUrl);
context.put("myApprovelTaskUrl", myApprovelTaskUrl);
context.put("approveUser", approver.getUserName());
String msg = GenUtils.mergeVelocityContext(context, "templates/approval/approveDao.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(approver.getUserEmail());
email.setFrom(user.getUserEmail(), user.getUserName());
email.setSubject("Codegen DAO 审批");
email.setHtmlMsg(msg);
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
return Status.OK();
} catch (Throwable e) {
LoggerManager.getInstance().error(e);
Status status = Status.ERROR();
status.setInfo(e.getMessage());
return status;
}
}
use of org.apache.commons.mail.HtmlEmail in project dal by ctripcorp.
the class GenTaskResource method taskApproveOperation.
@GET
@Path("taskApproveOperation")
@Produces(MediaType.APPLICATION_JSON)
public Status taskApproveOperation(@Context HttpServletRequest request, @QueryParam("taskId") int taskId, @QueryParam("taskType") String taskType, @QueryParam("approveFlag") int approveFlag, @QueryParam("approveMsg") String approveMsg) throws Exception {
try {
Status status = Status.ERROR();
String userNo = RequestUtil.getUserNo(request);
LoginUser user = BeanGetter.getDaoOfLoginUser().getUserByNo(userNo);
if (user == null) {
status.setInfo("please login fisrt.");
return status;
}
ApproveTask task = haveApprovePermision(user.getId(), taskId, taskType);
if (task == null) {
status.setInfo("you don't have permision to approve this task.");
return status;
}
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 Status.OK();
} catch (Throwable e) {
LoggerManager.getInstance().error(e);
Status status = Status.ERROR();
status.setInfo(e.getMessage());
return status;
}
}
use of org.apache.commons.mail.HtmlEmail in project weicoder by wdcode.
the class EmailApache method sendHtmlEmail.
/**
* 发送HTML格式带附件的邮件
* @param to 发送地址
* @param subject 邮件标题
* @param msg 邮件内容
* @param attach 附件
*/
protected final void sendHtmlEmail(String[] to, String subject, String msg, String attach) {
// 实例化邮件操作类
HtmlEmail email = new HtmlEmail();
// 添加附件
setAttachment(email, attach);
// 发送Email
sendEmail(email, to, subject, msg);
}
use of org.apache.commons.mail.HtmlEmail 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;
}
use of org.apache.commons.mail.HtmlEmail in project acs-aem-commons by Adobe-Consulting-Services.
the class EmailServiceImplTest method testSendEmailAttachment.
@Test
public final void testSendEmailAttachment() throws Exception {
final String expectedMessage = "This is just a message";
final String expectedSenderName = "John Smith";
final String expectedSenderEmailAddress = "john@smith.com";
String attachment = "This is a attachment.";
String attachmentName = "attachment.txt";
// Subject is provided inside the HtmlTemplate directly
final String expectedSubject = "Greetings";
final Map<String, String> params = new HashMap<String, String>();
params.put("message", expectedMessage);
params.put("senderName", expectedSenderName);
params.put("senderEmailAddress", expectedSenderEmailAddress);
final String recipient = "upasanac@acs.com";
Map<String, DataSource> attachments = new HashMap();
attachments.put(attachmentName, new ByteArrayDataSource(attachment, "text/plain"));
ArgumentCaptor<HtmlEmail> captor = ArgumentCaptor.forClass(HtmlEmail.class);
List<String> failureList = emailService.sendEmail(emailTemplateAttachmentPath, params, attachments, recipient);
verify(messageGatewayHtmlEmail, times(1)).send(captor.capture());
assertEquals(expectedSenderEmailAddress, captor.getValue().getFromAddress().getAddress());
assertEquals(expectedSenderName, captor.getValue().getFromAddress().getPersonal());
assertEquals(expectedSubject, captor.getValue().getSubject());
assertEquals(recipient, captor.getValue().getToAddresses().get(0).toString());
Method getContainer = captor.getValue().getClass().getSuperclass().getDeclaredMethod("getContainer");
getContainer.setAccessible(true);
MimeMultipart mimeMultipart = (MimeMultipart) getContainer.invoke(captor.getValue());
getContainer.setAccessible(false);
assertEquals(attachment, mimeMultipart.getBodyPart(0).getContent().toString());
// If email is sent to the recipient successfully, the response is an empty failureList
assertTrue(failureList.isEmpty());
}
Aggregations