SoFunction
Updated on 2025-03-01

Methods for sending and receiving emails based on JavaMail API

This article describes the method of sending and receiving emails based on the JavaMail API. Share it for your reference. The details are as follows:

According to its function, APIs can usually be divided into the following three categories.

(1) API for creating and parsing email content: The Message class is the core API for creating and parsing emails, and its instance object represents an email.
(2) API for sending emails: The Transport class is the core API class for sending emails. Its instance object represents the email sending object that implements a certain email sending protocol, such as the SMTP protocol.
(3) API for receiving emails: The Store class is the core API class for receiving emails. Its instance object represents the email receiving object that implements a certain email receiving protocol, such as the POP3 protocol.

2. Session class

The Session class is used to define the environment information required by the entire application, as well as collecting session information for the client to establish a network connection with the mail server, such as the host name, port number, and the mail sending and receiving protocol used. Session objects build Transport and Store objects for mail transmission and reception based on this information, and provide information support when creating Message objects for clients.

3. Send a simple email using JavaMail

Creates a Session object that contains network connection information for the mail server.
Create a Message object representing the content of the message.
Create a Transport object, connect to the server, send a 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 fileProperties props=new Properties();
//Set the sending host name, sending protocol and whether to verify the email("","" );
("", "smtp");
("", "true");
//Get the object of sending and receiving mail environmentSession session=(props);
Message message=createMessage(session);
//Create an object to send emailTransport tsp=();
("jb51", "jb51");
(message, ());
();
}
public static Message createMessage(Session session) throws Exception{
//Create an email based on the environment objectMimeMessage message=new MimeMessage(session);
//Set email properties(new InternetAddress("jb51@"));
(,new InternetAddress("jb51@"));
("hello");
//Create emailMimeBodyPart text=new MimeBodyPart();
("Hello?","text/html;charset=utf-8");
//Set the description relationshipMimeMultipart mm=new MimeMultipart();
(text);
(mm);
();
return message;
}
}

(2) JavaMail email sending content and pictures

public class SendImageMail {
public static void main(String[] args) throws Exception {
//Properties fileProperties props=new Properties();
//Set the sending host name, sending protocol and whether to verify the email("","" );
("", "smtp");
("", "true");
//Get the object of sending and receiving mail environmentSession session=(props);
Message message=createMessage(session);
//Create an object to send emailTransport tsp=();
("jb51", "jb51");
(message, ());
();
}
public static Message createMessage(Session session) throws Exception{
MimeMessage message=new MimeMessage(session);
(new InternetAddress("jb51@"));
(,new InternetAddress("jb51@"));
("picture");
MimeBodyPart text=new MimeBodyPart();
("Is it good looking?<br/><img src='cid:'>","text/html;charset=utf-8");
MimeBodyPart image=new MimeBodyPart();
(new DataHandler(new FileDataSource("src//")));
("");
MimeMultipart mm=new MimeMultipart();
(text);
(image);
("related");
(mm);
();
return message;
}
}

(3) JavaMail mail sending content, pictures and attachments

public class SendAttchImageMail {
public static void main(String[] args) throws Exception {
//Properties fileProperties props=new Properties();
//Set the sending host name, sending protocol and whether to verify the email("","" );
("", "smtp");
("", "true");
//Get the object of sending and receiving mail environmentSession session=(props);
Message message=createMessage(session);
//Create an object to send emailTransport tsp=();
("jb51", "jb51");
(message, ());
();
}
public static Message createMessage(Session session) throws Exception{
MimeMessage message=new MimeMessage(session);
(new InternetAddress("jb51@"));
(,new InternetAddress("jb51@"));
("picture");
MimeBodyPart text=new MimeBodyPart();
("Is it good looking?<br/><img src='cid:'>","text/html;charset=utf-8");
MimeBodyPart image=new MimeBodyPart();
(new DataHandler(new FileDataSource("src//")));
("");
MimeBodyPart attch=new MimeBodyPart();
DataHandler dh=new DataHandler(new FileDataSource("src//Silent.mp3"));(dh);
String name=();
((name));
MimeMultipart mm=new MimeMultipart();
(text);
(image);
("related");
MimeBodyPart part=new MimeBodyPart();
(mm);
MimeMultipart m=new MimeMultipart();
(part);
(attch);
("mixed");
(m);
();
return message;
}
}

Note: The email address must be real

I hope this article will be helpful to everyone's Java programming.