java 메일 보내는 실례 코드 (이식 가능)

웹 프로젝트를 예로 들면 코드는 이식할 수 있다
우선 메일을 가져와야 합니다.jar 패키지, 그리고 자신의 클래스 만들기
1: HTMLSender 클래스

package com.txq.mail;

 import java.util.Properties;

 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.NoSuchProviderException;
 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.MimeMultipart;
 import javax.mail.internet.MimeUtility;

 /**
  *
  * @author Administrator
  *
  */
 public class HTMLSender {
     private MimeMessage message;
     private Properties props;
     private Session session;
     private String name = "";
     private String password = "";

     public HTMLSender(String host,String name,String password)
     {
         this.name = name;
         this.password = password;
         props = System.getProperties();

         props.put("mail.smtp.host", host);
         props.put("mail.smtp.auth","true");
         MyAuthenticator auth = new MyAuthenticator(name,password);
         session = Session.getDefaultInstance(props,auth);

         message = new MimeMessage(session);
         new MimeMultipart();
     }

     public void setFrom(String from)
     {
         try
         {
             message.setFrom(new InternetAddress(from));
         }
         catch (AddressException e)
         {
             e.printStackTrace();
         }
         catch (MessagingException e)
         {
             e.printStackTrace();
         }
     }

     public void setTo(String to)
     {
         try
         {
             message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
         }
         catch(AddressException e)
         {
             e.printStackTrace();
         }
         catch(MessagingException e)
         {
             e.printStackTrace();
         }
     }

     public void setSubject(String subject)
     {
         try
         {
             message.setSubject(subject);
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
     }

     public void setContect(String content)
     {
         try
         {
             message.setContent(content,"text/html;charset=utf-8");
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
     }

     public boolean send()
     {
         try
         {
             Transport transport = session.getTransport("smtp");
             transport.connect((String)props.get("mail.smtp.host"),name,password);
             transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
             transport.close();
             return true;
         }
         catch(NoSuchProviderException e)
         {
             e.printStackTrace();
             return false;
         }
         catch(MessagingException e)
         {
             e.printStackTrace();
             return false;
         }
     }
 }
2: MyAuthenticator 클래스

package com.txq.mail;

 import javax.mail.Authenticator;
 import javax.mail.PasswordAuthentication;

 
 public class MyAuthenticator extends Authenticator{
     String name;
     String password;
     public MyAuthenticator(String name,String pasword)
     {
         this.name = name;
         this.password = pasword;
         getPasswordAuthentication();
     }
     protected PasswordAuthentication getPasswordAuthentication()
     {
         return new PasswordAuthentication(name,password);
     }
 }
3: MailAction 클래스

package com.txq.action;

 import java.io.PrintWriter;

 import com.opensymphony.xwork2.ActionSupport;
 import com.txq.mail.HTMLSender;

 public class MailAction extends ActionSupport {
     private String email;
     public String sendEmail(){
         // -
         String host = "smtp.sina.cn";
         // -
         String name = "followmusic";
         // -
         String password = "followmusic";
         //
         HTMLSender mail = new HTMLSender(host,name,password);
         //
         mail.setFrom("[email protected]");
         //
         mail.setTo("[email protected]");
         //
         mail.setSubject(" ");
         //
         mail.setContect(" ~");

         if(mail.send())
         {
             return SUCCESS;
         }
         else
         {
             return "error";
         }
     }
     public String getEmail() {
         return email;
     }
     public void setEmail(String email) {
         this.email = email;
     }
 }

좋은 웹페이지 즐겨찾기