소스가 짧으니 보시면 되시겠고...
기존 올려주신 소스에다가 첨부파일 한글명이 깨지지 않도록 encoding하는 모듈과 몇몇사항을 보완하였습니다.
제가 필요 없어서 보완하지 않은 사항은
RFC822 규격(?) 아무개< email _a_t_ address > 형식으로 보내주는 것과
제목을 별도로 인코딩안하여 일부 서버에서 깨질수 있습니다.
그리고 첨부파일을 보낼 위치를 개인환경에 맞게 바꾸세요.
그럼~
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class mailsend {
public final static void main( String[] args) throws Exception {
String msgSubj = "[공정경쟁]관리자입니다. ";//메일 제목
String to = "xxx _a_t_ hanmail.net"; //받는사람메일주소
String from = "someone _a_t_ kcube.co.kr"; //보내는사람메일주소
String host = "smtp 서버 ip";
String _mp1charset = "text/html; charset=euc-kr";
//파일 이름 얻기
String strSkinDir = "d:/tmp/mail/";//파일 몽땅 올릴 디렉토리
File f[] = (new File(strSkinDir)).listFiles();
for(int i = 0; i < f.length; i ++)
{
System.out.println(f[i].getName());
}
String msgText = "본문내용넣는곳입니다.......................";
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session sess = Session.getDefaultInstance(props, null);
try {
// create a message
InternetAddress _from = new InternetAddress(from);
Message msg = new MimeMessage(sess);
msg.setFrom(_from);
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(msgSubj);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(msgText + "<br>",_mp1charset);
// create the second message part
MimeBodyPart[] mbp2 = new MimeBodyPart[f.length];
for(int i = 0; i < f.length; i++ )
{
mbp2[i] = new MimeBodyPart();
}
//out.println(mbp2.length);
// attach the file to the message
FileDataSource[] fds = new FileDataSource[f.length];
for(int i = 0; i < f.length; i ++)
{
fds[i] = new FileDataSource( strSkinDir + f[i].getName());
mbp2[i].setDataHandler(new DataHandler(fds[i]));
//System.out.println( MimeUtility.encodeText(fds[i].getName(),"));
mbp2[i].setFileName( MimeUtility.encodeText(fds[i].getName(), "KSC5601", "Q"));
}
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
for(int i = 0; i < f.length; i ++)
{
mp.addBodyPart(mbp2[i]);
}
// add the Multipart to the message
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Mail sent.");
} catch (MessagingException mex) {
System.out.println(mex.getMessage());
System.out.println("Mail Error");
}
}
}