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