단순 메 일 발송 테스트

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *   JavaMail        
 */
public final class MailUtil {
    //        
    private static final String SenderEmailAddr = "[email protected]";

    //          
    private static final String SMTPUserName = = "[email protected]";
    
    //          
    private static final String SMTPPassword = "xxxxxx";

    //        SMTP   
    private static final String SMTPServerName = "smtp.gmail.com";

    //     
    private static final String TransportType = "smtp";

    //   
    private static Properties props;

    /**
     *       ,             ,      MailUtil.sendMail      
     *
     */
    private MailUtil() {

    }

    /**
     *      
     */
    static {
        MailUtil.props = new Properties();

        //             
        MailUtil.props.put("mail.smtp.host", MailUtil.SMTPServerName);
        //       
        MailUtil.props.put("mail.smtp.auth", "true");
        MailUtil.props.put("mail.smtp.starttls.enable","true");
    }

    /**
     *     
     * @param emailAddr:       
     * @param mailTitle:    
     * @param mailConcept:    
     */
    public static void sendMail(String emailAddr, String mailTitle,
            String mailConcept) {
        //             ,null     Authenticator(    )   
        Session s = Session.getInstance(MailUtil.props, null);

        //       ,              ,      
        s.setDebug(false);
        
        //              
        Message message = new MimeMessage(s);
        try {
            //      
            Address from = new InternetAddress(MailUtil.SenderEmailAddr);
            message.setFrom(from);

            //      
            Address to = new InternetAddress(emailAddr);
            message.setRecipient(Message.RecipientType.TO, to);

            //     
            message.setSubject(mailTitle);
            //       
            message.setText(mailConcept);
            //       
            message.setSentDate(new Date());
            //       
            message.saveChanges();

            Transport transport = s.getTransport(MailUtil.TransportType);
            //            ;
            transport.connect(MailUtil.SMTPServerName, MailUtil.SMTPUserName,
                    MailUtil.SMTPPassword);

            //     ,                   
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

            System.out.println("    ,    :" + emailAddr + "   :" + mailTitle
                    + "   :" + mailConcept + "  !");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("    ,    :" + emailAddr + "   :" + mailTitle
                    + "   :" + mailConcept + "  !    " + e.getMessage());
        }
    }

    /**
     *         
     * @param args
     */
    public static void main(String[] args){
        MailUtil.sendMail("[email protected]", "test2", "xx javaMail  ");
        System.out.print("      ");
    }
} 

좋은 웹페이지 즐겨찾기