Mail received by Yahoo, but not Hotmail. please help see my code
I am using the following code to send email to my Hotmail account.
Properties props = System.getProperties();
props.put("mail.smtp.host", "localhost");
Session session = Session.getDefaultInstance(props, null);
Transport transport = session.getTransport("smtp");
transport.connect();
String toAddress="myid@hotmail.com";
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("test@mydomain.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(toAddress));
message.setSubject("test");
message.setContent("test");
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
--
For the code above, Hotmail could not receive the mail.
If I change the toAddress to my Yahoo account, Yahoo can receive the email.
However, If I use telnet to send mail to my Hotmail account from the same machine, it works.
> telnet localhost 25
> HELO ..
> MAIL FROM:
> RCPT TO:
...
I could not figure out what is the difference between Yahoo and Hotmail email. Can anyone help me?
Thank you in advance.
Dave
# 1
I've heard rumors, which I haven't verified, that HotMail is rejecting
messages that have a JavaMail-generated Message-ID because
it thinks the message is spam. Apparently too many JavaMail users
are actually spammers. :-(
In the message you send using telnet, try pasting in a JavaMail style
Message-ID header and see if that makes a difference. Or, try
subclassing MimeMessage and override the updateMessageId
method to generate a different kind of ID.
Also, look in your spam mailbox on HotMail, if they support that.
# 6
Extend MimeMessage for change header Message-ID
/**
* Objet MimeMessage 閠endu pour changer l'ent阾e Message-ID
*/
private final static class MyMimeMessage extends MimeMessage
{
protected Session session;
/**
* Constructeur!
*
* @param _sessionjavax.mail.Session
*/
protected MyMimeMessage(Session _session)
{
super(_session);
this.session = _session;
}
// code other constructor that needed!
/**
* Modification du Message-ID ent阾e (appeler par updateHeaders!)
*/
protected void updateMessageID() throws MessagingException
{
setHeader("Message-ID", getMyMessageID(session));
setHeader("X-Mailer", "EMail_2.0"); // ajout du mailer...
}
/**
* Retourne un unique Message-ID.
*/
private static String getMyMessageID(Session _session)
{
InternetAddress localAddress = InternetAddress.getLocalAddress(_session);
String address = (localAddress != null) ? localAddress.getAddress() : "user@localhost";
Long id = new Long(0);
double d = java.lang.Math.random();
id = new Long((long) (d * Long.MAX_VALUE));
StringBuffer buffer = new StringBuffer();
buffer.append(id);
buffer.append('.');
buffer.append(System.currentTimeMillis());
buffer.append('.');
buffer.append("EMail.");
buffer.append(address);
return buffer.toString();
}
} // fin de classe