Send an HTML formatted message with a picture attachment in Java Mail
Source: Internet
Author: User
Use Java mail to send a message with a picture in HTML format, there are generally two ways to display a picture.
1. The picture exists on the server, we only need to put the picture in the server's link.
This hairstyle is simpler, but there are some drawbacks to this approach:
Images do not support offline browsing;
Each time browsing the mail needs to visit the Web service, increases the server burden;
If the image is in binary form in the database or dynamically generated, it can not be effectively resolved.
2. Send pictures as attachments, which is very useful in situations where pictures exist in the database.
This article also mainly discusses this situation.
For a basic knowledge of Java mail, see/softchannel/72348977504190464/20020713/1620276.shtml,
This article is not introduced.
When it comes to the attachment in the mail, I have to say the Minimultipart class, which provides an additional implementation method to add to the message.
A content-type is a message object that is set to Minimultipart for a content type.
The Minimultipart class is a container class that contains objects of type MimeBodyPart.
We can use a MimeBodyPart to contain the HTML source code, and the other mimebodypart contain binary picture attachments.
But here's an unexpected situation, let's look at the MimeBodyPart initialization code:
MimeBodyPart MDP = new MimeBodyPart (); Create a new bodypart to store attachments
DataHandler dh = new DataHandler (...);
Mdp.setdatahandler (DH); Set the content to DH for the BodyPart object
Where a subclass of DataSource is required when DataHandler is initialized
/** from JDK Doc */
Public DataHandler (DataSource DS)
Create a DataHandler instance referencing the specified DataSource.
The data exists in a byte stream form. The DataSource would provide a inputstream to access the data.
Parameters:ds-the DataSource
In JDK1.4, DataSource is a interface,datasource with only Mimepartdatasource, Urldatasource, Filedatasource three subclasses,
None of these three classes can create an instance directly from the binary stream (byte[). Of course, we can write the binary stream in memory to the file, and then let Filedatasource read in.
But this will bring the server to the external hard drive read and write, if the operation is frequent, will cause the server performance drop. Luckily, I found a class on the Internet that can directly create an instance directly with a binary stream.
Import java.io.*;
Import javax.activation.*;
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.