Javax.mailを使用してメールを送信

5988 ワード

暇を見つけて資料を探して、メールを送るdemoをして、具体的な説明はすべて注釈の中にあります.後でまた忘れないように!

package sendEmail;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**  
 *   
 * SendEmail  
 *        
 * @author yangchuang  
 * @since 2014-9-20   6:36:18    
 * @version 1.0.0  
 *     
 *                             
 */
public class SendEmail {
    /**  
     * Session  ,Session                    ,                      。
     */  
    private Session session;
    
    /**  
     * Message        ,         、             。  Message       ,          。
     * Message          ,                ,           ,        、   、        。
     *     MimeMessage,  Message       。
     */  
    private Message message;
    
    /**  
     *       ,       。
     */  
    private Transport transport;
    
    /**  
     *            (smtp.163.com)
     */  
    private String host;
    
    /**  
     *          ([email protected])
     */  
    private String userName;
    
    /**  
     *          (xxxxxx)
     */  
    private String pwd;
    
      
    /**  
     *          SendEmail.  
     *  
     * @param host                 (smtp.163.com)
     * @param userName          ([email protected])
     * @param pwd                (xxxxxx)
     * @throws MessagingException  
     */
    public SendEmail(String host, String userName, String pwd) throws MessagingException {
        Properties proTemp = new Properties();
        //                
        proTemp.put("mail.smtp.host", host);
        //       ,            ,        
        proTemp.put("mail.smtp.auth", "true");
        //   proTemp      Session  
        session = Session.getDefaultInstance(proTemp);
        //   session    Message  
        message = new MimeMessage(session);
        //   session    Message  
        transport = session.getTransport("smtp");
        this.host = host;
        this.userName = userName;
        this.pwd = pwd;
    }

    public void send(String from, String address, String subject, String txt) throws AddressException {
        this.send(from, new String[]{address}, subject, txt);
    }

    public void send(String from, String[] addressesStr, String subject, String txt) throws AddressException{
        Address[] addresses = new Address[addressesStr.length];
        for(int i = 0; i < addressesStr.length;i++){
            addresses[i] = new InternetAddress(addressesStr[i]);
        }
        this.send(new InternetAddress(from), addresses, subject, txt);
    }

    public void send(Address from, Address[] addresses, String subject, String txt) {
        try {
            message.setFrom(from);
            
            //           。     ,            ,         。
            //         Message.RecipientType.TO,Message.RecipientType.CC Message.RecipientType.BCC。
            // TO       ,CC     ,BCC       。         ,    InternetAddress   。
            message.addRecipients(Message.RecipientType.TO, addresses);
            message.setSubject(subject);
            //  multipart              ,         
            Multipart multipart = new MimeMultipart();
            //          
            BodyPart contentPart = new MimeBodyPart();
            contentPart.setText(txt);
            multipart.addBodyPart(contentPart);
            message.setContent(multipart);
            message.saveChanges();
            //   
            transport.connect(host, userName, pwd);
            //   
            transport.sendMessage(message, message.getAllRecipients());
            //   
            transport.close();
        }
        catch (AddressException e) {
            e.printStackTrace();
        }
        catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    
    public void modifyUser(String userName, String pwd){
        this.userName = userName;
        this.pwd = pwd;
    }

    public Transport getTransport() {
        return transport;
    }

    public String getUserName() {
        return userName;
    }

    public String getPwd() {
        return pwd;
    }

}

テストコード

package sendEmail;

import javax.mail.MessagingException;

public class SendEmailTest {
    public static void main(String[] args) throws MessagingException {
        SendEmail sendEmail = new SendEmail("smtp.163.com", "[email protected]", "xxxxxx");
        sendEmail.send("[email protected]", new String[]{"[email protected]","[email protected]"}, "       !", "       !");
        sendEmail.modifyUser("[email protected]", "xxxxxxxxx");
        sendEmail.send("[email protected]", new String[]{"[email protected]","[email protected]"}, "       !", "       !");
    }
}