JavaMail Introduction
JavaMail is a standard development class library provided by Sun for developers to implement mail delivery and reception in their applications, supporting commonly used messaging protocols such as SMTP, POP3, IMAP, and developers using JavaMail to write mail programs without having to consider the underlying communication details ( Socket), JavaMail also provides APIs that create a variety of message content in complex MIME formats. With JavaMail, we can implement software similar to Outlook, Foxmail. Although JavaMail (supported only JDK4 and above) is one of the Java APIs, it is not directly added to the JDK, so we need to download it separately. In addition, JavaMail relies on the JAF (JavaBeans Activation Framework), JAF has been merged into the JDK after Java6, and JDK5 's class libraries need to be downloaded separately.
Javamail:http://www.oracle.com/technetwork/java/javamail/index.html
JavaMail sending mail
The mail process can be referred to as: "Mail" telnet to send and receive mail procedures, in this case the main process is: first create a message (mail object), and then use Transport (mail Transfer object) to send mail to the mail server.
- Start by creating a new Maven project for Javaweb. Reference: "Maven" Eclipse uses MAVEN to create a Java Web project;
- In the Pom file, the introduction of the Javamail.jar package
- Create a new Demo1 class that uses transport non-static methods to send messages
1 PackageCom.hd.javamail;2 3 Importjava.util.Properties;4 5 Importjavax.mail.Address;6 ImportJavax.mail.Message;7 Importjavax.mail.Session;8 ImportJavax.mail.Transport;9 Importjavax.mail.internet.InternetAddress;Ten ImportJavax.mail.internet.MimeMessage; One A /** - * - * @authorH__d the * @date December 6, 2016 PM 7:01:27 - * - */ - Public classDemo1 { + - /** + * Send mail using transport non-static method A * Connect 163 service, send mail to QQ mailbox at */ - Public Static voidMain (string[] args)throwsException { - - //Properties -Properties Properties =NewProperties (); - //Setting authentication Properties inProperties.setproperty ("Mail.smtp.auth", "true"); - //set up communication protocols toProperties.setproperty ("Mail.transport.protocol", "SMTP"); + //Mail Environment Information -Session session =Session.getinstance (properties); the //debugging, printing information *Session.setdebug (true); $ Panax Notoginseng //Mail -Message message =NewMimeMessage (session); the //Theme +Message.setsubject ("test Message"); A //Sending Person theMessage.setfrom (NewInternetAddress ("[Email protected]")); + //content -Message.settext ("This is content"); $ $ //Message Transfer Object -Transport Transport =Session.gettransport (); - //Transport Connection: host,port,user,pass/host, port, user name, password theTransport.connect ("smtp.163.com", "[email protected]", "xxxxxx"); - //Send mailWuyiTransport.sendmessage (Message,NewAddress[] {NewInternetAddress ("[Email protected]") }); the - //Close Connection Wu transport.close (); - } About}
- Create a new Demo2 class that uses transport non-static methods to send messages
1 PackageCom.hd.javamail;2 3 Importjava.util.Properties;4 5 ImportJavax.mail.Authenticator;6 ImportJavax.mail.Message;7 ImportJavax.mail.Message.RecipientType;8 Importjavax.mail.PasswordAuthentication;9 Importjavax.mail.Session;Ten ImportJavax.mail.Transport; One Importjavax.mail.internet.InternetAddress; A ImportJavax.mail.internet.MimeMessage; - - /** the * - * @authorH__d - * @date December 6, 2016 PM 7:05:07 - * + */ - Public classDemo2 { + A /** at * Send mail using transport static method - * Connect 163 service, send mail to multiple QQ mailbox - * @paramargs - * @throwsException - */ - Public Static voidMain (string[] args)throwsException { in //Property Object -Properties Properties =NewProperties (); to //open Debug Debug, print information +Properties.setproperty ("Mail.debug", "true"); - //The sending server requires authentication theProperties.setproperty ("Mail.smtp.auth", "true"); * //send server port, can not be set, default is $Properties.setproperty ("Mail.smtp.port", "25");Panax Notoginseng //Send mail Agreement name -Properties.setproperty ("Mail.transport.protocol", "SMTP"); the //set the mail server host name +Properties.setproperty ("Mail.host", "smtp.163.com"); A //Environmental Information theSession session = Session.getinstance (properties,NewAuthenticator () { + @Override - protectedpasswordauthentication getpasswordauthentication () { $ //set the account information in session, transport send the message will use $ return NewPasswordauthentication ("[Email protected]", "xxxxx"); - } - }); the - //Create a Mail objectWuyiMessage message =NewMimeMessage (session); the //Set Theme -Message.setsubject ("Chinese theme")); Wu //Sender -Message.setfrom (NewInternetAddress ("[Email protected]")); About //Multiple Recipients $Message.setrecipients (recipienttype.to, Internetaddress.parse ("[Email protected],[email protected]")); - //cc Person -Message.setrecipient (recipienttype.cc,NewInternetAddress ("[Email protected]")); - //Dark Send People AMessage.setrecipient (RECIPIENTTYPE.BCC,NewInternetAddress ("[Email protected]")); + //HTML content theMessage.setcontent ("<span style= ' color:red ' > Chinese hehe </span>", "Text/html;charset=utf-8"); - $ //Connect to mail server, send mail, close connection, all done the transport.send (message); the the } the}
"Mail" JavaMail introduction and send mail (i)