2

我在 smtp 服务器端口 465 上使用 JAVA 邮件从我的应用程序发送邮件。我需要的是,我必须在发送邮件之前设置 Message-ID。我做了一些研发,找到了下面的代码。我已经覆盖了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

4 回答 4

4

在您的代码中

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

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

试试这个..,。

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++;
}
}
于 2013-07-27T11:41:05.920 回答
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);
}
}
于 2013-07-23T19:04:02.037 回答
0

您可以在调用 Transport.send() 之前将 message-id 设置为 MimeMessage,方法是像这样扩展 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>");
于 2016-02-23T13:46:33.140 回答
0

您可以在“@”左侧尝试使用 .(dot) 的自定义 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。这些对于为自定义消息 ID 创建语法非常有帮助。

  1. RFC 2822
  2. RFC 5322

希望这也对你有用。

于 2019-07-05T06:12:51.547 回答