Import javax. mail .*;
Import javax. mail. internet .*;
Import java. util .*;
Public class sendMail
{
Public static void main (String args []) throws Exception
{
String host = "smtp.sina.com.cn ";
String from = "javamail@sina.com ";
String to = "javamail@china.com ";
String username = "javamail ";
String password = "password ";
// Get system properties
// Properties props = System. getProperties (); this is the case in many examples. In fact, the following sentence is better and can be used in the applet.
Properties props = new Properties ();
// Setup mail server
Props. put ("mail. smtp. host", host );
Props. put ("mail. smtp. auth", "true "); //
// Get session
Session session = Session. getDefaultInstance (props );
// Watch the mail commands go by to the mail server
Session. setDebug (true );
// Define message
MimeMessage message = new MimeMessage (session );
Message. setFrom (new InternetAddress (from ));
Message. addRecipient (Message. RecipientType.,
New InternetAddress ());
Message. setSubject ("Hello JavaMail ");
Message. setText ("Welcome to JavaMail ");
// Send message
Message. saveChanges ();
Transport transport = session. getTransport ("smtp ");
Transport. connect (host, username, password );
Transport. sendMessage (message, message. getAllRecipients ());
Transport. close ();
}
}