Hi,
I'm trying to use JavaMail with MS exchange 2007 and am experiencing strange behavior.
I'm using a single email address to send test messages to for now. The problem is that my test emails go through erratically, i.e. about once in four messages go through. Sometimes, two consecutive messages will go through as well. I have turned on smtp debugging and it seems like JavaMail doesn't get a response from Exchange.... Sometimes it will hang on DATA, other times it will hang on EOF. After 5 mins (the timeout period, I think), JavaMail will give a socket exception.
Has anyone encountered a similar problem? Is my Exchange configuration to blame or am I not setting a flag in my mail client properly?
Any help would be appreciated.
Here is the code I'm using:
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Provider;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.SMTPAddressFailedException;
import com.sun.mail.smtp.SMTPAddressSucceededException;
import com.sun.mail.smtp.SMTPSendFailedException;
/**
*
*/
/**
* @author hsophie
*
*/
public class JavaMailTest {
/**
* @param args
*/
public static void main(String[] args) {
final Properties props = new Properties();
//print debug messages
props.put("mail.debug", "true");
//set the SMTP properties
props.put("mail.smtp.host", "xxxxx"); //server ip here
props.put("mail.smtp.port", "25");
//set the authentication related properties
//props.put("mail.smtp.auth", "true"); //exchange doesn't require auth
//props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.user", "frontdesk@xxxx.com"); //domain name here
props.put("mail.smtp.password", "xxxx"); //password here
//these will make it throw more meaningful exceptions for better email sending validation
props.put("mail.smtp.sendpartial", "true");
//props.put("mail.smtp.reportsuccess", "true");
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(props.getProperty("mail.smtp.user"),
props.getProperty("mail.smtp.password"));
}
};
System.out.println("Creating session...");
Session session = Session.getInstance(props, auth);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("frontdesk@xxxxx.com", "Front Desk"));
//add recipients
//add the first address which is a valid address
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(args[0]));
msg.setDisposition(MimeMessage.INLINE);
Date now = new Date(System.currentTimeMillis());
msg.setSentDate(now);
msg.setSubject(now.toString() + " JavaMail Test");
msg.setContent("<html><body><p>Hello there <br> How are <b>YOU</b></body></html>", "text/html");
Transport.send(msg);
/*
//tried using the following code, but no difference
msg.saveChanges();
SMTPTransport t = session.getTransport("smtp");
t.connect();
t.sendMessage(msg, msg.getAllRecipients());
t.close();
*/
}
catch (MessagingException mex) {
if (mex instanceof SMTPSendFailedException) {
SMTPSendFailedException sfe = (SMTPSendFailedException)mex;
System.out.println("SMTPSendFailedException was thrown: " + sfe.getMessage());
sfe.printStackTrace();
}
else {
System.out.println("Some other exception: " + mex.getMessage());
mex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Thanks.
Hammad.