Multiple sends and user verification
The following will first introduce how to send emails to multiple recipients and how to use the Authenticators object to achieve user authentication.
When specifying a recipient, we can have two ways to specify it. The previous blog temporarily specified the recipient when sending an email, and it can actually be specified in the Message object.
message.addRecipient(Message.RecipientType.TO,new InternetAddress(" [email protected] ")); This is just for sending it to one recipient, but how to deal with it with multiple recipients? There are also two ways to deal with it.
1. When sending emails, the sendMessage() method of Transport uses an array to specify the recipient. At this time, we only need to add more recipient addresses to complete it.
2. When using the Message object to add recipients, we can use the parse(String string) method of the InternetAddress object. This method returns an InternetAddress array, which can also be sent to multiple recipients.
We know that when developing JavaMail, we must perform authorization verification. The purpose of authorization verification is to prevent others from sending emails arbitrarily and reduce the generation of spam.
We can check when obtaining the Session object. There are two methods in the Session object:
Both methods have a common parameter authenticator, which is an Authenticator object. The Authenticator object helps users to verify information and complete authorization verification. There is a getPasswordAuthentication() method in the Authenticator object. This method returns a PasswordAuthentication object. There are two methods in the PasswordAuthentication object: getPassword() and getUserName() that means we encapsulate password and userName in the PasswordAuthentication object. Through these two methods, we can get the user name and password. User information verification can be completed.
Examples are as follows:
public class JavaMail_02 { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.163.com"); Session session = Session.getInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication("******","*********"); } }); session.setDebug(true); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("[email protected]")); msg.setSubject("JavaMail test program..."); msg.setContent("<span style='color:red'>This is my second javaMail test program....</span>", "text/html;charset=gbk"); //msg.setRecipients(RecipientType.TO, new Address[]{new InternetAddress("1111@@qq.com"),new InternetAddress("[email protected]")}); msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]")); Transport.send(msg); } } Email with pictures and attachments
In actual emails, we generally involve more complex email structures, such as attachments, pictures, songs, etc. At this time, we must have a clear understanding of the structure of the email. Before developing composite email, you need to have a certain understanding of the structure of composite emails.
The above image shows the overall structure of a composite email, and we can see that a complex email consists of multiple parts. It has a header and body, but the body is not as simple as before, but consists of several parts. The head needs to serve as an indication, which needs to explain what kind of separator to use to separate the text and what combination relationships are used between the parts of the text. For the above email it consists of three parts, each with its own head and body, and the first part is also composed of two parts.
Combination relationship of composite email:
There are many combinations between the main text parts. The combination relationship is as follows:
alternative: select relationship. The above plain text and hypertext is a choice relationship.
related:Affiliated relationship. If the above hypertext text displays a picture, then when we send an email, we must include the picture in the email, which is the so-called embedded resource, which is for hypertext. So there is a relationship between the two of them.
mixed: Mixed relationship. Plain text, hypertext and embedded resources form a whole and they are juxtaposed with attachments, and there is a mixed relationship between the two.
API for composite mail organization structure:
The MimeMessage class represents the entire email.
The MimeBodyPart class represents a MiME message of the email.
The MimeMultipart class represents a combined MIME message composed of multiple MIME messages.
The following example: The email contains two attachments, the main part includes plain text and hypertext, and the hypertext means displaying a picture. The source code is as follows:
public class JavaMail_03 { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.163.com"); Session session = Session.getInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication("***","******"); } }); Message message = new MimeMessage(session); message.setSubject("third JavaMail test program"); message.setFrom(new InternetAddress("/""+MimeUtility.encodeText("Chen Ming")+"/"<[email protected]>")); message.setRecipients(RecipientType.TO, new Address[]{new InternetAddress("[email protected]")}); //Mail text MimeMultipart multipart = new MimeMultipart("mixed"); message.setContent(multipart); /* * The content of the message* Includes one email body and two attachments*/ MimeBodyPart content = new MimeBodyPart(); //Mail content MimeBodyPart attach1 = new MimeBodyPart(); //Attachment 1 MimeBodyPart attach2 = new MimeBodyPart(); //Attachment 2 //Add the email content to multipart multipart.addBodyPart(content); multipart.addBodyPart(attch1); multipart.addBodyPart(attch2); //Set attachment 1 DataSource ds1 = new FileDataSource("G://ebook//oracle password.txt"); DataHandler dh1 = new DataHandler(ds1); attach1.setDataHandler(dh1); attach1.setFileName("oracle.txt"); //Set attachment 2 DataSource ds2 = new FileDataSource("G://ebook//account.txt"); DataHandler dh2 = new DataHandler(ds2); attach2.setDataHandler(dh2); attach2.setFileName(MimeUtility.encodeText("account.txt")); /* * Set content (text) ----is a complex body* Including HTML text and displaying an image*/ MimeMultipart bodyMultipart = new MimeMultipart("related"); content.setContent(bodyMultipart); //Construct the text MimeBodyPart htmlBody = new MimeBodyPart(); MimeBodyPart gifBody = new MimeBodyPart(); bodyMultipart.addBodyPart(htmlBody); bodyMultipart.addBodyPart(gifBody); //Set the picture DataSource gifds = new FileDataSource("F://picture//picture//4.jpg"); DataHandler gifdh = new DataHandler(gifds); gifBody.setDataHandler(gifdh); gifBody.setHeader("Content-ID", "<"+gifds.getName()+">"); //gifBody.setHeader("Content-Location", "http://www.itcast.cn/logo.gif"); //Set HTML body htmlBody.setContent("<span style='color:red;font-size:16px'>This is my third JavaMail test! It includes attachments and pictures, which is a bit complicated...</span><br>" + "The displayed picture <img src='cid:4.jpg'/>", "text/html;charset=UTF-8"); message.saveChanges(); //Generate email Transport.send(message); } }