Java email development-JavaMail (2)

Source: Internet
Author: User

This blog describes how to send emails to multiple recipients and how to use the Authenticators object for user authentication.

You can specify the recipient in two ways. In the previous blog, the recipient is temporarily specified when an email is sent. You can also specify the recipient in the Message object.

 

1 message.addRecipient(Message.RecipientType.TO,new InternetAddress(” 995812509@99.com ”));

 

 

This is only sent to one recipient, but how can I handle multiple recipients? There are two ways to handle the problem.

1. When sending an email, the sendMessage () method of Transport uses an array to specify the recipient. In this case, you only need to add more recipient addresses.

2. When using the Message object to add recipients, we can use the parse (String string) method of the InternetAddress object. This method returns the InternetAddress array, which can also be sent to multiple recipients.

 

We know that we must perform authorization verification when developing JavaMail. Authorization verification aims to prevent others from sending emails randomly and reduce the generation of spam. In the previous blog, I used the Transport connect (host, port, username, password) method for verification. In fact, we can also perform verification when obtaining the Session object. There are two methods in the Session object: getDefaultInstance (prop, authenticator) and getInstance (prop, authenticator). Both methods have a common parameter authenticator, which is an Authenticator object. The Authenticator object is used to verify information and complete Authorization verification. The Authenticator object contains the getPasswordAuthentication () method. This method returns a PasswordAuthentication object. The PasswordAuthentication object has two methods: getPassword () and getUserName () that is to say, we will encapsulate password and userName in the PasswordAuthentication object, through these two methods we can get the user name and password. You can complete user information verification.

Example:

1 public class JavaMail_02 {2 public static void main (String [] args) throws Exception {3 Properties props = new Properties (); 4 props. setProperty ("mail. smtp. auth "," true "); 5 props. setProperty ("mail. transport. protocol "," smtp "); 6 props. setProperty ("mail. host "," smtp.163.com "); 7 8 Session session = Session. getInstance (props, 9 new Authenticator () {10 protected PasswordAuthentication getPasswordAuthentication () {11 return new PasswordAuthentication ("********", "*********"); 12} 13}); 14 session. setDebug (true); 15 16 Message msg = new MimeMessage (session); 17 msg. setFrom (new InternetAddress ("chenssy995812509@163.com"); 18 19 msg. setSubject ("JavaMail test program... "); 20 msg. setContent ("<span style = 'color: red'> This is my second javaMail test program .... </span> "," text/html; charset = gbk "); 21 // msg. setRecipients (RecipientType. TO, new Address [] {new InternetAddress ("1111 @ qq.com"), new InternetAddress ("2222@qq.cpm")}); 22 msg. setRecipients (RecipientType. TO, InternetAddress. parse ("995812509@qq.com, 1247723213@qq.com"); 23 24 Transport. send (msg); 25} 26 27}

 

 

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.