MailHelp.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package com.qxgmat.help;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.nuliji.tools.exception.ParameterException;
  4. import com.nuliji.tools.third.sendcloud.SendCloudMail;
  5. import com.nuliji.tools.third.sendcloud.SendCloudSms;
  6. import com.qxgmat.data.constants.SessionKey;
  7. import com.qxgmat.dto.SmsSessionDto;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.core.io.FileSystemResource;
  13. import org.springframework.stereotype.Service;
  14. import javax.servlet.http.HttpSession;
  15. import java.util.Date;
  16. import java.util.Map;
  17. /**
  18. * Created by GaoJie on 2017/11/3.
  19. */
  20. @Service
  21. public class MailHelp {
  22. private static final Logger logger = LoggerFactory.getLogger(MailHelp.class);
  23. private SendCloudMail mail;
  24. @Value("${third.sendcloud.from}")
  25. private String from;
  26. @Value("${third.sendcloud.fromName}")
  27. private String fromName;
  28. @Autowired
  29. private void getSms(@Value("${third.sendcloud.apiUser}") String apiUser,
  30. @Value("${third.sendcloud.apiKey}") String apiKey) {
  31. this.mail = new SendCloudMail(apiUser, apiKey);
  32. }
  33. public boolean sendBaseMail(String email, String subject, String body, Map<String, String> params){
  34. // 替换模版
  35. body = replaceBody(body, params);
  36. SendCloudMail.Response response = mail.sendMail(email, subject, body, from, fromName, null);
  37. return response.getResult();
  38. }
  39. public boolean sendAttachMail(String email, String subject, String body, Map<String, String> params, String filePath){
  40. // 替换模版
  41. body = replaceBody(body, params);
  42. SendCloudMail.Response response = mail.sendMail(email, subject, body, from, fromName, new FileSystemResource(filePath));
  43. return response.getResult();
  44. }
  45. private String replaceBody(String body, Map<String, String> params){
  46. for(String key :params.keySet()){
  47. body = body.replaceAll("{"+key+"}", params.get(key));
  48. }
  49. return body;
  50. }
  51. }