JavaMail Api列表标头不起作用

时间:2014-02-22 19:20:27

标签: java email javamail listings

我正在尝试在电子邮件标题中添加一些与列表相关的标题行,但它不起作用..

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行仍在其中...

我的错在哪儿?

干杯

2 个答案:

答案 0 :(得分:1)

您的代码中没有任何明显错误,因此您需要对其进行调试。开始here。另外,在发送消息之前添加msg.writeTo(new FileOutputStream(“msg.txt”));然后检查msg.txt文件以确保它包含标头。如果确实如此,并且它们仍未显示在收件人的邮箱中,则一路上的某个服务器正在删除它们。

答案 1 :(得分:0)

以下是通过 GMail 发送消息的示例。如果邮件传递到邮件服务器,您可以看到哪些邮件头已成功设置。

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

public class Example {
    public static void main(String[] args) {
        final String username = "...@gmail.com";
        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("...@gmail.com"));
            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: ...@gmail.com
To: ...
Message-Id: <9449509.0.1393099214569.JavaMail....@...>
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