java sendmail ¿¹Á¦ - º¸¾ÈÁ¢¼ÓÀÌ ÇÊ¿äÇÏÁö ¾ÊÀº ¸ÞÀÏ°ú º¸¾ÈÁ¢¼ÓÀÌ ÇÊ¿äÇÑ ¸ÞÀÏ·Î ±¸ºÐ
=== Ãß°¡ ===
º¸¾ÈÁ¢¼ÓÀÌ ÇÊ¿äÇÏÁö ¾ÊÀº ¸ÞÀÏ(PlainMailHandler)°ú º¸¾ÈÁ¢¼ÓÀÌ ÇÊ¿äÇÑ ¸ÞÀÏ(SecureMailHandler)·Î ±¸ºÐÇÏ¿© ó¸®
PlainMailHandler - »ç³» ¸ÞÀÏ Å×½ºÆ® ¿Ï·á
SecureMailHandler - ³×À̹ö¸ÞÀÏ, G¸ÞÀÏ Å×½ºÆ® ¿Ï·á
JavamailÀÇ Properties ¼³Á¤¿ë Á¤º¸ => http://www.websina.com/bugzero/kb/javamail-properties.html
¸ÞÀÏÀü¼Û¿ë ÇÁ·ÎÅäÄÝÀº SMTPÀÌ°í, ¸ÞÀÏ ¼¹ö¿¡¼ ¸ÞÀÏÀ» °¡Á®¿À´Â ÇÁ·ÎÅäÄÝÀº POP°ú IMAP·Î ±¸ºÐµÈ´Ù.
POPÀº ¸ÞÀϼ¹ö¿¡¼ ¸ÞÀÏÀ» °¡Á®¿À¸é¼ ÇØ´ç¸ÞÀÏÀ» »èÁ¦ÇÏ°í, IMAPÀº ¸ÞÀϼ¹ö¿¡¼ ¸ÞÀÏÀ» °¡Á®¿À±â¸¸ ÇÑ´Ù.
IMAPÀº ¸ð¹ÙÀÏ°ú ÀÏ¹Ý µ¥½ºÅ©Å¾ ¿¬µ¿À» À§ÇØ »ç¿ëÇÒ ¼ö ÀÖ´Ù.
==========
ÀÏ´Ü ¸ÞÀÏÀ» º¸³»±â À§Çؼ´Â SMTP ¸¦ ±¸ÇöÇØ¾ß ÇÑ´Ù.
ÀÏ´Ü ¿À¶óŬ¿¡¼ Á¦°øÇÏ´Â API¸¦ ÀÌ¿ëÇÏ¸é °£´ÜÇÏ°Ô ±¸ÇöÀÌ °¡´ÉÇÏ´Ù.
JavaMail API ´Ù¿î·Îµå : http://www.oracle.com/technetwork/java/index-138643.html
º¸¾ÈÁ¢¼ÓÀÌ ÇÊ¿äÇÏÁö ¾ÊÀº °æ¿ì ´ÙÀ½°ú °°ÀÌ API¸¦ ÀÌ¿ëÇؼ ±¸ÇöÀÌ °¡´ÉÇÏ´Ù.
==== Å×½ºÆ® ¿¹Á¦ ===
package sdk.mail;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
/**
* ±âº»ÀûÀÎ ¸ÞÀÏ Àü¼Û
* UTF-8·Î ¸ÞÀÏÀ» Àü¼Û ÇϹǷΠÇѱ۵µ ó¸® °¡´É
*
* @author sdk
*
*/
public class PlainMail {
private String mailServer = null; // E-MAIL ¼¹ö ÁÖ¼Ò
private Message message = null; // º¸³»´Â ¸ÞÀÏ ¸Þ½ÃÁö
private String sender = null; // º¸³»´Â »ç¶÷ À̸ÞÀÏ ÁÖ¼Ò
private String senderNmae = null; // º¸³»´Â »ç¶÷ Ç¥½Ã À̸§
private String subject = null; // Á¦¸ñ
private String content = null; // º»¹®
private String receiver = null; // ¹Þ´Â »ç¶÷ ¸®½ºÆ®
private Address senderAddress = null; // º¸³»´Â »ç¶÷ ÁÖ¼Ò
private Address[] receiverAddress = null; // ¹Þ´Â »ç¶÷ ÁÖ¼Ò
public PlainMail() {}
public PlainMail(String mailServer){
setMailServer(mailServer);
}
public void SendMail() throws UnsupportedEncodingException, MessagingException
{
if(sender == null || subject == null || content == null || receiver == null)
throw new NullPointerException("sender, subject, content, receiver is null.");
initializeMailServer();
initializeSender();
initializeReceiver();
Send();
}
/**
* ¸ÞÀÏ ¼¹ö ÃʱâÈ
* Message °´Ã¼ »ý¼º
*
*/
private void initializeMailServer(){
Properties properties = new Properties();
properties.put("mail.smtp.host", mailServer);
Session s = Session.getDefaultInstance(properties);
message = new MimeMessage(s);
}
/**
* º¸³»´Â »ç¶÷ ÃʱâÈ
*
* @throws UnsupportedEncodingException Ç¥½Ã À̸§ÀÌ ÀνÄÇÒ ¼ö ¾ø´Â ¹®ÀÚ(Base 64 ±âÁØ)À϶§ ¹ß»ý
*/
private void initializeSender() throws UnsupportedEncodingException {
if(senderNmae == null) // º¸³»´Â »ç¶÷ Ç¥½Ã À̸§À» ÃʱâÈ ÇÏÁö ¾ÊÀ¸¸é ¸ÞÀÏÁÖ¼Ò·Î ÃʱâÈ
senderNmae = sender;
senderAddress = new InternetAddress(sender, MimeUtility.encodeText(senderNmae, "UTF-8", "B")); // Ç¥½Ã À̸§Àº ÀÎÄÚµùÇؼ Ç¥Çö
}
/**
* ¹Þ´Â »ç¶÷ ÃʱâÈ
*
* @throws AddressException ¹Þ´Â »ç¶÷ÀÌ ÀÎ½Ä ÇÒ ¼ö ¾ø´Â À̸§ÀÏ ¶§ ¹ß»ý
*/
private void initializeReceiver() throws AddressException {
ArrayList<String> receiverList = new ArrayList<String>();
StringTokenizer stMailAddress = new StringTokenizer(receiver, ";");
while (stMailAddress.hasMoreTokens()) {
receiverList.add(stMailAddress.nextToken());
}
receiverAddress = new Address[receiverList.size()];
for (int i = 0; i < receiverList.size(); i++) {
receiverAddress[i] = new InternetAddress(receiverList.get(i));
}
}
/**
* ¸ÞÀÏ Àü¼Û
*
* @throws MessagingException ¸Þ½ÃÁö ÃʱâÈÁß ¿À·ù ¹ß»ý
* @throws UnsupportedEncodingException ¸ÞÀÏ Á¦¸ñÀÌ ÀνÄÇÒ ¼ö ¾ø´Â ¹®ÀÚÀÏ ¶§ ¹ß»ý
*/
private void Send() throws MessagingException, UnsupportedEncodingException{
message.setHeader("content-type", "text/html;charset=UTF-8");
message.setFrom(senderAddress);
message.setRecipients(Message.RecipientType.TO, receiverAddress); // ¹Þ´Â »ç¶÷ ŸÀÔ (TO, CC, BCC)
message.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"));
message.setContent(content, "text/html;charset=UTF-8");
message.setSentDate(new java.util.Date());
Transport.send(message);
}
/**
* ¸ÞÀÏ ¼¹ö
*
* @param mailServer
*/
public void setMailServer(String mailServer){
this.mailServer = mailServer;
}
/**
* º¸³»´Â »ç¶÷ ÁÖ¼Ò
*
* @param sender
*/
public void setSender(String sender){
this.sender = sender;
}
/**
* º¸³»´Â »ç¶÷ Ç¥½Ã À̸§
*
* @param senderName
*/
public void setSenderName(String senderName){
this.senderNmae = senderName;
}
/**
* ¸ÞÀÏ Á¦¸ñ
*
* @param subject
*/
public void setSubject(String subject){
this.subject = subject;
}
/**
* ¸ÞÀÏ ³»¿ë
*
* @param content
*/
public void setContent(String content){
this.content = content;
}
/**
* ¹Þ´Â »ç¶÷ ¸®½ºÆ®
* ¼¼¹ÌÄÝ·Ð(';')À¸·Î ±¸ºÐ
*
* @param receiver
*/
public void setReceiver(String receiver){
this.receiver = receiver;
}
public static void main(String[] args){
PlainMail mail = new PlainMail();
mail.setMailServer("¸ÞÀÏ ¼¹ö ÁÖ¼Ò");
mail.setSender("º¸³»´Â »ç¶÷");
mail.setSenderName("Å×½ºÅÍ");
mail.setReceiver("¹Þ´Â »ç¶÷");
mail.setSubject("Á¦¸ñ - ÀÚ¹Ù¸ÞÀÏ Å×½ºÆ®");
mail.setContent("ÀÚ¹Ù¸ÞÀÏ Å×½ºÆ®¿ë ¸ÞÀÏÀÔ´Ï´Ù.\nThis is javamail test.\n2010-12-13");
try {
mail.SendMail();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}