java:郵件發送失敗總結(只是外網伺服器有問題)

來源:互聯網
上載者:User

標籤:java:郵件發送失敗

案例一:

    運行環境比較

    1、本地用同樣的代碼、系統內容及發郵件的帳號是沒有問題的;

    2、外網其它伺服器項目使用相同技術也沒有問題;

    問題現象

    郵件模板中某連結(如:“http://www.baidu.com/...”)在郵箱發送後自動執行了,導致其它業務出錯。

    處理方式

    1、處理前:“http://www.baidu.com/...”這部分字串是在發郵件時整個填充到郵件模板中某個變數;   

    2、處理後:“http://”固定在郵件模板中,“www.baidu.com/...”這部分會變化就仍然用程式填充至模板;

   處理結果

   問題已解決,郵件中的連結不再自動執行。


案例二:

    運行環境比較

    1、本地用同樣的代碼、系統內容及發郵件的帳號也是沒有問題的;

    問題現象

    發郵件報錯如下:

     com.sun.mail.smtp.SMTPSendFailedException: 553 Envolope sender mismatch with login user.

    處理方式

    根據網上搜尋的資料,說是發郵件的username 和 發送地址不匹配,具體如下:

    :553 Envolope sender mismatch with login user.-->553年Envolope發送方與登入使用者不匹配。
        Session session = Session.getInstance(props, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
        String userName = "[email protected]";//A 
        return new PasswordAuthentication(userName, password);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("[email protected]"));// B 
        答:A和B 必須統一!

    所以我認真檢查了下,發現我們項目的用法確實有點不一樣,不過應該也是正確的呀。

我們項目中的B是這樣的

new InternetAddress(StringUtil.formatAddress(“小明”, “[email protected]”);/** * 格式化 Name <[email protected]> 的地址 * @param name 名字 * @param email Email地址 * @return 格式化的地址 */public static String formatAddress(String name, String email) {    if (StringHelper.isEmpty(name)) {return email;    }    try {//"%1$s <%2$s>"return String.format("%1$s <%2$s>", MimeUtility.encodeText(name,"utf-8", "B"), email);    } catch (UnsupportedEncodingException e) {e.printStackTrace();    }return email;}

   對比下:

   A: "[email protected]"

      B:?utf-8?B?5ZKx572R5ZWG5Z+O?= <[email protected]>

      於是我就改為下面這個樣子:

  new InternetAddress(StringUtil.formatAddress(“[email protected]”);//A和B一樣了

   傳外網、重啟伺服器,測試下還真沒問題了,只不是寄件者不能顯示中文名“小明”而是預設的“ceshi”.

  

   一是這樣寄件者不好看;二是我覺得應該不是這個問題,因為本地也是同樣的配置但沒問題啊!

  

   感覺還是伺服器自身的問題,於是我又把代碼改回去,重啟伺服器,再測試,哎~~~~~~~~

  

   坑啊~~郵件發送成功了。也就是說其實我只需要重啟下伺服器就好了!!!

  

   可是為什麼呢?哎~~ 這種事很難解釋,但時常會遇到!!

  

   那麼長個記性,下次遇到本地測試機沒問題,卻偏偏外網伺服器突然就出問題了,並且之前還好好的;那麼兄台,這個時候先不管三七二十一,先重啟下試試吧!省得白忙活了不是~~


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.