Javamail in-depth experience Development 2: Architecture of javamail and complex email sending

Source: Internet
Author: User

 

I. The whole process of email transmission:

Ii. Architecture and API classification of javamail

Javamail APIs are generally divided into the following three categories by function:
API for creating and parsing mail content: the message class is the core API for creating and parsing mail, and its instance object represents an email.
Mail sending API: The transport class is the core API class for sending mail. Its instance object represents the mail sending object that implements a mail sending protocol, such as the SMTP protocol.
Mail receiving API: The store class is the core API class for receiving mail. Its instance object represents the mail receiving object that implements a mail receiving protocol, such as POP3 protocol.
Session
The session class is used to define the environment information required by the entire application and collect the session information for establishing a network connection between the client and the mail server, for example, the host name and port number of the email server, and the mail sending and receiving protocols used. The Session Object builds the transport and store objects used for sending and receiving emails based on the information, and provides information support when the client creates a message object.

Iii. Mail sending process:

4. Use javamail to send a complex email with images and attachments:

 

APIS related to the mail organization structure:

 

The mimemessage class indicates the entire email.
The mimebodypart class indicates a mime message of a mail.
The mimemultipart class indicates a combined mime message composed of multiple MIME messages.

 

Structure of complex Emails:

 

 

 

Email sending steps;

1. Build a development environment: Prepare jar packages for sending emails:

Mail. Jar
Activation. Jar (JAF): (optional. This package is not required if jdk6.0 + is used)

2. Write the code implementation as follows:

Package COM. itheima; import Java. io. fileoutputstream; import Java. util. properties; import javax. activation. datahandler; import javax. activation. filedatasource; import javax. mail. authenticator; import javax. mail. message; import javax. mail. passwordauthentication; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetaddress; import javax. mail. internet. mimebodypart; import Jav Ax. mail. internet. mimemessage; import javax. mail. internet. mimemultipart; // you can send a complex email to yourself. Public class sendmessage {public static void main (string [] ARGs) throws exception {properties props = new properties (); // environment variable props. setproperty ("mail. host "," smtp.163.com "); props. setproperty ("mail. transport. protocol "," SMTP "); props. setproperty ("mail. SMTP. auth "," true "); Session session = session. getinstance (props, n EW authenticator () {@ overridepublic passwordauthentication getpasswordauthentication () {return New passwordauthentication ("Laotian", "123") ;}}); Session. setdebug (true); // start debugging: Monitoring SMTP protocol Transport Ts = session. gettransport (); Ts. connect ("smtp.163.com", "Laotian", "123"); // connect to the mail sending server. // create the message object mimemessagemimemessage message = new mimemessage (session); message. setsubject ("tttttttt"); message. setfrom (New inte Rnetaddress ("laotian@163.com"); message. setrecipient (message. recipienttype. to, new internetaddress ("laotian@163.com"); // create each part of the message, set type, with the ID of the settings. Mimebodypartmimebodypart Part1 = new mimebodypart (); part1.setcontent ("AAA  AAA", "text/html "); mimebodypart Part2 = new mimebodypart (); // JAF: datahandler can automatically perceive the file type images/jpegdatahandler DH = new datahandler (New filedatasource ("D:/2.jpg ")); part2.setdatahandler (DH); part2.setcontentid ("mm"); // describes the relationship between various parts. mimemultipartmimemultipart multi = new mimemultipart (); Multi. addbodypart (Part1); Multi. addbodypart (Part2); Multi. setsubtype ("related"); // describe the link // Add it to message in mimemessage. setcontent (Multi); message. savechanges (); Ts. send (message, message. getallrecipients ());}}

 

 

 

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.