| 18 | import java.util.UUID; |
| 19 | |
| 20 | public class NewSendEmail { |
| 21 | |
| 22 | public static Integer sendEmail(EmailConfig emailConfig, String username, String uid, String toEmail, JSONObject jsonObject) { |
| 23 | |
| 24 | Properties props = new Properties(); |
| 25 | props.put("mail.smtp.auth", "true"); |
| 26 | props.put("mail.smtp.ssl.enable", "true"); |
| 27 | props.put("mail.debug", "false"); |
| 28 | props.put("mail.transport.protocol", "smtp"); |
| 29 | props.put("mail.debug", "false"); |
| 30 | props.put("mail.smtp.timeout", "20000"); |
| 31 | props.put("mail.smtp.port", emailConfig.getPort());//465 25 |
| 32 | props.put("mail.smtp.host", emailConfig.getEmailurl()); |
| 33 | // 配置一次即可,可以配置为静态方法 |
| 34 | // OhMyEmail.config(OhMyEmail.SMTP_QQ(false), "xxxx@qq.com", "your@password"); |
| 35 | OhMyEmail.config(props, emailConfig.getEmails(), emailConfig.getEmailkey()); |
| 36 | |
| 37 | String webname=jsonObject.getString("webname"); |
| 38 | String domain = jsonObject.getString("domain"); |
| 39 | try { |
| 40 | //生成模板 |
| 41 | PebbleEngine engine = new PebbleEngine.Builder().build(); |
| 42 | ClassPathResource classPathResource = new ClassPathResource("emailTemplate/emailRegister.html"); |
| 43 | PebbleTemplate compiledTemplate = engine.getTemplate(classPathResource.getPath()); |
| 44 | Map<String, Object> context = new HashMap<>(); |
| 45 | context.put("username", username); |
| 46 | context.put("webname", webname); |
| 47 | context.put("url", domain+"/user/activation?activation="+uid+"&username=" + username ); |
| 48 | Writer writer = new StringWriter(); |
| 49 | compiledTemplate.evaluate(writer, context); |
| 50 | String output = writer.toString(); |
| 51 | OhMyEmail.subject(webname+"账号激活") |
| 52 | .from(webname) |
| 53 | .to(toEmail) |
| 54 | .html(output) |
| 55 | .send(); |
| 56 | return 1; |
| 57 | } catch (Exception e) { |
| 58 | e.printStackTrace(); |
| 59 | return 0; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static Msg sendTestEmail(EmailConfig emailConfig, String toEmail) { |
| 64 | Msg msg = new Msg(); |
| 65 | Properties props = new Properties(); |
| 66 | try { |
| 67 | props.put("mail.smtp.auth", "true"); |
| 68 | props.put("mail.smtp.ssl.enable", "true"); |
| 69 | props.put("mail.debug", "false"); |
| 70 | props.put("mail.transport.protocol", "smtp"); |
| 71 | props.put("mail.debug", "false"); |
| 72 | props.put("mail.smtp.timeout", "20000"); |
| 73 | props.put("mail.smtp.port", emailConfig.getPort());//465 25 |
| 74 | props.put("mail.smtp.host", emailConfig.getEmailurl()); |
| 75 | OhMyEmail.config(props, emailConfig.getEmails(), emailConfig.getEmailkey()); |
| 76 | String webname="Hellohao图像托管程序"; |
| 77 | OhMyEmail.subject("Hellohao图像托管程序邮箱配置测试") |
nothing calls this directly
no outgoing calls
no test coverage detected