2014-02-22 13 views
0

我嘗試添加在電子郵件標題一些上市相關的頭線,但它不工作..JavaMail的阿比上市頭不起作用

msg.addHeader("List-ID", lmsg.getListId()); 
msg.addHeader("List-Archive", lmsg.getListId()); 
msg.setHeader("List-Post", lmsg.getListId()); 
msg.setHeader("List-Unsubscribe", lmsg.getListId()); 
msg.setHeader("X-Mailer", mailer); 
msg.setSentDate(lmsg.getSendDate()); 
Transport.send(msg); 

沒什麼列表標題行的是在接收但是x-mailer行仍然在...

我的錯在哪裏?

乾杯

回答

0

這裏是經由發送消息的GMail的一個例子。如果郵件被髮送到郵件服務器,您可以看到哪些郵件頭設置成功。

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class Example { 
    public static void main(String[] args) { 
     final String username = "[email protected]"; 
     final String password = "..."; 

     Properties properties = new Properties(); 

     properties.put("mail.smtp.auth", "true"); 
     properties.put("mail.smtp.starttls.enable", "true"); 
     properties.put("mail.smtp.host", "smtp.gmail.com"); 
     properties.put("mail.smtp.port", "587"); 

     Session session = Session.getInstance(properties, new Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
      }); 

     try { 
      Message msg = new MimeMessage(session); 

      msg.setFrom(new InternetAddress("[email protected]")); 
      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("...")); 

      msg.setSubject("Subject"); 
      msg.setText("Text"); 

      msg.addHeader("List-ID", "1"); 
      msg.addHeader("List-Archive", "1"); 
      msg.setHeader("List-Post", "1"); 
      msg.setHeader("List-Unsubscribe", "1"); 
      //msg.setHeader("X-Mailer", mailer); 
      msg.setSentDate(new Date()); 

      Transport.send(msg); 

      System.out.println("Message is successfully delivered"); 
      Enumeration headers = msg.getAllHeaders(); 
      while (headers.hasMoreElements()) { 
       Header header = (Header) headers.nextElement(); 
       System.out.println(header.getName() + ": " + header.getValue()); 
      } 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

使輸出端:

Message is successfully delivered 
Date: Sun, 23 Feb 2014 00:00:14 +0400 (MSK) 
From: [email protected] 
To: ... 
Message-Id: <[email protected]> 
Subject: Subject 
MIME-Version: 1.0 
Content-Type: text/plain; charset=us-ascii 
Content-Transfer-Encoding: 7bit 
List-ID: 1 
List-Archive: 1 
List-Post: 1 
List-Unsubscribe: 1 
1

沒有什麼明顯的錯誤在你的代碼,所以你需要調試。開始here。另外,在發送消息之前添加msg.writeTo(new FileOutputStream(「msg.txt」));然後檢查msg.txt文件以確保它包含標題。如果確實如此,並且他們仍然沒有顯示在收件人的郵箱中,那麼一些服務器正在刪除它們。