Пример #1
0
  /**
   * HTML 형식으로 된 메일을 보낸다.
   *
   * @param mailTo 받는사람 주소
   * @param mailFrom 보내는 사람 주소
   * @param subject 메일 제목
   * @param content HTML 메일 컨텐츠
   * @throws MessagingException
   * @throws UnsupportedEncodingException
   */
  public void sendHtmlMail(String mailTo, String mailFrom, String subject, String content)
      throws MessagingException, UnsupportedEncodingException {

    // properties object 얻기
    Properties props = System.getProperties();

    // SMTP host property 정의
    // props.put(smtpProps, smtpHost);
    props.put(SMTP_PROPS, SMTP_HOST);
    log.debug("[" + className + "]-SMTP_HOST =" + SMTP_HOST);

    // javax mail 을 디버깅 모드로 설정
    props.put(SMTP_DEBUG, SMTP_TRUE);
    log.debug("[" + className + "]-SMTP_TRUE =" + SMTP_TRUE);

    if (MarConstants.getServerName() == MarConstants.SERVER_LOCAL) {
      // gmail smtp 서비스 포트 설정
      props.put(SMTP_PORT, SMTP_PORTV2);
      props.put("type", "javax.mail.Session"); // 해도 그만 안해도 그만
      props.put("auth", "Application"); // 해도 그만 안해도 그만
      log.debug("[" + className + "]-SMTP_PORTV2 =" + SMTP_PORTV2);

      // 로그인할때 Transport Layer Security (TLS) 를 사용할것인지 설정 gmail 에선 tls가 필수가 아니므로 해도그만 안해도 그만이다.
      props.put(SMTP_TLS, SMTP_TRUE);
      props.put(SMTP_TRANS, SMTP_PROTOCOL); // 프로토콜 설정
      log.debug("[" + className + "]-filename =" + SMTP_TRUE);
      log.debug("[" + className + "]-filename =" + SMTP_PROTOCOL);

      // gmail 인증용  Secure Sockets Layer (SSL) 설정
      // gmail 에서 인증? 사용해주므로 요건 안해주면 안됨
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    } else {
      props.put(SMTP_PORT, SMTP_PORTV);
      log.debug("[" + className + "]-filename =" + SMTP_PORTV);
    }

    // session 얻기
    Session session = null;

    if (MarConstants.getServerName() == MarConstants.SERVER_LOCAL) {
      // smtp 인증 을 설정
      props.put("mail.smtp.auth", "true");

      Authenticator auth =
          new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
              // return new PasswordAuthentication("*****@*****.**", "vlffldzm2102");
              return new PasswordAuthentication("*****@*****.**", "");
            }
          };
      // session 얻기
      session = Session.getDefaultInstance(props, auth);
      // log.debug("["+className+"]-session ="+session);
    } else {
      session = Session.getDefaultInstance(props, null);
      // log.debug("["+className+"]-session ="+session);
    }

    if (log.isDebugEnabled()) {
      session.setDebug(true);
    }

    log.debug("[" + className + "]-mailFrom =" + mailFrom);
    log.debug("[" + className + "]-mailTo =" + mailTo);

    // 새로운 message object 생성
    MimeMessage message = new MimeMessage(session);
    // MimeUtility.encodeText(subject, "KSC5601", "B");
    message.setHeader("Content-Type", "text/html; charset=" + MAIL_ENCODING);
    message.setHeader("Content-Transfer-Encoding", MAIL_ENCODING64);
    message.setSubject(subject);
    // 일반적인 message object 채우기
    // message.setContent(content, "text/html; charset=" + MAIL_ENCODING);
    // message.setContent(content, "text/html; charset=UTF-8");
    message.setContent(content, "text/html;charset=UTF-8");
    message.setFrom(new InternetAddress(mailFrom));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
    // message 보내기

    Transport.send(message);
    log.debug("[" + className + "]-Send Mail Success");
  }
Пример #2
0
@SuppressWarnings("unused")
public class MailServer {

  private static Logger log = Logger.getLogger("A9");
  private static String className = "MailServer";

  private String SMTP_HOST = MarConstants.getValue("mar.config.smtpHost");
  private String SMTP_PROPS = MarConstants.getValue("mar.config.smtpProps");
  private String SMTP_DEBUG = MarConstants.getValue("mar.config.smtpDebug");
  private String SMTP_TRUE = MarConstants.getValue("mar.config.smtpTrue");
  private String SMTP_PORT = MarConstants.getValue("mar.config.smtpPort");
  private String SMTP_PORTV = MarConstants.getValue("mar.config.smtpPortV");
  private String SMTP_PORTV2 = MarConstants.getValue("mar.config.smtpPortV2");
  private String SMTP_TLS = MarConstants.getValue("mar.config.smtpTls");
  private String SMTP_TRANS = MarConstants.getValue("mar.config.smtpTrans");
  private String SMTP_PROTOCOL = MarConstants.getValue("mar.config.smtpProtocol");
  private String HTML_PATH = MarConstants.getValue("mar.config.htmlpath");

