이거 맘대로 되는 세상이 아니구만...

javaMail 첨부메일 보내기 본문

java

javaMail 첨부메일 보내기

바이홍 2008. 5. 26. 15:14
반응형


import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import com.top.common.util.StringUtil;
import com.top.eng.entity.Entity;
 
/**
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class consoleMail {
 
 private static final String SMTP_HOST_NAME = "smtp.naver.com";    //HOST
 private static final String SMTP_AUTH_USER = "********";              //ID
 private static final String SMTP_AUTH_PWD = "******";                  //PASSWORD
private static final String emailMsgTxt = "";                                    //내용
 private static final String emailSubjectTxt = "";                              //제목
 private static final String emailFromAddress = "AAA@GMAIL.COM"; //보내는 이 이메일주소
 
 // Add List of Email address to who email needs to be sent to
 private static final String[] emailList = {"aaa@gmail.com", "ccc@gmail.com"};   //받는 사람 메일주소
 
 public static void main(String args[]) throws Exception {
  try {
   consoleMail smtpMailSender = new consoleMail();
   smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
   System.out.println("["+StringUtil.getTodayTime()+"] : Sucessfully Sent mail to All Users");  //로그
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println("["+StringUtil.getTodayTime()+"] :  Error Sent mail to All Users   =]: "+ e.getMessage());
  }
 }
 
 public void postMail(String recipients[], String subject, String message,
   String from) throws Exception {
 
  boolean debug = false;
 
  try {
   
  //Set the host smtp address
  Properties props = new Properties();
  props.put("mail.transport.protocol", "smtp");
  props.put("mail.smtp.starttls.enable","true");
  props.put("mail.smtp.host", SMTP_HOST_NAME);
  props.put("mail.smtp.auth", "true");
 
  Authenticator auth = new SMTPAuthenticator();
  Session session = Session.getDefaultInstance(props, auth);
 
  session.setDebug(debug);
 
  // create a message
  Message msg = new MimeMessage(session);
 
  // set the from and to address
  InternetAddress addressFrom = new InternetAddress(from);
  msg.setFrom(addressFrom);
 
  InternetAddress[] addressTo = new InternetAddress[recipients.length];
  for (int i = 0; i < recipients.length; i++) {
   addressTo[i] = new InternetAddress(recipients[i]);
  }
  msg.setRecipients(Message.RecipientType.TO, addressTo);
 

  //메일 제목///
  subject = StringUtil.getDate( StringUtil.getOffsetDateToDate(StringUtil.getToday("yyyyMMdd"), -1), "yyyy-MM-dd")+"일 정보입니다.";
  msg.setSubject(MimeUtility.encodeText(subject,"EUC-KR","B"));
 

  //메일 내용
  message = "<html><body><font color=green>"
    + "<b>★ 머꼬<b>"
    + 머꼬"
    + " 머꼬
    + "</font></body></html>";
  MimeBodyPart mbp_cont = new MimeBodyPart();
  mbp_cont.setContent(message, "text/html; charset=euc-kr");   //한글 과 html사용  

  String fileaddress = "파일이 있는 절대경로";                           //파일이 있는 절대경로
  MimeBodyPart mbp = new MimeBodyPart();
  FileDataSource fds = new FileDataSource(fileaddress);
  mbp.setDataHandler(new DataHandler(fds));
  mbp.setFileName(MimeUtility.encodeText(fds.getName(),"EUC-KR","B"));   //한글파일네임을 적기 위해서
 
  Multipart mp = new MimeMultipart();
  mp.addBodyPart(mbp_cont);
  mp.addBodyPart(mbp);
  msg.setContent(mp);
 
  Transport.send(msg);
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
 }
 
 /**
  * SimpleAuthenticator is used to do simple authentication when the SMTP
  * server requires it.
  */
 private class SMTPAuthenticator extends Authenticator {
 
  public PasswordAuthentication getPasswordAuthentication() {
   String username = SMTP_AUTH_USER;
   String password = SMTP_AUTH_PWD;
   return new PasswordAuthentication(username, password);
  }
 }
}

반응형
Comments