반응형
public boolean sendMail(String filePath,String host, String receiver, String sender, String subject, String contents, InquireFile[] files){
boolean flag = false;
Properties prop = new Properties();
prop.put("mail.smtp.host" , host);
Session mailSession = Session.getDefaultInstance(prop, null);
try{
InternetAddress from = new InternetAddress(sender);
Message msg = new MimeMessage(mailSession);
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
msg.setSubject(subject);
Multipart multi = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(contents, "text/html; charset=ms949");
multi.addBodyPart(mbp);
if(files != null){
for(int i = 0; i < files.length; i++){
if(files[i] == null ) continue;
DataSource fds = new FileDataSource(filePath + "/" + files[i].getFileName() );
MimeBodyPart m = new MimeBodyPart();
m.setDataHandler(new DataHandler(fds));
m.setFileName( toEng(fds.getName() ) );
multi.addBodyPart(m);
}
}
msg.setContent(multi);
msg.setSentDate(new Date());
Transport.send(msg);
flag = true;
}catch(Exception e){
Logger logger = Logger.getLogger(this.getClass());
logger.error(this.getClass().getName() + " : " + e);
}
return flag;
}
boolean flag = false;
Properties prop = new Properties();
prop.put("mail.smtp.host" , host);
Session mailSession = Session.getDefaultInstance(prop, null);
try{
InternetAddress from = new InternetAddress(sender);
Message msg = new MimeMessage(mailSession);
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
msg.setSubject(subject);
Multipart multi = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(contents, "text/html; charset=ms949");
multi.addBodyPart(mbp);
if(files != null){
for(int i = 0; i < files.length; i++){
if(files[i] == null ) continue;
DataSource fds = new FileDataSource(filePath + "/" + files[i].getFileName() );
MimeBodyPart m = new MimeBodyPart();
m.setDataHandler(new DataHandler(fds));
m.setFileName( toEng(fds.getName() ) );
multi.addBodyPart(m);
}
}
msg.setContent(multi);
msg.setSentDate(new Date());
Transport.send(msg);
flag = true;
}catch(Exception e){
Logger logger = Logger.getLogger(this.getClass());
logger.error(this.getClass().getName() + " : " + e);
}
return flag;
}
public static String toEng(String origin) throws Exception{
return new String(origin.getBytes(), "ISO-8859-1");
}
==================================================================================
재미 있는건 한글 변환 부분 에서 new String(한글.getBytes("KSC5601"), "ISO-8859-1") 처럼 안해두 된다는것임...
다운로드시 한글 변환도 위와 같이 하면 URLEncoding 을 안해도 됨 ㅡㅡ
daum.net , naver.com, hotmail.com, outlook express 에서 테스트 함
테스트 환경 : Win xp , tomcat 4.1.30 , Apache 2.0.49 , j2sdk 1.4.2_05
사용 디비 : MySQL, Informix , Oracle
[출처] [본문스크랩] 메일(첨부자료 3개) 보내기 .. 한글도 잘됨|작성자 메멘토