¸ÞÀÏ¹ß¼Û ¼Ò½º ÀÚ¹Ù ¸ÞÀÏ ÅÛÇø´ html ¿©ºÎ ġȯ ½ÇÀü
¸ÞÀÏ¹ß¼Û ¼Ò½ºThe.¹Î±º | 2004/08/13 07:22 | Java¾ÛÀ¸·Î º¸±â
mail-8961.jar
activation.jar
¸ÞÀÏ¹ß¼Û ¼Ò½º
±è¼º¹Ú ¼±»ý´Ô ÀÛ¼º
»ç¿ëÇÒ·Á¸é ÷ºÎÇÑ jarÆÄÀÏÀ» classpath¿¡ ÁöÁ¤À» ÇؾßÇÕ´Ï´Ù
/*
* Created on 2004. 2. 8.
*
*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
* @author ±è¼º¹Ú(urstory@nownuri.net)
*
* J2SDK 1.4 À̻󿡼 µ¿ÀÛÇÑ´Ù. String classÀÇ replaceAll¸Þ¼Òµå »ç¿ë.
*
*/
public class SendMail {
private String to;
private String from;
private String host;
private String template;
private String subject;
private String msg;
private boolean istext;
public SendMail(String to, String from, String host, String subject, String msg, boolean istext){
this(to, from, host, null, subject, msg, istext);
}
/**
* ¸ÞÀÏÀ» º¸³»±â À§ÇÏ¿© Á¤º¸¸¦ ÃʱâÈ ÇÑ´Ù.
* @param to ¹Þ´Â »ç¶÷
* @param from º¸³»´Â »ç¶÷
* @param host º¸³»±â À§ÇÑ ¸ÞÀÏ ¼¹ö
* @param template ¸Þ¼¼ÁöÀÇ ÅÛÇø´ --body-- ºÎºÐÀÌ msg ·Î ġȯµÈ´Ù.
* @param subject Á¦¸ñ
* @param msg º¸³»´Â ¸ÞÀÏ
* @param istext ÅؽºÆ® ¸ÞÀÏÀÏ °æ¿ì true, htmlÇü½ÄÀÏ °æ¿ì false
*/
public SendMail(String to, String from, String host, String template, String subject, String msg, boolean istext){
this.to = to;
this.from = from;
this.host = host;
this.template = template;
this.subject = subject;
this.msg = msg;
this.istext = istext;
}
/**
* ¸ÞÀÏÀ» Àü¼ÛÇÑ´Ù.
* @return ¸ÞÀÏÀ» ¼º°øÀûÀ¸·Î Àü¼ÛÇÏ¿´À» °æ¿ì true, ±×·¸Áö ¾ÊÀ» °æ¿ì false
*/
public boolean send(){
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session sess = Session.getDefaultInstance(props, null);
try {
// create a message
Message message = new MimeMessage(sess);
message.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setSentDate(new Date());
if(template != null){
msg = template.replaceAll("--body--", msg);
}
if(!istext)
message.setContent(msg,"text/html; charset=euc-kr"); // HTML Çü½Ä
else
message.setText(msg); // TEXT Çü½Ä
Transport.send(message);
} catch (MessagingException mex) {
System.out.println("error msg :" + mex.toString());
return false;
}
return true;
}
public static void main(String[] args) {
//SendMail sendMail = new SendMail("¹Þ´Â»ç¶÷emailÁÖ¼Ò", "º¸³»´Â»ç¶÷emailÁÖ¼Ò", "smtp ¼¹öÁÖ¼Ò", null, "¾È³çÇϼ¼¿ä.", "¾È³çÇϼ¼¿ä. ÀÌ°ÍÀº º»¹® ³»¿ëÀÌ¿¡¿ä.<h1>hello</h1>", false);
SendMail sendMail = new SendMail("¹Þ´Â»ç¶÷emailÁÖ¼Ò", "º¸³»´Â»ç¶÷emailÁÖ¼Ò", "smtp¼¹öÁÖ¼Ò", "<table border=1><tr><td>--body--</td></tr></table>", "¾È³çÇϼ¼¿ä.", "¾È³çÇϼ¼¿ä. ÀÌ°ÍÀº º»¹® ³»¿ëÀÌ¿¡¿ä.<h1>hello</h1>", false);
if(sendMail.send())
System.out.println("Àü¼ÛÀÌ ¼º°øÇÏ¿´½À´Ï´Ù.");
else
System.out.println("Àü¼ÛÀÌ ½ÇÆÐÇÏ¿´½À´Ï´Ù.");
}