java - Set MessageId in header before sending mail -
i sending mails application using java mail on smtp server, port 465. need that, have set message-id before sending mail. did r&d , found code below. had override method updatemessageid()
of 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"); } }
and had made instance of custommimemessage
in service , invoke updatemessageid()
method using instance, still message-id generated gmail.
in code
setheader("message-id", "message id");
you trying set "message id" message-id quite wrong have set unique id qualify rules of message id (read this).
try 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 <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++; } }
Comments
Post a Comment