  // private String HTML_FILE_PATH    = MarConstants.getValue("mar.config.html.filepath");
  // private String SERVER_PATH    = MarConstants.getValue("mar.config.server.path");

  // private String smtpHost     = "smtp.feelingk.com";
  // private String smtpProps    = "mail.smtp.host";

  static String MAIL_ENCODING = "UTF-8";
  static String MAIL_ENCODING64 = "base64";
  /**
   * HTML 형식으로 된 메일을 보낸다.
   *
   * @param mailTo 받는사람 주소
   * @param mailFrom 보내는 사람 주소
   * @param subject 메일 제목
   * @param content HTML 메일 컨텐츠
   * @throws MessagingException
   * @throws UnsupportedEncodingException
   */
  public void sendHtmlMail(String mailTo, String mailFrom, String subject, String content)
      throws MessagingException, UnsupportedEncodingException {

    // properties object 얻기
    Properties props = System.getProperties();

    // SMTP host property 정의
    // props.put(smtpProps, smtpHost);
    props.put(SMTP_PROPS, SMTP_HOST);
    log.debug("[" + className + "]-SMTP_HOST =" + SMTP_HOST);

    // javax mail 을 디버깅 모드로 설정
    props.put(SMTP_DEBUG, SMTP_TRUE);
    log.debug("[" + className + "]-SMTP_TRUE =" + SMTP_TRUE);

    if (MarConstants.getServerName() == MarConstants.SERVER_LOCAL) {
      // gmail smtp 서비스 포트 설정
      props.put(SMTP_PORT, SMTP_PORTV2);
      props.put("type", "javax.mail.Session"); // 해도 그만 안해도 그만
      props.put("auth", "Application"); // 해도 그만 안해도 그만
      log.debug("[" + className + "]-SMTP_PORTV2 =" + SMTP_PORTV2);

      // 로그인할때 Transport Layer Security (TLS) 를 사용할것인지 설정 gmail 에선 tls가 필수가 아니므로 해도그만 안해도 그만이다.
      props.put(SMTP_TLS, SMTP_TRUE);
      props.put(SMTP_TRANS, SMTP_PROTOCOL); // 프로토콜 설정
      log.debug("[" + className + "]-filename =" + SMTP_TRUE);
      log.debug("[" + className + "]-filename =" + SMTP_PROTOCOL);

      // gmail 인증용  Secure Sockets Layer (SSL) 설정
      // gmail 에서 인증? 사용해주므로 요건 안해주면 안됨
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    } else {
      props.put(SMTP_PORT, SMTP_PORTV);
      log.debug("[" + className + "]-filename =" + SMTP_PORTV);
    }

    // session 얻기
    Session session = null;

    if (MarConstants.getServerName() == MarConstants.SERVER_LOCAL) {
      // smtp 인증 을 설정
      props.put("mail.smtp.auth", "true");

      Authenticator auth =
          new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
              // return new PasswordAuthentication("*****@*****.**", "vlffldzm2102");
              return new PasswordAuthentication("*****@*****.**", "");
            }
          };
      // session 얻기
      session = Session.getDefaultInstance(props, auth);
      // log.debug("["+className+"]-session ="+session);
    } else {
      session = Session.getDefaultInstance(props, null);
      // log.debug("["+className+"]-session ="+session);
    }

    if (log.isDebugEnabled()) {
      session.setDebug(true);
    }

    log.debug("[" + className + "]-mailFrom =" + mailFrom);
    log.debug("[" + className + "]-mailTo =" + mailTo);

    // 새로운 message object 생성
    MimeMessage message = new MimeMessage(session);
    // MimeUtility.encodeText(subject, "KSC5601", "B");
    message.setHeader("Content-Type", "text/html; charset=" + MAIL_ENCODING);
    message.setHeader("Content-Transfer-Encoding", MAIL_ENCODING64);
    message.setSubject(subject);
    // 일반적인 message object 채우기
    // message.setContent(content, "text/html; charset=" + MAIL_ENCODING);
    // message.setContent(content, "text/html; charset=UTF-8");
    message.setContent(content, "text/html;charset=UTF-8");
    message.setFrom(new InternetAddress(mailFrom));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
    // message 보내기

    Transport.send(message);
    log.debug("[" + className + "]-Send Mail Success");
  }

  public String SaveFile(String fName, String body) throws IOException {
    String path = "/appHtml/" + fName + ".html";
    log.debug("[" + className + "]-path =" + path);
    File f = new File(HTML_PATH + fName + ".html");
    File f1 = new File(HTML_PATH + fName.substring(0, 8)); // 생성할 디렉토리 경로
    if (!f1.exists()) {
      f1.mkdir();
    }
    FileWriter fw = new FileWriter(f);
    BufferedWriter bw = new BufferedWriter(fw, 1024);
    PrintWriter pw = new PrintWriter(bw);

    pw.println(body);

    pw.close();

    return path;
  }
}