The method of sending and receiving mail based on JavaMail API _java

Source: Internet
Author: User

This article describes a method for sending and receiving messages based on the JavaMail API. Share to everyone for your reference. Specifically as follows:

The 1.JavaMail API can usually be divided into three main categories according to its function division

(1) The Api:message class for creating and parsing mail content is the core API for creating and parsing messages, and its instance object represents an e-mail message.
(2) The Api:transport class that sends the message is the core API class that sends the message, and its instance object represents a message sending object that implements a mail delivery protocol, such as an SMTP protocol.
(3) The Api:store class that receives the message is the core API class that receives the message, and its instance object represents a message receiving object that implements a message receipt protocol, such as the POP3 protocol.

2. Session Class

The session class is used to define the environment information required by the entire application and to collect information about the sessions of the client that establish a network connection to the mail server, such as the host name of the mail server, the port number, the message sending and receiving protocols used, and so on. The session object builds the transport and store objects that are used to send and receive messages based on this information, and provides information support when creating a message object for the client.

3. Use JavaMail to send a simple message

Creates a session object that contains network connection information for the mail server.
Creates a Message object that represents the content of the messages.
Create a Transport object, connect to the server, send a message, and close the connection.

4. Examples

(1) JavaMail message only send content

public class SendMail {public static void main (string[] args) throws Exception {//Property file Properties Props=new properties (
);
Set the sending host name of the message, send the protocol and verify the Props.setproperty ("Mail.host", "smtp.sohu.com");
Props.setproperty ("Mail.transport.protocol", "SMTP");
Props.setproperty ("Mail.smtp.auth", "true");
Gets the object session session=session.getinstance (props) that sends the receiving mail environment;
Message Message=createmessage (session);
Create an object to send a message transport tsp=session.gettransport ();
Tsp.connect ("jb51", "jb51");
Tsp.sendmessage (Message, message.getallrecipients ());
Tsp.close (); Throws exception{///Create a message based on the Environment object (createmessage) public static messages (session session) MimeMessage Message=new Mimemessa
GE (session);
Set the message Properties Message.setfrom (New InternetAddress ("jb51@sohu.com"));
Message.setrecipient (message.recipienttype.to,new internetaddress ("jb51@sina.com"));
Message.setsubject ("Hello");
Create the mail body MimeBodyPart text=new mimebodypart ();
Text.setcontent ("Hello?", "Text/html;charset=utf-8");
Set description Relationship Mimemultipart mm=new mimemultipart (); Mm.Addbodypart (text);
Message.setcontent (mm);
Message.savechanges ();
return message;

 }
}

(2) JavaMail message send content and pictures

public class Sendimagemail {public static void main (string[] args) throws Exception {//Property file Properties Props=new Proper
Ties ();
Set the sending host name of the message, send the protocol and verify the Props.setproperty ("Mail.host", "smtp.sohu.com");
Props.setproperty ("Mail.transport.protocol", "SMTP");
Props.setproperty ("Mail.smtp.auth", "true");
Gets the object session session=session.getinstance (props) that sends the receiving mail environment;
Message Message=createmessage (session);
Create an object to send a message transport tsp=session.gettransport ();
Tsp.connect ("jb51", "jb51");
Tsp.sendmessage (Message, message.getallrecipients ());
Tsp.close (); Throws exception{MimeMessage Message=new MimeMessage (session), public static message CreateMessage (session session); Me
Ssage.setfrom (New InternetAddress ("jb51@sohu.com"));
Message.setrecipient (message.recipienttype.to,new internetaddress ("jb51@sina.com"));
Message.setsubject ("picture");
MimeBodyPart text=new MimeBodyPart ();
Text.setcontent ("Nice? <br/> 

(3) JavaMail messages send content, pictures, and attachments

public class Sendattchimagemail {public static void main (string[] args) throws Exception {//Property file Properties Props=new P
Roperties ();
Set the sending host name of the message, send the protocol and verify the Props.setproperty ("Mail.host", "smtp.sohu.com");
Props.setproperty ("Mail.transport.protocol", "SMTP");
Props.setproperty ("Mail.smtp.auth", "true");
Gets the object session session=session.getinstance (props) that sends the receiving mail environment;
Message Message=createmessage (session);
Create an object to send a message transport tsp=session.gettransport ();
Tsp.connect ("jb51", "jb51");
Tsp.sendmessage (Message, message.getallrecipients ());
Tsp.close (); Throws exception{MimeMessage Message=new MimeMessage (session), public static message CreateMessage (session session); Me
Ssage.setfrom (New InternetAddress ("jb51@sohu.com"));
Message.setrecipient (message.recipienttype.to,new internetaddress ("jb51@sina.com"));
Message.setsubject ("picture");
MimeBodyPart text=new MimeBodyPart ();
Text.setcontent ("Nice? <br/> 

Note: The email address must be true

I hope this article will help you with your Java programming.

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.