使用JavaMail發送郵件

一、採用的是QQ的smtp服務器發送郵件,其它服務器沒驗證html

二、須要QQ郵箱的POP3|SMTP服務開啓,而且拷貝下受權碼代碼中會用到java

 

import java.util.Calendar;
import java.util.Properties;安全

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;服務器

/*
 * 使用JavaMail API 在Java程序中發送e-mail
 */
public class SendEmail {
    /*
     * param:
     * smtpHost:服務器域名,這裏採用的是QQ郵箱的smtp服務器
     * from:你的要發送郵件的郵箱
     * fromUserPassword:要發送郵箱的密碼
     * to:收件人的郵箱
     * subject:發送的郵件的主題
     * messageText:郵件正文
     * messageType:發送內容的格式
     */
    @SuppressWarnings("static-access")
    public static void sendMessage(
            String smtpHost,
            String from,
            String fromUserPassword,
            String to,
            String subject,
            String messageText,
            String messageType) throws MessagingException{
        //一、配置javax.mail.Session對象
        System.out.println("爲" + smtpHost + "配置mail session對象");
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);
        //props.put("mail.smtp.starttls.enable", "true");//使用 STARTTLS安全鏈接
        props.put("mail.smtp.port", "465"); 
        props.put("mail.smtp.auth", "true");  //使用驗證
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.ssl.enable", "true");
        //props.put("mail.debug", "true");
        Session mailSession = Session.getInstance(props, new MyAuthenticator(from,"*************"));
        //Session mailSession = Session.getDefaultInstance(props,null);
        //二、編寫消息
        System.out.println("編寫消息from--to:"+from+"----"+to);
        InternetAddress fromAddress = new InternetAddress(from);
        InternetAddress toAddress = new InternetAddress(to);
        
        MimeMessage message = new MimeMessage(mailSession);
        
        message.setFrom(fromAddress);
        message.addRecipient(RecipientType.TO, toAddress);
        
        message.setSentDate(Calendar.getInstance().getTime());
        message.setSubject(subject);
        message.setContent(messageText,messageType);
        
        //三、發送消息
        Transport transport = mailSession.getTransport("smtp");
        //transport.connect(smtpHost,from,fromUserPassword);
        transport.connect(from, fromUserPassword);
        transport.send(message, message.getRecipients(RecipientType.TO));
        System.out.println("message yes");
        
    }
    public static void main(String[] args) {
        try {
            sendMessage("smtp.qq.com",
                    "yourqqmail@qq.com", 
                    "yourmailpassword", 
                    "你的任意郵箱地址", 
                    "nihao", 
                    "<<<<<>>>>>>",
                    "text/html;charset=gb2312");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
}網絡

/*
 * 注意:參數  password 爲 POP3/SMTP 的受權碼 (16位),而不是郵箱密碼session

* 就是上邊提到的從QQ郵箱獲取的受權碼
 */
class MyAuthenticator extends Authenticator{
    String userName = "";
    String password = "";
    
    public MyAuthenticator() {
        
    }
    public MyAuthenticator(String userName,String password){
        this.userName = userName;
        this.password = password;
    }
    protected PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(userName, password);
    }
}this

備註:代碼主體來自於網絡上的前輩,在下只是在前輩基礎上作了微調,若有雷同,請原諒我吧~~~~~~~~在此對其分享表示感謝O(∩_∩)O哈!
 spa

相關文章
相關標籤/搜索