package test;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
/**
*
*
* @author $Author$
* @version $Revision$
*/
public class EmailSend {
/**
* @param args
*
*/
public static void main(String[] args) {
String subject = "메일제목입니다.";
String msgText = "메일내용입니다.";
// 호스트 설정...
String host = "smtp.naver.com";
// 보내는 사람
String senderAddr = "pluggers@naver.com";
String senderNm = "하기";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// 권한 분리...
Authenticator auth = new EmailAuthentication();
Session sess = Session.getInstance(props, auth);
// 받는사람
String rcvAddr = "pluggers@naver.com";
String rcvNm = "하기";
try {
Message msg = new MimeMessage(sess);
// 제목 인코딩
msg.setSubject(MimeUtility.encodeText(subject, "euc-kr", "B"));
// 전송일 설정
msg.setSentDate(new Date());
InternetAddress fromAddr = new InternetAddress(senderAddr, senderNm);
msg.setFrom(fromAddr);
InternetAddress[] toAddr = { new InternetAddress(rcvAddr, rcvNm) };
msg.setRecipients(Message.RecipientType.TO, toAddr);
msg.setContent(msgText, "text/html; charset=euc-kr"); // HTML
Transport.send(msg);
} catch (UnsupportedEncodingException usee) {
usee.printStackTrace();
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
package test;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* 메일서버 로그인
*
* @author $Author$
* @version $Revision$
*/
public class EmailAuthentication extends Authenticator {
PasswordAuthentication pa;
public EmailAuthentication() {
// ID, Password 설정
pa = new PasswordAuthentication("pluggers", "******");
}
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}