• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending mail through Exchange Server  RSS feed

mark babcock
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!