JavaEmail API 를 이용하기 위해서는 2개의 환경설정과 설치가 필요합니다.
1. JavaBeans Activation Framworks
http://java.sun.com/javase/technologies/desktop/javabeans/glasgow/jaf.html
2. JavaMail.
http://java.sun.com/products/javamail/index.jsp
다운로드가 완료된후 CLASSPATH를 설정합니다.;
;C:\javamail-1_4_1\javamail-1.4.1\mail.jar;C:\javamail-1_4_1\activation\activation.jar
이와 같이 자신의 경로에 맞게 추가 해줍니다.
/demo/폴더안에 보면 많은 예제들이 있습니다.;
우선 SMTP 서버가 있어야 합니다. naver.com 같은경우 으뜸회원이 되면 이용할수 있습니다.
msgsendsample2.java
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class msgsendsample2 {
static String msgText = "This is a message body.\nHere's the second line.";
public msgsendsample2() {
String args [] = {"받는쪽","보내는쪽","SMTP서버","session debugging"};
System.out.println();
String to = args[0];
String from = args[1];
String host = args[2];
boolean debug = Boolean.valueOf(args[3]).booleanValue();
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
Authenticator auth = new MyAuthentication() ;
if (debug) props.put("mail.debug", args[3]);
//Session session = Session.getInstance(props, null);
Session session = Session.getInstance(props, auth);
session.setDebug(debug);
try {
// create a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(args[0])};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("JavaMail APIs Test");
msg.setSentDate(new Date());
// If the desired charset is known, you can use
// setText(text, charset)
msg.setText(msgText);
Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("\n--Exception handling in msgsendsample.java");
mex.printStackTrace();
System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent Addresses");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
System.out.println(" "+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent Addresses");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
System.out.println(" "+validSent[i]);
}
}
}
System.out.println();
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);
}
}
public static void main(String[] args) {
new msgsendsample2();
}
private static void usage() {
System.out.println("usage: java msgsendsample <to> <from> <smtp> true|false");
}
}
class MyAuthentication extends Authenticator {
PasswordAuthentication smtpPass;
public MyAuthentication(){
//SMTP서버의 아이디와 패스워드
smtpPass = new PasswordAuthentication("아이디","패스워드");
}
public PasswordAuthentication getPasswordAuthentication(){
return smtpPass;
}
};
