在发送邮件之前在标头中设置MessageId

时间:2013-07-23 18:44:55

标签: java email grails groovy javamail

我在smtp服务器端口465上使用JAVA邮件从我的应用程序发送邮件。我需要的是,我必须在发送邮件之前设置Message-ID。我做了一些R& D并找到了下面的代码。我已覆盖updateMessageID()

的方法MimeMessage
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

public class CustomMimeMessage extends MimeMessage {

public CustomMimeMessage(Session session) {
    super(session);
}

@Override
protected void updateMessageID() throws MessagingException {

    setHeader("Message-ID", "message id");

   }
}

然后我在我的服务中创建了CustomMimeMessage的实例,然后使用该实例调用updateMessageID()方法,但我仍然获得了gmail生成的Message-ID。

4 个答案:

答案 0 :(得分:4)

在您的代码中

setHeader("Message-ID", "message id");

您正在尝试将“消息ID”设置为Message-ID,这是非常错误的,您必须设置一个唯一ID来限定消息ID的所有规则(Read This)。

试试这个..,。

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class CustomMimeMessage extends MimeMessage {
Session session;
private static int id = 0;

public CustomMimeMessage(Session session) {
    super(session);
    this.session=session;
}

@Override
protected void updateMessageID() throws MessagingException {
    setHeader("Message-ID", "<" + getUniqueMessageIDValue(session) + ">");
}

public static String getUniqueMessageIDValue(Session ssn) {
    String suffix = null;

    InternetAddress addr = InternetAddress.getLocalAddress(ssn);
    if (addr != null)
        suffix = addr.getAddress();
    else {
        suffix = "javamailuser@localhost"; // worst-case default
    }

    StringBuffer s = new StringBuffer();

    // Unique string is <hashcode>.<id>.<currentTime>.JavaMail.<suffix>
    s.append(s.hashCode()).append('.').append(getUniqueId()).append('.').
      append(System.currentTimeMillis()).append('.').
      append("JavaMail.").
      append(suffix);
    return s.toString();
    }

private static synchronized int getUniqueId() {
        return id++;
}
}

答案 1 :(得分:1)

我正在做类似的事情但是从本地主机发送。这可能有帮助。

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class SendEmail {
/**
 * Sends an email based on paramaters passed to it.
 * 
 * @param toWho - the recipiants email address 
 * @param fromWho - the senders email address
 * @param subject - the subject line of the email  
 * @param body - the email message body  
 * @return void
 * @throws AddressException
 * @throws MessageingException
 */
public void sendMail(String toWho, String subject, String body, String fromWho) throws AddressException, MessagingException {

    // Setting Properties
    Properties props = System.getProperties();
    props.put("mail.imaps.ssl.trust", "*"); // trusting all server certificates
    props.setProperty("mail.store.protocol", "imaps");

    // Get the default Session object.
    Session session = Session.getDefaultInstance(props, null);

    // Create a default MimeMessage object.
    MimeMessage message = new MimeMessage(session);
    // Set From header
    message.setFrom(new InternetAddress(fromWho));
    // Set to header
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(toWho));
    // Header set subject
    message.setSubject(subject);
    // Message Body
    message.setContent(body, "text/html; charset=utf-8");
    // Send message
    Transport.send(message);
}
}

答案 2 :(得分:0)

你可以在调用Transport.send()之前通过像这样扩展MimeMessage来将message-id设置为MimeMessage。

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

public class MyMimeMessage extends MimeMessage {

    public MailorMimeMessage(Session session) {
        super(session);
    }

    @Override
    protected void updateMessageID() throws MessagingException {
        if (getHeader("Message-Id") == null) {
            super.updateMessageID();
        }
    }
}

并设置自定义消息ID。

message.setHeader("Message-Id","<MY-MESSAGE-ID>");

答案 3 :(得分:0)

您可以在'@'左侧之前使用。(点)尝试自定义ID。它为我工作。 检查以下代码段:

public class CustomMimeMessage extends MimeMessage {

Session session;
private static int id = 0;

public CustomMimeMessage(Session session) {
    super(session);
    this.session = session;
}

protected void updateMessageID() throws MessagingException {
    debugLog("Calling updateMessageID()");
    setHeader("Message-ID", "<" + getUniqueMessageIDValue(session) + ">");
}

public static String getUniqueMessageIDValue(Session ssn) {
    String suffix = null;

    InternetAddress addr = InternetAddress.getLocalAddress(ssn);
    if (addr != null) {
        testLog("InternetAddress = " + addr.toString());
        String address = addr.getAddress();
        if (address.contains("@")) {
            address = address.substring(address.lastIndexOf("@"), address.length());
            suffix = address;
        }
    }

    if (suffix == null) {
        suffix = "@mail";// worst-case default
    }
    testLog("suffix Address = " + suffix);

    StringBuffer s = new StringBuffer();
    s.append(System.currentTimeMillis()).append("")
            .append(getUniqueId()).append(suffix).append(".sm");
    testLog("NEW MESSAGE-ID: " + s.toString());
    return s.toString();
}

private static synchronized int getUniqueId() {
    return id++;
}

有关更多详细信息,请参考以下RFC。这些对于创建自定义message-id的语法确实很有帮助。

1)RFC 2822
2)RFC 5322

希望这对您也有用。