public static Configuration getConfiguration()
      throws SQLException, TemplateModelException, IOException {
    if (cfg == null) {
      cfg = new Configuration(new Version("2.3.23"));
      cfg.setDefaultEncoding("UTF-8");
      cfg.setSharedVariable(
          "portalAddr", SystemConfigDao.getSystemConfig(Config.TemplateInfo.CONFIG_ID));

      // 获取资源路径
      String classPath = cfg.getClass().getResource("/").getFile().toString();
      cfg.setDirectoryForTemplateLoading(new File(classPath));
    }

    return cfg;
  }
Ejemplo n.º 2
0
  public static void sendMail(String mail, MailContent mailContent) throws Exception {

    Properties props = new Properties();
    //		try {
    //			props.load(new FileInputStream(new File("settings.properties")));
    //		} catch (FileNotFoundException e1) {
    //			e1.printStackTrace();
    //		} catch (IOException e1) {
    //			e1.printStackTrace();
    //		}

    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session =
        Session.getDefaultInstance(
            props,
            new Authenticator() {

              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("*****@*****.**", "mycity@123");
              }
            });

    try {
      String a = mail;
      String b = mailContent.getTopp();
      String c = mailContent.getBody1();
      String d = mailContent.getBody2();
      Message message = new MimeMessage(session);
      //			MimeUtility.encodeText(subject, "utf-8", "B")
      message.setFrom(new InternetAddress("*****@*****.**"));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail));
      message.setSubject(MimeUtility.encodeText(mailContent.getSubJect(), "utf-8", "B"));

      BodyPart body = new MimeBodyPart();
      // freemarker stuff.
      Configuration cfg = new Configuration();
      FileTemplateLoader ftl1 =
          new FileTemplateLoader(
              new File(ServletActionContext.getServletContext().getRealPath("/")));
      cfg.setClassForTemplateLoading(cfg.getClass(), "/");
      cfg.setTemplateLoader(ftl1);
      Template template = null;
      template =
          cfg.getTemplate("resources/mycity_cms/templates/mail/html-mail-template.html", "UTF-8");

      Map<String, String> rootMap = new HashMap<String, String>();
      rootMap.put("to", mailContent.getTopp());
      rootMap.put("body1", mailContent.getBody1());
      rootMap.put("body2", mailContent.getBody2());
      Writer out = new StringWriter();
      template.process(rootMap, out);
      // freemarker stuff ends.

      /* you can add html tags in your text to decorate it. */
      //			body.setContent(out.toString(),	"text/html");
      body.setContent(out.toString(), "text/html; charset=UTF-8");

      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(body);
      // cfg
      //			body = new MimeBodyPart();
      //
      //			String filename = "hello.txt";
      //			DataSource source = new FileDataSource(filename);
      //			body.setDataHandler(new DataHandler(source));
      //			body.setFileName(filename);
      //			multipart.addBodyPart(body);
      //
      message.setContent(multipart, "text/html; charset=UTF-8;");
      //
      Transport.send(message);

    } catch (MessagingException e) {
      LogUtility.logError(e, e.getMessage());
    }

    System.out.println("Done....");
  }