Java SMTP Mail

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.util.*;
import java.security.Security;

public class SendMail
{
 public static void main(String[] args)
 {
  Properties prop = new Properties();
  prop.put("mail.smtp.host", "mail.host");
  prop.put("mail.smtp.port", "25");
  prop.put("mail.smtp.starttls.enable","true");
  prop.put("mail.smtp.auth",  "true");
//  prop.put("mail.smtp.debug", "true");
 
 
  String UserName = "database user id";
  String PassWord = "database password";
  String type = "text/html; charset=KSC5601";
 
  try
   {
   Authenticator authenticator = null; 
   authenticator = new SMTPAuthenticator(UserName, PassWord); 
      Session session = Session.getInstance(prop, authenticator);
//      session.setDebug(true);

      MimeMessage msg = new MimeMessage(session);
      msg.setSubject("SMTP 서버를 이용한 JavaMail 테스트");
      Address fromAddr = new InternetAddress("보내는이 메일주소");   // 보내는 사람의 메일주소
      msg.setFrom(fromAddr);
      Address toAddr = new InternetAddress("받는이 메일주소");  // 받는 사람의 메일주소
      msg.addRecipient(Message.RecipientType.TO, toAddr);
      String message = "SMTP 서버를 이용한 JavaMail 테스트" 
                     + "<img src=\"http://html url img/" />" ;
      msg.setContent(message, type );
                    
    
      System.out.println("Message: " + msg.getContent());
      Transport.send(msg);
      System.out.println("SMTP서버를 이용한 메일보내기 성공");
  }
  catch (Exception mex)
  { // 메일전송시 오류가 발생한 경우
    System.out.println("메일전송 오류 ");
    mex.printStackTrace();
  }
 }
}
class SMTPAuthenticator extends Authenticator { 
       PasswordAuthentication passwordAuthentication; 
       SMTPAuthenticator(String userName, String password) { 
       passwordAuthentication = new PasswordAuthentication(userName, password); 
       } 
       public PasswordAuthentication getPasswordAuthentication() { 
               return passwordAuthentication; 
   } 
}

댓글

이 블로그의 인기 게시물

jquery css

Struts2의 작동 원리

JSP/Servlet 한글 깨짐 처리 ?