JavaMail tutorial creating mail (2)

The JavaMail API uses javax mail. Message class represents a message. Message class is an abstract class, so we need to use its subclass javax mail. internet. Mimemessage class is used to create the instance object of message class. If we create a simple text message, mimemessage class can meet our needs. However, if we need to create a complex message containing embedded resources or attachments, we need to use mimemessage and javax in JavaMail API mail. internet. Mimebodypart and javax mail. internet. Mimemultipart and other classes.

1. The mimemessage class represents the entire message

2. The mimebodypart class represents a mime message of a mail

3. The mimemultipart class represents a combined mime message composed of multiple mime messages.

The working relationship of these three classes is shown in the following figure:

Although application developers usually only need to use the three main classes mimemessage, mimebodypart and mimemultipart when using the JavaMail API to create mail content, it is also necessary to understand their class inheritance relationship. The following figure lists the inheritance relationship and common methods of these three classes.

Now use javax mail. internet. Mimemessage class to create a simple text message,

We use the email client (Foxmail here) to open the test.eml file under disk C. you can see the following information, indicating that our email was created successfully.

Let's briefly explain some of the classes that appear above

1. Mimemessage class: as described above, mimemessage is a specific implementation class of message class, which is used to create an instance object of message class. Here, the constructor passes in a session object as a parameter;

2. Session class: this object is used to collect the network connection information between the client and the mail server and define the environment information required by the whole mail program. This information is saved in the session object as the attribute of the session object, which uses Java util. The properties object obtains the mail server, user name, password information and shared information to be used by the whole application. Since the construction method of the session class is private, we use the static factory method getdefaultinstance () provided by the session class to obtain a default session object;

3. Properties class: this class represents a persistent property set, which is used to store relevant key value pair information as parameters to create a session object. Here, an empty set is constructed as parameters;

4. Internetaddress class: this class is a subclass of the abstract class address class, which is used to create an email address;

5. Recipienttype class: this class is an internal class of message class. This class has three static variables. To represents the recipient, CC represents the CC (the recipient knows the CC), and BCC represents the Bcc (the recipient does not know the BCC).

The above email only contains simple text. Sometimes we need to use HTML files to enrich our email body, such as using HTML tags to typeset the email body, and using HTML tags to introduce some pictures or sounds into the email body. The following code creates an email containing HTML format

We use Foxmail to open test EML, as shown below

The difference from the previous code is that when setting the mail content, we use the setcontent method instead of the settext method, and specify that the MIME type of the mail body is text / HTML.

Now we have learned to create an email with HTML tags, but sometimes we may need to insert some pictures into the email to express our meaning more intuitively, so we need to use mimemultipart class and mimebodypart class at this time

The code seems a little complicated. Let's take another look at the relationship diagram between the three classes mentioned above: mimemessage, mimememultipart and mimebodypart, which will make it easier to understand the above code. Let's open picturemessage EML, the following information shows that we have successfully created an email with a picture, and the picture is included in the email as an attachment

Mimemultipart has two constructors,

The first is parameterless, and the default MIME type of the instance object is mixed. The second is to formulate a type to create the instance object of mimemultipart class, which has three common types: mixed, related and alternative. The combination relationship of these three types in mime is as follows:

Now we use the combination relationship shown above to create an email with attachments and embedded resources in the body

Open complexmessage EML file, you can see the following contents:

Now we have learned how to create a plain text email, an email with HTML format, and an email with embedded pictures and attachments. The next article will introduce how to send mail. The third introduction to JavaMail is sending mail

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>