Need Pure java code to send mails from microsoft exchange server to any application
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pratibha_Mahata
Posted On:   Wednesday, May 2, 2007 02:05 AM

How can i write a Pure java code to send mails from microsoft exchange server to any application.

Re: Need Pure java code to send mails from microsoft exchange server to any application

Posted By:   Anonymous  
Posted On:   Wednesday, May 2, 2007 03:17 AM

Hi Prathiba,


here is the sample code to send an email from java program.All that you need to run the program is put the mail.jar and activation.jar in classpath and send the
microsoft exchange server address as the first argument to this program and from and to addresses respectively




import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class MailExample {
public static void main(String args[]) throws Exception {
if (args.length != 3) {
System.err.println("Usage: java MailExample host from to");
System.exit(-1);
}

String host = args[0];
String from = args[1];
String to = args[2];

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session = Session.getDefaultInstance(props, null);

// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("The Subject");
message.setText("The Message");

// Send message
Transport.send(message);
}
}
About | Sitemap | Contact