2013-07-23 923 views
2

我使用SMTP邮件在smtp服务器上发送邮件,我的应用程序使用SMTP端口465.我的需要是,我必须在发送邮件之前设置Message-ID。我做了一些R & D,发现下面的代码。我不得不重写方法updateMessageID()MimeMessage在发送邮件之前在邮件头中设置MessageId

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中产生的消息ID。

+0

这可能意味着,当邮件经过Gmail的服务器时,Gmail会修改标题将其转发到目的地之前。你不能使用其他标题吗? –

+0

如果打开[JavaMail会话调试](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug),您是否看到正在发送的消息中正确的Message-ID标头?如果Gmail正在修改通过它传递的消息的消息ID,这将是非常糟糕的...... –

回答

4

在你的代码

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

您要设置“消息ID”为消息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 = "[email protected]"; // 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

感谢这个宝贵的答案。 – Abs

+0

起初,这并不适用于我,但[这个答案](http://stackoverflow.com/a/13199695/581205)帮助。我错过了压倒一切的部分。 – maaartinus

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); 
} 
} 
0

您可以在通过扩展MimeMessage调用Transport.send()之前将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>");