You first need to add a Javax.mail-1.4.5.jar reference.
Then use the following main code to do it.
packageorg.lyk.main;Importjava.util.Properties;Importjavax.mail.Authenticator;Importjavax.mail.Message.RecipientType;Importjavax.mail.MessagingException;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;Importjavax.mail.Transport;Importjavax.mail.internet.AddressException;Importjavax.mail.internet.InternetAddress;Importjavax.mail.internet.MimeMessage; public classmain{ public Static voidMain (string[] Args)throwsException {Try { //Configure environment properties for sending messages FinalProperties props =NewProperties (); /** Available Properties: mail.store.protocol/mail.transport.protocol/mail.host */mail.user/mail.from */ //indicates that SMTP sends a message and authentication is requiredProps.put ("mail.smtp.auth", "true"); Props.put ("mail.smtp.host", "smtp.163.com"); //Sender's Account numberProps.put ("mail.user", "[email protected]"); //password to provide when accessing the SMTP serviceProps.put ("mail.password", "XXXX"); //Building authorization information for SMTP authenticationAuthenticator Authenticator =NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {//User name, PasswordString userName = Props.getproperty ("mail.user"); String Password= Props.getproperty ("mail.password"); return Newpasswordauthentication (userName, password); } }; //Create a mail session using environment properties and authorization informationSession mailsession =session.getinstance (props, authenticator); //Create a mail messageMimeMessage message =NewMimeMessage (mailsession); //Set Senderinternetaddress form =NewInternetAddress (props.getproperty ("mail.user")); Message.setfrom (form); //Set RecipientInternetAddress to =NewInternetAddress ("[email protected]"); Message.setrecipient (recipienttype.to, to); //set up ccinternetaddress cc =NewInternetAddress ("[email protected]"); Message.setrecipient (recipienttype.cc, CC); //set Secret send, Other recipients cannot see the BCC email address//internetaddress bcc = new internetaddress ("[email protected]"); //message.setrecipient (recipienttype.cc, bcc); //set the message headerMessage.setsubject ("test message"); //set the content body of a messageMessage.setcontent ("); //Send mailTransport.send (message); } Catch(Exception E) {e.printstacktrace (); } System.out.println ("///~ Main done"); }}
Java Send mail