The method of sending and receiving mail based on JavaMail API

  • 2020-04-01 04:02:41
  • OfStack

This article illustrates the method of sending and receiving mail based on the JavaMail API. Share with you for your reference. The details are as follows:

1. According to its functions, the JavaMail API can be generally divided into the following three categories

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

2. The Session classes

The Session class is used to define the environment information needed for the entire application and to collect the Session information for the client to establish a network connection with the mail server, such as the host name of the mail server, the port number, the adopted mail sending and receiving protocol, and so on. The Session object USES this information to build Transport and Store objects for sending and receiving mail, and to provide information support for clients when they create Message objects.

3. Use JavaMail to send a simple email

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

Example 4.

(1) JavaMail only sends content


public class SendMail {
public static void main(String[] args) throws Exception {
//Properties file
Properties props=new Properties();
//Sets the sending host name, sending protocol, and whether to verify the message
props.setProperty("mail.host","smtp.sohu.com" );
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
//Gets the object that sends and receives the mail environment
Session session=Session.getInstance(props);
Message message=createMessage(session);
//Create the object to send the message to
Transport tsp=session.getTransport();
tsp.connect("jb51", "jb51");
tsp.sendMessage(message, message.getAllRecipients());
tsp.close();
}
public static Message createMessage(Session session) throws Exception{
//Create a message based on the environment object
MimeMessage message=new MimeMessage(session);
//Set mail properties
message.setFrom(new InternetAddress("jb51@sohu.com"));
message.setRecipient(Message.RecipientType.TO,new InternetAddress("jb51@sina.com"));
message.setSubject("hello");
//Create 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 sends contents and pictures


public class SendImageMail {
public static void main(String[] args) throws Exception {
//Properties file
Properties props=new Properties();
//Sets the sending host name, sending protocol, and whether to verify the message
props.setProperty("mail.host","smtp.sohu.com" );
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
//Gets the object that sends and receives the mail environment
Session session=Session.getInstance(props);
Message message=createMessage(session);
//Create the object to send the message to
Transport tsp=session.getTransport();
tsp.connect("jb51", "jb51");
tsp.sendMessage(message, message.getAllRecipients());
tsp.close();
}
public static Message createMessage(Session session) throws Exception{
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress("jb51@sohu.com"));
message.setRecipient(Message.RecipientType.TO,new InternetAddress("jb51@sina.com"));
message.setSubject(" The picture ");
MimeBodyPart text=new MimeBodyPart();
text.setContent(" Good-looking? ?<br/><img src='cid:xx.jpg'>","text/html;charset=utf-8");
MimeBodyPart image=new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src//f.jpg")));
image.setContentID("xx.jpg");
MimeMultipart mm=new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
message.setContent(mm);
message.saveChanges();
return message;
}
}

(3) JavaMail sends contents, pictures and attachments


public class SendAttchImageMail {
public static void main(String[] args) throws Exception {
//Properties file
Properties props=new Properties();
//Sets the sending host name, sending protocol, and whether to verify the message
props.setProperty("mail.host","smtp.sohu.com" );
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
//Gets the object that sends and receives the mail environment
Session session=Session.getInstance(props);
Message message=createMessage(session);
//Create the object to send the message to
Transport tsp=session.getTransport();
tsp.connect("jb51", "jb51");
tsp.sendMessage(message, message.getAllRecipients());
tsp.close();
}
public static Message createMessage(Session session) throws Exception{
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress("jb51@sohu.com"));
message.setRecipient(Message.RecipientType.TO,new InternetAddress("jb51@sina.com"));
message.setSubject(" The picture ");
MimeBodyPart text=new MimeBodyPart();
text.setContent(" Good-looking? ?<br/><img src='cid:xx.jpg'>","text/html;charset=utf-8");
MimeBodyPart image=new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src//f.jpg")));
image.setContentID("xx.jpg");
MimeBodyPart attch=new MimeBodyPart();
DataHandler dh=new DataHandler(new FileDataSource("src//There was silence.
attch.setDataHandler(dh);
String name=dh.getName();
attch.setFileName(MimeUtility.encodeText(name));
MimeMultipart mm=new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
MimeBodyPart part=new MimeBodyPart();
part.setContent(mm);
MimeMultipart m=new MimeMultipart();
m.addBodyPart(part);
m.addBodyPart(attch);
m.setSubType("mixed");
message.setContent(m);
message.saveChanges();
return message;
}
}

Note: the email address must be real

I hope this article has been helpful to your Java programming.


Related articles: