2014-02-22 58 views
0

我寫了一個java代碼來發送html電子郵件。當我一次將電子郵件發送給一組收件人時,電子郵件會按照它的顯示方式顯示。下面是相同的代碼:html有時不在電子郵件中顯示

String content="<html><p><h1>This is my first java mail with an image but there is a difference.</h1></p><p><img src=\"http://***/ImageLoader2.php\"></p></html>"; 
m.setSubject(subject); 
m.setContent(content,"text/html"); 

InternetAddress[] toAdd=new InternetAddress[to.length]; 

    for(int i=0;i<to.length;i++) 
     toAdd[i]=new InternetAddress(to[i]); 

    for (InternetAddress toAdd1 : toAdd) 
       m.addRecipient(Message.RecipientType.TO, toAdd1); 
      Transport t=s.getTransport("smtp"); 
      t.connect(host, user, pass); 
      t.sendMessage(m, m.getAllRecipients()); 
      System.out.println("MESAAGES SENT"); 
    t.close(); 

而這裏的輸出:

first http://i58.tinypic.com/33ejtvq.png 現在,這裏就是我單獨發送的電子郵件的代碼:

m.setSubject(subject); 
Transport t=s.getTransport("smtp"); 
    t.connect(host, user, pass); 

      for(int i=0;i<to.length;i++){ 
       content="<html><p><h1>This is my first java mail with an image but there is a difference.</h1></p><p><img src=\"http://***/ImageLoader2.php?uid="+to[i]+"\"></p></html>"; 
       InternetAddress toAdd=new InternetAddress(to[i]); 
       m.setRecipient(Message.RecipientType.TO, toAdd); 
       m.setContent(content,"text/html"); 
       t.sendMessage(m, m.getAllRecipients()); 
      }   

      System.out.println("MESAAGES SENT"); 
    t.close(); 
     } 

這裏是這個代碼的輸出:

second http://i60.tinypic.com/21b7nz8.png 這是爲什麼發生?我知道css經常在網絡郵件系統上打破,但我沒有使用任何。谷歌也沒有任何幫助。

感謝

編輯

在被破壞的郵件,這些領域中缺少標題:

Content-Type: text/html; charset=us-ascii 
Content-Transfer-Encoding: 7bit 

其他,沒有其他signifiacnt差異。

編輯

我也嘗試使用m.setHeader()設置標頭,但沒有奏效。

+0

此外,對於Windows Live郵件,當我使用這兩種方法時,電子郵件都會正確顯示 –

+0

您是否可以突出顯示正在工作和已損壞示例之間的消息源的差異? – tripleee

+0

Gmail可以通過點擊向下箭頭並選擇「顯示原創」來查看原始內容。爲兩者做這件事並進行比較。有可能後者沒有有效的html或內容類型設置不正確。 –

回答

1

我認爲問題在於你正在爲每個收件人重複使用相同的MimeMessage對象。每次創建一個新的MimeMessage對象,我懷疑它會更好。

+0

這有效!但爲什麼?爲什麼每次重新使用對象時都會刪除內容類型標題?我的實現是否有問題,或者是否與執行MIME對象itslef有關?我也嘗試重置標題每次,但是沒有工作要麼 –

+0

我不知道爲什麼原始版本失敗;這將需要更多的調試。我認爲這與saveChanges沒有被調用有關。通常,當你發送時它被隱含地調用,但是這隻發生在第一次。 –

相關問題