Javaはテンセント企業のメールボックスのメールを送って、sslプロトコルに基づいて

3760 ワード

仕事の中で多くの場合、コードに基づいてメールを送信する必要があります.最近、JavaMailに基づいてテストdemoを書いて依存を導入しました.

        
            javax.mail
            mail
            1.4.1
        
        
            javax.mail
            mail
            1.4.4
        
    

インスタンスコード
package com.demo;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

public class EMailDemo {

    private static String account = "";	//     
    private static String pass = "";		//    
    private static String host = "smtp.exmail.qq.com";		//     (     )
    private static String port = "465";		//  
    private static String protocol = "smtp"; //  

    private static MimeMessage mimeMessage;

    /**
    *        ,       Authenticator     PasswordAuthentication,
   * SMTP   (   ),  javax.mail.Authenticator
   */
    static class MyAuthenricator extends Authenticator {
        String username = null;
        String password = null;
        public MyAuthenricator(String username,String password){
            this.username=username;
            this.password=password;
        }
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username,password);
        }
    }

    /**
   *       
   * @param subject     
   * @param sendHtml     
   * @param receiveUser    
    */
    public static void sendEmail(String subject,
            String sendHtml, String receiveUser){
        Properties prop = new Properties();
        //   
        prop.setProperty("mail.transport.protocol", protocol);
        //    
        prop.setProperty("mail.smtp.host", host);
        //   
        prop.setProperty("mail.smtp.port", port);
        //   smtp    
        prop.setProperty("mail.smtp.auth", "true");

        //  SSL,      !
        //      
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }

        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
        //   DEBUG  ,                ,                     ;
        session.setDebug(true);
        mimeMessage = new MimeMessage(session);

        try {
            //   
            mimeMessage.setFrom(new InternetAddress(account));

            //   
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveUser));
            //  
            mimeMessage.setSubject(subject);
            //  
            mimeMessage.setSentDate(new Date());

            //      
            mimeMessage.setText(sendHtml,"UTF-8");
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sendEmail("test", "  ", "[email protected]");
    }
}

テストクラスに受信者と送信者、送信者のパスワードを追加すればメールを送信できます.