Description: Transfer from foreign blog : Welcome to the originalThis example is relatively new, easy to understand, worthy of novice reference
Take Gmail for example, note:
1. Send via TLS
1 PackageCom.mkyong.common;2 3 Importjava.util.Properties;4 5 ImportJavax.mail.Message;6 Importjavax.mail.MessagingException;7 Importjavax.mail.PasswordAuthentication;8 Importjavax.mail.Session;9 ImportJavax.mail.Transport;Ten Importjavax.mail.internet.InternetAddress; One ImportJavax.mail.internet.MimeMessage; A - Public classSendmailtls { - the Public Static voidMain (string[] args) { - - FinalString username = "[email protected]"; - FinalString password = "Password"; + -Properties props =NewProperties (); +Props.put ("Mail.smtp.auth", "true"); AProps.put ("Mail.smtp.starttls.enable", "true"); atProps.put ("Mail.smtp.host", "smtp.gmail.com"); -Props.put ("Mail.smtp.port", "587"); - -Session session =session.getinstance (Props, - NewJavax.mail.Authenticator () { - protectedpasswordauthentication getpasswordauthentication () { in return Newpasswordauthentication (username, password); - } to }); + - Try { the *Message message =NewMimeMessage (session); $Message.setfrom (NewInternetAddress ("[Email protected]"));Panax Notoginseng message.setrecipients (Message.RecipientType.TO, -Internetaddress.parse ("[Email protected]")); theMessage.setsubject ("Testing Subject"); +Message.settext ("Dear Mail Crawler," A+ "\ n \ Spam to my email, please!"); the + transport.send (message); - $System.out.println ("Done"); $ -}Catch(messagingexception e) { - Throw NewRuntimeException (e); the } - }Wuyi}
2. Send via SSL
1 PackageCom.mkyong.common;2 3 Importjava.util.Properties;4 ImportJavax.mail.Message;5 Importjavax.mail.MessagingException;6 Importjavax.mail.PasswordAuthentication;7 Importjavax.mail.Session;8 ImportJavax.mail.Transport;9 Importjavax.mail.internet.InternetAddress;Ten ImportJavax.mail.internet.MimeMessage; One A Public classSendmailssl { - Public Static voidMain (string[] args) { -Properties props =NewProperties (); theProps.put ("Mail.smtp.host", "smtp.gmail.com"); -Props.put ("Mail.smtp.socketFactory.port", "465"); -Props.put ("Mail.smtp.socketFactory.class", -"Javax.net.ssl.SSLSocketFactory"); +Props.put ("Mail.smtp.auth", "true"); -Props.put ("Mail.smtp.port", "465"); + ASession session =session.getdefaultinstance (Props, at NewJavax.mail.Authenticator () { - protectedpasswordauthentication getpasswordauthentication () { - return NewPasswordauthentication ("username", "password"); - } - }); - in Try { - toMessage message =NewMimeMessage (session); +Message.setfrom (NewInternetAddress ("[Email protected]")); - message.setrecipients (Message.RecipientType.TO, theInternetaddress.parse ("[Email protected]")); *Message.setsubject ("Testing Subject"); $Message.settext ("Dear Mail Crawler," +Panax Notoginseng"\ n \ Spam to my email, please!"); - the transport.send (message); + ASystem.out.println ("Done"); the +}Catch(messagingexception e) { - Throw NewRuntimeException (e); $ } $ } -}