Java Mail Mail send instance "move"

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.