<font face='±¼¸²'>Authenticator
=====================================
java.net ÆÐÅ°Áö¿¡µµ Authenticatorµµ ÀÖ´Ù. JavaMail API´Â java.netÆÐÅ°Áöó·³
»ç¿ëÀÚÀ̸§°ú Æнº¿öµåÁ¤º¸µîÀ» ÅëÇØ ÀÚ¿ø¿¡ Á¢±ÙÇÏ°Ô ÇÏ´Â ÀåÁ¡µµ °¡Áö°í ÀÖ´Ù.
¿©±â¼ ¸»ÇÏ´Â ÀÚ¿ø(resource)´Â Áï ¸ÞÀÏ ¼¹ö¸¦ ¸»ÇÏ´Â °ÍÀε¥, Authenticator´Â
javax.mail ÆÐÅ°Áö¿¡ Á¤ÀǵǾî ÀÖ´Ù(´ç±Ù java.netÆÐÅ°Áö¿Í´Â ´Ù¸£´Ù)
Authenticator¸¦ »ç¿ëÇϱâ À§Çؼ´Â ÇØ´ç AuthenticatorŬ·¡½º¸¦ »ó¼ÓÇÑ
PasswordAuthenticatorŬ·¡½º·ÎºÎÅÍ ÀνºÅϽº¸¦ »ý¼ºÇÏ°í getPasswordAuthentication()
¸Þ¼Òµå¸¦ ¸®ÅϽÃÄÑ¾ß ÇÑ´Ù.
Authenticator¸¦ »ç¿ëÇϱâ À§Çؼ´Â ¹Ýµå½Ã ¼¼¼ÇÀ» »ý¼ºÇÏ¿© »ç¿ëÇØ¾ß Çϸç,
ÀÎÁõÀÌ ÇÊ¿äÇÑ°æ¿ì ÇØ´ç ¼¼¼ÇÀÌ Authenticator¿¡°Ô ÀÎÁõ½Ãµµ¸¦ Å뺸ÇÏ°Ô µÈ´Ù.
ÀÎÁõ½Ã¿¡´Â ȯ°æ¼³Á¤ÆÄÀÏ¿¡ ÀúÀåµÇ¾î ÀÖ´Â username°ú password ¶Ç´Â
À©µµ¿ìÀÇ Æ˾÷âÀ» ¶ç¿òÀ¸·Î¼ ÇØ´ç°ªÀ» ÀÔ·Â¹Þ°Ô µÇ°í, PasswordAuthenticationÀÇ
°´Ã¼¸¦ ¸®ÅÏÇÒ¼ö ÀÖµµ·Ï ÇÑ´Ù.
Properties props = new Properties(); // ÇÁ·ÎÆÛƼ »ý¼º
Authenticator auth = new MyAuthenticator(); // Authenticator¸¦ »ó¼Ó¹ÞÀº Ŭ·¡½º°´Ã¼ÀÇ »ý¼º
Session session = Session.getDefaultInstance(props, auth); //¼¼¼Ç»ý¼º
class MyAuthentication extends Authenticator {
PasswordAuthentication pa;
public MyAuthentication(){
pa = new PasswordAuthentication("your_id","your_password");
}
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}
À§ÀÇ ³»¿ëÀÌ ÇÊ¿äÇÒ °æ¿ì ¼¼¼Ç°´Ã¼¿¡ ÀÎÁõÀ» Çؾߵȴٴ °É ½Ã½ºÅÛÃøÀ¸·Î ÅëÁöÇϱâ À§ÇØ
props.put("mail.smtp.auth","true");
¶óÀÎÀÌ ¹Ýµå½Ã µé¾î°¡ ÀÖ¾î¾ß ÇÑ´Ù.
ÀÚ.. ¿¹Àü¿¡ Çß´ø msgsendsampleŬ·¡½º¸¦ local smtp°¡ ¾Æ´Ñ ¿ÜºÎ smtp¼¹ö¿¡ ÀÎÁõÀ»
½ÃµµÇÔÀ¸·Î¼ »ç¿ëÇϵµ·Ï Çغ¸°Ú´Ù.
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class msgsendsample {
static String msgText = "This is a message body.\nHere's the second line.";
public msgsendsample() {
String arg [] ={"ienvyou@orgio.net","ienvyou@openitech.co.kr", "openitech.co.kr", "true"};
System.out.println();
String to = arg[0];
String from = arg[1];
String host = arg[2];
System.out.println("babo!!");
boolean debug = Boolean.valueOf(arg[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", arg[3]);
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
try {
// create a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(arg[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 msgsendsample();
}
private static void usage() {
System.out.println("usage: java msgsendsample <<to> <from> <smtp> true|false");
}
}
class MyAuthentication extends Authenticator {
PasswordAuthentication pa;
public MyAuthentication(){
// ¿©·¯ºÐµéÀÌ »ç¿ëÇÏ°í ÀÖ´Â smtp serverÀÇ ¾ÆÀ̵ð¿Í Æнº¿öµå¸¦ ÀÔ·ÂÇÑ´Ù.
pa = new PasswordAuthentication("your_id","your_password");
}
// ¾Æ·¡ÀÇ ¸Þ¼Òµå´Â ½Ã½ºÅÛÃø¿¡¼ »ç¿ëÇÏ´Â ¸Þ¼ÒµåÀÌ´Ù.
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}
|