在Spring MVC框架下利用RESTful API和MongoDB实现用户留言与邮件反馈
/** * SendFeedbackEmailHandler.java*
* @author Zero
*/
public> private static final Logger LOG = LoggerFactory.getLogger(SendFeedbackEmailHandler.class);
private static final String SUCCESS_RESULT = "1";
private static final String SUBJECT = "有新的待处理留言信息";
//TEMPLATE为反馈邮件发送模板
private static final String TEMPLATE = "template/zh_feedback_root_notice.html";
private transient UserFeedbackRepository userFeedbackRepository = BeanProvider.getBean(UserFeedbackRepository.class);
private transient MailRepository mailRepository = BeanProvider.getBean(MailRepository.class);
private UserFeedbackFormDto formDto;
public SendFeedbackEmailHandler(UserFeedbackFormDto formDto) {
this.formDto = formDto;
}
public String handle() {
//将留言写入数据库
UserFeedback userFeedback = newUserFeedback();
userFeedbackRepository.saveUserFeedback(userFeedback);
//记录日志
AuditLog.create("添加留言(fullName = " + userFeedback.fullName() + ", uuid = " + userFeedback.uuid() + ")");
LOG.debug("{}|Save Feedback : {}", WebUtils.getIp(), userFeedback);
//邮件反馈
sendNoticeEmailToRoot(userFeedback);
return SUCCESS_RESULT;
}
private UserFeedback newUserFeedback() {
return new UserFeedback()
.fullName(formDto.getFullName())
.position(formDto.getPosition())
.phone(formDto.getPhone())
.contentType(formDto.getContentType())
.content(formDto.getContent());
}
//发送邮件的具体实现
protected void sendNoticeEmailToRoot(UserFeedback userFeedback) {
final> //rootEmailAddress
List<String> emails => if (null == emails || emails.size() < 1) {
LOG.debug("{}| Not Found EmailAddress: {}", SecurityUtils.username(), emails);
return;
}
Map<String, Object> params = new HashMap<>();
params.put("fullName", userFeedback.fullName());
params.put("position", userFeedback.position());
params.put("phone", userFeedback.phone());
params.put("contentType", userFeedback.contentType().getLabel());
params.put("content", userFeedback.content());
params.put("hostUrl", Holder.host() + "login");
STRender render = new STRender(TEMPLATE, params);
String content = render.render();
for (String email : emails) {
//MailTransmitter为邮件发送器
MailTransmitter mailTransmitter = new MailTransmitter().subject(SUBJECT).to(email).content(content);
final MailTransmitResult result = mailRepository.sendMail(mailTransmitter);
AuditLog.createMailLog(email, SUBJECT + "{发送结果:" + result + "}", "来自 " + userFeedback.fullName() + " 的" + userFeedback.contentType().getLabel() + "留言信息需要处理");
}
}
}
页:
[1]