본문 바로가기
프로그래밍/java

org.apache.commons.mail 메일 보내기

by RPoint 2012. 8. 16.

package eont.mail;



import org.apache.commons.mail.*;

import org.apache.log4j.Logger;

import org.springframework.stereotype.Service;


import java.util.*;

import javax.mail.*;

import java.io.*;

import java.awt.image.*;

import javax.imageio.*;

import javax.imageio.ImageIO.*;

import eont.mail.WebImage;

import eont.xml.eontConfig;



public class SendHtmlEmail {


 static final Logger logger = Logger.getLogger("email");

  public SendHtmlEmail() throws Exception{

    try{

        System.out.println("SendHtmlEmail initialize");

    }catch (Exception e){

        System.out.println(e.toString());

    }

  }


 

  public static void Send(String mailsubject,

                          String mailto,

                          String mailto_name,

                          String mailfrom,

                          String mailsender,

                          String content,

                          String attach_name,

                          String save_path

                          ) throws EmailException, MessagingException, UnsupportedEncodingException {

  

    HtmlEmail htmlEmail = null;

    Date currDate = new Date();

    

 

    logger.debug("mailsubject  : "+mailsubject);

    logger.debug("mailfrom     : "+mailfrom); // 보내는 사람

    logger.debug("mailto       : "+mailto); // 받는 사람    

    logger.debug("mailsender   : "+mailsender);

    


    htmlEmail = new HtmlEmail() ;


    String SendingHtml = content;

   

    

   

      

      try {

        htmlEmail.setCharset("euc-kr");

        htmlEmail.setHostName("0.0.0.0"); // 아이피 

       

        

        htmlEmail.setSmtpPort(25);

        htmlEmail.setAuthenticator(new DefaultAuthenticator("아이디", "패스워드"));


        htmlEmail.setDebug(true);

        htmlEmail.setFrom(mailfrom, mailsender);

        htmlEmail.addTo(mailto, mailto_name);

        


        htmlEmail.setSubject(mailsubject);

        Properties props = new Properties();

        props.put("mail.smtp.auth", "true");

        props.put("mail.debug", "true");

        props.put("mail.smtp.port", "25");

        props.put("mail.smtp.socketFactory.port", "25");

        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        props.put("mail.smtp.socketFactory.fallback", "false");

        props.put("mail.smtp.starttls.enable", "true");

        Session sess = Session.getDefaultInstance(props, null);

       

        /* 첨부파일 */

        if(attach_name !=null 

        && save_path !=null 

        && !attach_name.equals("")

        &&  !save_path.equals("") )

        {

       

        eontConfig tc = new eontConfig();          

        save_path = tc.getConfigValue("AttachPathOutBound")+save_path;

       

       EmailAttachment attachment = new EmailAttachment();

       attachment.setPath(save_path); // 파일경로

       attachment.setDisposition(EmailAttachment.ATTACHMENT);

       attachment.setDescription("");

       attachment.setName(attach_name); // 실제 파일 이름

       attachment.setName(new String(attach_name.getBytes("euc-kr"), "latin1")); // 한글 첨부파일 깨짐..

       

       htmlEmail.attach(attachment);

        }

        

       

        htmlEmail.setHtmlMsg(SendingHtml);

        htmlEmail.setSentDate(currDate);

        htmlEmail.send();

    

      }

      catch (EmailException ee) {

        //System.out.println(ee.toString());

      }finally{

        String fn = "receipt_email/receipt."+mailto+".html";

        File f = new File(fn);

        if(!f.delete())

          logger.debug("fail delete file : receipt_email/receipt."+mailto+".html");

     }  

   }

 }


댓글