SimpleSendMessage. java
Import java. util .*;
Import javax. mail .*;
Import javax. mail. internet .*;
Import javax. activation .*;
Public class SimpleSendMessage {
Public static void main (String [] args ){
// Collect the necessary information to send a simple message
// Make sure to replace the values for host, to, and from
// Valid information.
// Host-must be a valid smtp server that you currently have
// Access.
// To-whoever is going to get your email
// From-whoever you want to be. Just remember that allow smtp
// Servers will validate the domain of the from address
// Before allowing the mail to be sent.
String host = "server.myhost.com ";
String to = "YourFriend@somewhere.com ";
String from = "MeMeMe@myhost.com ";
String subject = "JSP Rules! ";
String messageText = "I am sending a message using"
+ "JavaMail API. \ nI can include any text that I want .";
Boolean sessionDebug = false;
// Create some properties and get the default Session.
Properties props = System. getProperties ();
Props. put ("mail. host", host );
Props. put ("mail. transport. protocol", "smtp ");
Session session = Session. getDefaultInstance (props, null );
// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// Will.
Session. setDebug (sessionDebug );
Try {
// Instantiate a new MimeMessage and fill it with
// Required information.
Message msg = new MimeMessage (session );
Msg. setFrom (new InternetAddress (from ));
InternetAddress [] address = {new InternetAddress ()};
Msg. setRecipients (Message. RecipientType. TO, address );
Msg. setSubject (subject );
Msg. setSentDate (new Date ());
Msg. setText (messageText );
// Hand the message to the default transport service
// For delivery.
Transport. send (msg );
}
Catch (MessagingException mex ){
Mex. printStackTrace ();
}
}
}