Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem when sending java mail to all hotmail accounts

Status
Not open for further replies.

SON1

Programmer
Nov 21, 2004
51
CY
I am trying to send mail using java mail, however my mail towards all hotmails accounts are not delivered . The sender's address is not blacklisted and my email content is not filtered as spam by hotmail as i can send the same email content from my outlook using the same sender's email address.

I figure out it could be something wrong with my encoding/headers/parameters etc. I would like some help here. This is the code for my mail out class file. All my emails are html (text also not working for hotmail accounts). I have tried both base64 and 8bit encoding but with no luck. Needless to say that me emails towards other providers are delivered perfectly.


package congress3.services.automated;

import congress3.lib.Configuration;
import congress3.objects.Email;
import congress3.services.EmailService;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class EmailEngine {

private static EmailEngine myInstance = new EmailEngine();
private EmailEngine() {}
public static EmailEngine getInstance() { return myInstance; }

public static void SendOutboundEmails() {

try{
List outgoingEmailList = EmailService.getInstance().getOutgoingEmailList();

if (outgoingEmailList != null ) {
for (int x=0 ; x < outgoingEmailList.size() ; x++ ) {
Email email = (Email) outgoingEmailList.get(x);
sendMessage(email);
Thread.sleep(1000);
}// end for
}
} catch(Exception x) { System.out.println(x);} //user try

}


private static void sendMessage(Email email) throws Exception {

boolean successful=true;

String to = email.getToField();
String subject = email.getSubjectField();
String message = email.getTextField();
String from = email.getFromField();


try {
Properties p = new Properties();
p.put("mail.host", Configuration.MAIL_HOST);
Session session = Session.getInstance(p, new MailAuthenticator());
MimeMessage msg = new MimeMessage(session);
Address fromAddress = new InternetAddress(from);
Address toAddress = new InternetAddress(to);


msg.setContent(message, "text/html ; charset=UTF-8");
msg.setSubject(subject, "UTF-8");
msg.addHeader("MIME-Version", "1.0");
msg.addHeader("Content-Transfer-Encoding", "BASE64");
msg.addHeader("Content-Type", "text/html");

msg.setFrom(fromAddress);
msg.setRecipient(Message.RecipientType.TO, toAddress);


Transport.send(msg);
} catch (MessagingException ex) {
System.out.println(ex);
successful=false;
}
EmailService.getInstance().markEmailAsSent(successful, true, email.getID());
}
}

class MailAuthenticator extends javax.mail.Authenticator {
public javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(Configuration.MAIL_USERNAME,Configuration.MAIL_PASSWORD);
}
}
 
Which mx server are you using for hotmail ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
as in the IP ? not sure what do you mean by your question.
 
Well java mail is not an MTA - so your MX Server must either be a local MTA, which will in effect forward your mail on to the target MTA, or you must directly connect to the target MTA your self.

For example, hotmail has 4 MX servers :

mx1.hotmail.com
mx2.hotmail.com
mx3.hotmail.com
mx4.hotmail.com

Are you connecting to these MX servers, or to your own local MTA ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I have the same problem and only with hotmail. After many tests, I have gotten that I receive mails to hotmail, but only text/plain. If i set the header "text/html", i don't receive more email.

My code is:

import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.*;
import javax.naming.*;
import javax.naming.directory.*;
import javax.activation.*;

class Hotmail
{
public static void main(String[] args)
{
String body = "<html><body><table><tr><td>Hola maricón?</td></tr></table></body></html>";
//sendMail("root@plexus.kodics.de","jlconde@globalia-sistemas.com",
"globalia-sistemas.com","hotmail",body);
sendMail("root@plexus.kodics.de","jlconde15@hotmail.com",
"mail.hotmail.com","hotmail",body);
//sendMail("root@plexus.kodics.de","lillae25@yahoo.de","mx1.mail.
yahoo.com","hotmail",body);
}

public static void sendMail(String from,String to,String server,String subject,String text)
{

boolean debug = true;

//text = "Hello User,\nThese are your registration details.";

try
{
// create some properties and get the Session
Properties props = new Properties();
//props.put("mail.smtp.host", "mail.pivotal-systems.com");
props.put("mail.smtp.host", server);

Session session2 = Session.getInstance(props, null);
session2.setDebug(debug);

// create a message
MimeMessage msg = new MimeMessage(session2);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
//msg.setText(text);

try
{
//msg.setDataHandler(new DataHandler(new ByteArrayDataSource(text,"text/html;charset=iso-8859-1")));
//msg.setHeader("Content-Transfer-Encoding", "base64");
msg.setDataHandler(new DataHandler( new ByteArrayDataSource(text, "text/plain;charset=iso-8859-1")));
}
catch (Exception e)
{
System.out.println("Error codificando base64");
}



msg.setHeader("Return-Path", "root@plexus.kodics.de");
msg.setHeader("X-Mailer", "sendhtml");
msg.setHeader("X-Priority", "3");
// set the Date: header
msg.setSentDate(new Date());


// send the message
Transport transport = session2.getTransport("smtp");
//transport.connect("mail.pivotal-systems.com", "martin@pivotal-systems.com", "free4all");
transport.connect();
msg.saveChanges();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

} catch (MessagingException mex) {
Exception e = mex.getNextException();
System.out.println("ERROR->"+mex.getMessage());
try
{
String error = e.toString();
String code = error.substring(error.indexOf(":")+2);
String msg = code.substring(code.indexOf(" ")+1);
code = code.substring(0,code.indexOf(" "));
System.out.println("CODE->"+code);
System.out.println("MSG->"+msg);
}
catch (Exception ex)
{
System.out.println("CODE->");
System.out.println("MSG->");
}

}
}

}
 
Certain mail servers won't pass on mail that has more than 6-10 recipients. Yahoo! won't, so it's possible hot mail won't either.
 
I determined that the problem with hotmail was that it filtered out emails with some kind of html tags. I made a W3 HTML compatible email, removing many microsoft office tags on the way - which maybe be the actuall cause of the problem.

C.
 
Yep, I'd bet on the Office tags being the problem!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top