Java程式發送郵件

來源:互聯網
上載者:User

標籤:tle   技術   address   開放   時間   cheng   csdn   技術分享   set   

  之前上網有看到過別人總結的使用java程式發送郵件,於是自己下來練習,把自己學習的一些心得總結出來.

首先我們這裡需要採用兩個jar包:

 

需要的朋友可以自行上網去CSDN類似的網站上面找

順便把自己測試案例貼了出來,裡面有些詳細的注釋,接下來會提醒寫demo的時候大家一些注意的地方.把中間有自己遇到的問題.貼出來供大家參考.

1.首先確保發送人的郵箱  跟  接收人的郵箱  的smtp協議開著.   ------》可以上郵箱設定裡面開啟.

這裡使用的QQ郵箱   我們可以在郵箱協助中找到QQ郵箱的pop3跟smtp伺服器位址已經開放的相應連接埠 一般是465,或者是587

2.極其重要一點:現在很多郵箱都採用授權碼的方式對第三方開放 pop3跟smtp服務   ,所以我們在用郵箱賬戶密碼登入郵箱時,記得要把密碼換成相應的  授權碼.

   (需要我們手機去發簡訊驗證的)

3.我們測試發送的郵件內容有可能被郵件伺服器當成是垃圾郵件攔截掉,這裡我們需要設定下.將其加入到郵件的白名單中,可避開反垃圾誤判

4.針對發送失敗的錯誤,我們可以仔細分析出錯的code碼,上網尋找對應的錯誤碼資訊,找到錯誤的原因.

   這裡針對發送郵箱是網易來說:

http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html   

 

 

 測試demo如下:

package Test;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Testmail {
    // 發送郵件的帳號
    public static String ownEmailAccount = "[email protected]";
    // 發送郵件的密碼------》授權碼
    public static String ownEmailPassword = "xxxxxxxxxxx";
    // 發送郵件的smtp 伺服器 地址
    public static String myEmailSMTPHost = "smtp.163.com";
    // 發送郵件對方的郵箱
    public static String receiveMailAccount = "[email protected]";

    public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        // 設定郵件傳輸採用的協議smtp
        prop.setProperty("mail.transport.protocol", "smtp");
        // 設定發送人郵件伺服器的smtp地址
        // 這裡以網易的郵箱smtp伺服器位址為例
        prop.setProperty("mail.smtp.host", myEmailSMTPHost);
        // 設定驗證機制
        prop.setProperty("mail.smtp.auth", "true");

        // SMTP 伺服器的連接埠 (非 SSL 串連的連接埠一般預設為 25, 可以不添加, 如果開啟了 SSL 串連,
        // 需要改為對應郵箱的 SMTP 伺服器的連接埠, 具體可查看對應郵箱服務的協助,
        // QQ郵箱的SMTP(SLL)連接埠為465或587, 其他郵箱自行去查看)

        /*final String smtpPort = "465";
        prop.setProperty("mail.smtp.port", smtpPort);
        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.socketFactory.port", smtpPort);*/

        // 建立對象回話跟伺服器互動
        Session session = Session.getInstance(prop);
        // 會話採用debug模式
        session.setDebug(true);
        // 建立郵件對象
        Message message = createSimpleMail(session);

        Transport trans = session.getTransport();
        // 連結郵件伺服器
        trans.connect(ownEmailAccount, ownEmailPassword);
        // 發送資訊
        trans.sendMessage(message, message.getAllRecipients());
        // 關閉連結
        trans.close();
    }

    /**  
    * @Title: createSimpleMail  
    * @Description: 建立郵件對象
    * @author: chengpeng
    * @param @param session
    * @param @return
    * @param @throws Exception    設定檔案  
    * @return Message    傳回型別  
    * @throws  
    */
    public static Message createSimpleMail(Session session) throws Exception {
        MimeMessage message = new MimeMessage(session);
        // 設定發送郵件地址,param1 代表發送地址 param2 代表發送的名稱(任意的) param3 代表名稱編碼方式
        message.setFrom(new InternetAddress("[email protected]", "張三", "utf-8"));
        // 代表收件者
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiveMailAccount, "李四", "utf-8"));
        // To: 增加收件者(可選)
        /*message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress("[email protected]", "USER_DD", "UTF-8"));
        // Cc: 抄送(可選)
        message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("[email protected]", "USER_EE", "UTF-8"));
        // Bcc: 密送(可選)
        message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress("[email protected]", "USER_FF", "UTF-8"));*/
        // 設定郵件主題
        message.setSubject("測試轉寄郵件");
        // 設定郵件內容
        message.setContent("早安,世界   你最近好嗎!", "text/html;charset=utf-8");
        // 設定發送時間
        message.setSentDate(new Date());
        // 儲存上面的編輯內容
        message.saveChanges();
        // 將上面建立的對象寫入本地
        OutputStream out = new FileOutputStream("MyEmail.eml");
        message.writeTo(out);
        out.flush();
        out.close();
        return message;

    }

}

參考連結:http://blog.csdn.net/xietansheng/article/details/51673073

Java程式發送郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.