The example of this article describes the method based on the Javamail API. Share it for everyone for your reference. The specifics are as follows:
1. Javamail API can usually be divided into three categories as follows by their functions
(1) API: MESSAGE class is the core API of creating and parsing emails. Its instance object represents an email.
(2) The API of sending the email: The Transport class is the core API class that sends the email. Its instance object represents an email sending object of a mail sending protocol, such as the SMTP protocol.
(3) API that receives email: The Store class is the core API class that receives the mail. Its instance object represents the mail receiving object of the mail receiving protocol, such as the POP3 protocol.
2. Session class
The session class is used to define the environmental information required by the entire application, as well as collecting session information for clients to establish a network connection with the email server, such as the host name of the mail server, the port number, the email sending and receiving protocol used. The Session object provides information support for the Transport and Store objects used for mail and receiving for mail, as well as information support for the client.
3. Use Javamail to send a simple mail
Create a session object with network connection information containing mail server.
Create MESSAGE objects representing the content of the mail.
Create the Transport object, connect to the server, send Message, and close the connection.
4. Example
(1) Javamail email only sends content
Public Class Sendmail {Public Static Void Main (String [] ARGS) Throws Exception {// Properties Properties (); Verify props.setproperty ("Mail. host "," smtp.sohu.com "); props.setproperty (" Mail.transport.protocol "," smtp "); props.setproperty (" mail.smtp.auth ");" true "); The object of the email environment session session = session.getInstance (props); Message Message = CreateMessage (session); // Create the object of the email transport = session.gettransport ( ); tsp.connect ("jb51", "jb51" ); *Sp.SendMessage (Message, Message.getallRecipients ()); TSP.Close ();} Public Static Creatersage (Session Session) Throws ON {// Create an email MIMEMESSAGE Message = New Mimemessage (Session) according to environmental objects ; // Set mail attributes Message.setfrom (New InternetAddress ("[email protected]"); MESSAGE.SetRcipient ("" @sina.com "); Message.Setsubject ( "Hello"); // Create MIMEBODYPART Text = New MimebodyPart (); Text.SetContent ("Hello?", "Text/HTML; Charset = UTF-8"); = New Mimemultipart (); mm.addbodypart (text); Message.setContent (mm); Message.saveChanges (); Return Message;}}}
(2) Javamail email sends content and pictures
Public Class SendImageMail {Public Static Void Main (String [] ARGS) Throws Exception {// Properties Propes = New Properties (); Send an agreement and whether to verify the Props.setProperty ("Mail. host "," smtp.sohu.com "); props.setproperty (" Mail.transport.protocol "," smtp "); props.setproperty (" mail.smtp.auth ");" true "); The object of the email environment session session = session.getInstance (props); Message Message = CreateMessage (session); // Create the object of the email transport = session.gettransport ( ); tsp.connect ("jb51", "jb51" ); *Sp.SendMessage (Message, Message.getallRecipients ()); TSP.Close ();} Public Static Creatersage (Session Session) Throws on {MimeMessage Message = New MimeMessage (SessIn); Message.setFrom (New Internetaddress ("" [email protected] "); MESSAGE.SetRcipient (MESSAGE.RCIPIENTTYPE.TO, New Internetaddress (" [email protected] "); dypart text = new mimebodypart (); text .setContent ("Is it good? <br/> <IMG SRC = 'CID: xx.jpg'>", "Text/HTML; Charset = UTF-8"); ahandler ( New Datahandler (New Filedatasource ("SRC // F.jpg")); Image.setContentid ("xx.jpg"); EXT); mm.addbodypart (image) ; Mm.setSubtype ("Related"); MESSAGE.SetContent (MM); Message.saveChanges (); Return Message;}}
(3) Javamail email sends content, pictures and attachments
Public Class SendattchimageMail {Public Static Void Main (String [] ARGS) Throws Exception {// Properties Propers = New Properties (); // Machine name, sending protocol, and verifying props.Setproperty ("Mail. host "," smtp.sohu.com "); props.setproperty (" Mail.transport.protocol "," smtp "); props.setproperty (" mail.smtp.auth ");" true "); The object of the email environment session session = session.getInstance (props); Message Message = CreateMessage (session); // Create the object of the email transport = session.gettransport ( ); tsp.connect ("jb51", "jb51" ); *Sp.SendMessage (Message, Message.getallRecipients ()); TSP.Close ();} Public Static Creatersage (Session Session) Throws on {MimeMessage Message = New MimeMessage (SessIn); Message.setFrom (New Internetaddress ("" [email protected] "); MESSAGE.SetRcipient (MESSAGE.RCIPIENTTYPE.TO, New Internetaddress (" [email protected] "); dypart text = new mimebodypart (); text .setContent ("Is it good? <br/> <IMG SRC = 'CID: xx.jpg'>", "Text/HTML; Charset = UTF-8"); ahandler ( New Datahandler (New Filedatasource ("SRC // F.jpg")); Image.setContentid ("xx.jpg"); W Datahandler (New FileDataSource ("SRC // Silent .mp3 ")); Attch.setdatahandler (dh); string name = dh.getName (); attch.setFilename (mimeutility.encodetext (name)); = New Mimemultipart (); mm.addbodypart (text) ; mm.addbodypart (image); mm.setsubtype ("related"); ultipart (); m.addbodypart (part); m.addbodypart (Attch); m.setsubtype ("mixed"); Message.SetContent (m); Message.saveChanges (); Return Message;}}
Note: The mailbox address must be real
It is hoped that this article is helpful to everyone's Java program design.