aspose file tools*
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
Author

Sending mail through Exchange Server

mark babcock
Greenhorn

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);
try
{
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// 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));
mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}


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

Joined: Jul 11, 2001
Posts: 15286
    
    6

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 - A Test Data Generation Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending mail through Exchange Server
 
Similar Threads
How to Display an Email with an image on Different Email Account Using javax.mail
Attaching files using Java Mail API
How to Display Email with an image on Different Email Account Using java.mail API
File attachments using Java Mail API