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 ();
}
}