wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes Sending mail through Exchange Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending mail through Exchange Server" Watch "Sending mail through Exchange Server" New topic

Sending mail through Exchange Server

mark babcock

Joined: Jun 26, 2002
Posts: 14
I need to be able to connect to exchange and send mail. How can I do this? Am I forced to revert to VB MAPI?
String to = "lemerde@hotmail.com";
String from = "lemerde@hotmail.com";
String host = ExchangeServer;
boolean debug = false;
String msgText1 = "Sending a file.\n";
String subject = "Sending a file";
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
// add the Multipart to the message
// set the Date: header
msg.setSentDate(new Date());
} catch (MessagingException mex) {
Exception ex = null;
if ((ex = mex.getNextException()) != null) {

Session session = Session.getDefaultInstance(props, null);
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

I noticed that in your props object, you are using "mail.smtp.host". I am assuming you took this from the examples. You need to replace that with the IP or DNS Host name of your Exchange Server.
I send mail through an Exchange Server all the time where I work with the JavaMail API. However, something else you might take note of, where I work, everything leaving through the server must match a specific Domain Name. For example, my FROM Address will always have to have "@intrustbank.com". I noted also that you were sending mail FROM a Hotmail Domain. Your Exchange Server could be blocking this outgoing domain. This keeps people from spamming through your server.

GenRocket - Experts at Building Test Data
I agree. Here's the link: http://aspose.com/file-tools
subject: Sending mail through Exchange Server
Similar Threads
How to Display Email with an image on Different Email Account Using java.mail API
File attachments using Java Mail API
How to Display an Email with an image on Different Email Account Using javax.mail
Attaching files using Java Mail API