基於java mail實現簡單的QQ郵箱發送郵件

來源:互聯網
上載者:User

標籤:自己   自己的   github   href   技術分享   util   logs   transport   測試   

剛學習到java郵件相關的知識,先寫下這篇部落格,方便以後翻閱學習。

-----------------------------第一步 開啟SMTP服務

在 QQ 郵箱裡的 設定->賬戶裡開啟 SMTP 服務 

完成驗證

 

擷取授權碼(後面代碼實現時使用)

-----------------------------第二步 環境配置

即下載第三方庫

https://github.com/javaee/javamail/releases

 

-----------------------------第三步 代碼實現

package com.core;import java.security.GeneralSecurityException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Properties;import javax.mail.Address;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import com.sun.mail.util.MailSSLSocketFactory;public class MailTool {    public static void main(String[] args) throws MessagingException, GeneralSecurityException {                Properties props = new Properties();        // 開啟debug調試        props.setProperty("mail.debug", "true");        // 發送伺服器需要身分識別驗證        props.setProperty("mail.smtp.auth", "true");        // 設定郵件伺服器主機名稱        props.setProperty("mail.host", "smtp.qq.com");        // 發送郵件協議名稱        props.setProperty("mail.transport.protocol", "smtp");        // 開啟SSL加密,否則會失敗        MailSSLSocketFactory sf = new MailSSLSocketFactory();        sf.setTrustAllHosts(true);        props.put("mail.smtp.ssl.enable", "true");        props.put("mail.smtp.ssl.socketFactory", sf);        // 建立session        Session session = Session.getInstance(props);                // 建立郵件        Message msg = new MimeMessage(session);        // 設定標題        msg.setSubject("測試郵件");        // 編輯內容        StringBuilder builder = new StringBuilder();        builder.append("這是一封java mail測試郵件\n");        builder.append("這是第二行");        builder.append("\n時間 " + getStringDate());        // 設定內容        msg.setText(builder.toString());        // 發送的郵箱地址        msg.setFrom(new InternetAddress("自己的郵箱@qq.com"));        // 通過session得到transport對象        Transport transport = session.getTransport();        // 串連郵件伺服器:郵箱類型,帳號,授權碼代替密碼(更安全)        transport.connect("smtp.qq.com", "自己的郵箱@qq.com", "授權碼");        // 發送郵件        transport.sendMessage(msg, new Address[] { new InternetAddress("目標郵箱@qq.com") });        transport.close();    }        /**     * 擷取目前時間     * @return String     */    public static String getStringDate() {        Date currentTime = new Date();        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String dateString = formatter.format(currentTime);        return dateString;    }}

-----------------------------第四步 效果展示

 

 

 

-----------------------------第五步 推薦

JAVA發送郵件最全樣本

Java基於JavaMail實現向QQ郵箱發送郵件

 

基於java mail實現簡單的QQ郵箱發送郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.