First, RFC882 document simple description
The RFC882 document sets out how to write a simple message (plain text message), a simple message that contains two parts of the message header and the body of the message, separated by a blank line between the message headers and the body of the message.
The message headers contain the following:
From field --for indicating sender
To field --for indicating recipients
Subject Field --used to describe the message subject
cc Field -CC, copy to another recipient while sending the message to the recipient, and the recipient can see who the message was copied to
bcc field --a secret send message to another recipient while sending it to the recipient, and the recipient cannot see who the message was sent to
The message body refers to the specific content of the message.
Second, the MIME protocol simple Introduction
In our actual development, a message may contain pictures, and may contain attachments, in which case, the RFC882 document the format of the message can not meet the requirements.
The MIME protocol is an upgrade and a complement to the RFC822 document, which describes how to produce a complex message. Usually we call a MIME message the message described by the MIME protocol. The data described by the MIME protocol is called a MIME message.
For a complex message, if you include multiple different data, the MIME protocol provides that you want to delimit multiple pieces of data using a separator line, and use the Content-type header field to describe the type of data and the relationship between multiple data.
Create and send messages using JavaMail
The message created by JavaMail is based on a MIME protocol. Therefore, you can use JavaMail to create complex messages that contain pictures, including attachments.
3.1, the JavaMail API simple Introduction
3.2. Create Mail Send test project
3.3. Send a simple message containing only text
Package me.gacl.main;
Import java.util.Properties;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage; /** * @ClassName: Sendmail * @Description: Send email * @author: Lonely Wolf * @date: 2015-1-12 Afternoon 9:42:56 * * * * * * public class Sendmai L {/** * @param args * @throws Exception/public static void main (string[] args) throws Exception {Propertie
s prop = new Properties ();
Prop.setproperty ("Mail.host", "smtp.sohu.com");
Prop.setproperty ("Mail.transport.protocol", "SMTP");
Prop.setproperty ("Mail.smtp.auth", "true");
5 Steps to send a message using JavaMail/1, create session Sessions session = Session.getinstance (prop);
Turn on debug mode for session so that you can view the running status of the program sending email session.setdebug (true);
2, through session to get transport object Transport ts = Session.gettransport ();
3, use the mailbox username and password to connect to the mail server, send a message, the sender needs to submit the user name and password of the mailbox to the SMTP server, the user name and password are authenticated before they can normally send the message to the recipient.
Ts.connect ("smtp.sohu.com", "GaCl", "Mailbox Password");
4. Create mail Message message = Createsimplemail (session);
5, Send mail ts.sendmessage (message, message.getallrecipients ());
Ts.close (); /** * @Method: Createsimplemail * @Description: Create a message that contains only text * @Anthor: Aloof Wolf * @param session * @return * @th Rows Exception/public static MimeMessage Createsimplemail (Sessions session) throws Exception {//Create mail Object Mimemessa
GE message = new mimemessage (session);
Indicates the sender of the message Message.setfrom (new internetaddress ("gacl@sohu.com")); Indicates the recipient of the message, and now the sender and the recipient are the same, and that is to send themselves Message.setrecipient (Message.RecipientType.TO, New InternetAddress ("
Gacl@sohu.com "));
The title of the message Message.setsubject ("Simple message containing text only"); The text content of the message message.setcontent ("Hello!").
"," Text/html;charset=utf-8 ");
Returns the creation of a good mail object return message;
}
}
3.4, sending messages with inline pictures
Package me.gacl.main;
Import Java.io.FileOutputStream;
Import java.util.Properties;
Import Javax.activation.DataHandler;
Import Javax.activation.FileDataSource;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart; /** * @ClassName: Sendmail * @Description: Send email * @author: Lonely Wolf * @date: 2015-1-12 Afternoon 9:42:56 * * * * * * public class Sendmai L {/** * @param args * @throws Exception/public static void main (string[] args) throws Exception {Propertie
s prop = new Properties ();
Prop.setproperty ("Mail.host", "smtp.sohu.com");
Prop.setproperty ("Mail.transport.protocol", "SMTP");
Prop.setproperty ("Mail.smtp.auth", "true");
5 Steps to send a message using JavaMail/1, create session Sessions session = Session.getinstance (prop);
Turn on debug mode for session so that you can view the running status of the program sending email session.setdebug (true); 2. Through Session Get Transport Object Transport ts = Session.gettransport ();
3, connected to the mail server, the sender needs to provide the user name and password of the mailbox to verify Ts.connect ("smtp.sohu.com", "GaCl", "Mailbox Password");
4, create mail message messages = Createimagemail (session);
5, Send mail ts.sendmessage (message, message.getallrecipients ());
Ts.close (); /** * @Method: Createimagemail * @Description: Generate a message body with pictures of the message * @Anthor: Lonely Wolf * * @param session * @return * @t Hrows Exception */public static MimeMessage Createimagemail (Session session) throws Exception {//create mail mimemessage m
Essage = new MimeMessage (session);
Set the basic information for the message//Sender Message.setfrom (new internetaddress ("gacl@sohu.com"));
Recipient Message.setrecipient (Message.RecipientType.TO, New internetaddress ("xdp_gacl@sina.cn"));
Message title Message.setsubject ("Mail with Pictures");
Prepare mail data//Prepare message body data MimeBodyPart text = new MimeBodyPart ();
Text.setcontent ("This is a message body with pictures
3.5, sending messages that contain attachments
Package me.gacl.main;
Import Java.io.FileOutputStream;
Import java.util.Properties;
Import Javax.activation.DataHandler;
Import Javax.activation.FileDataSource;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart; /** * @ClassName: Sendmail * @Description: Send email * @author: Lonely Wolf * @date: 2015-1-12 Afternoon 9:42:56 * * * * * * public class Sendmai L {/** * @param args * @throws Exception/public static void main (string[] args) throws Exception {Propertie
s prop = new Properties ();
Prop.setproperty ("Mail.host", "smtp.sohu.com");
Prop.setproperty ("Mail.transport.protocol", "SMTP");
Prop.setproperty ("Mail.smtp.auth", "true");
5 Steps to send a message using JavaMail/1, create session Sessions session = Session.getinstance (prop);
Turn on debug mode for session so that you can view the running status of the program sending email session.setdebug (true); 2. Through Session Get Transport Object Transport ts = Session.gettransport ();
3, connected to the mail server Ts.connect ("smtp.sohu.com", "GaCl", "Mailbox Password");
4, create mail message messages = Createattachmail (session);
5, Send mail ts.sendmessage (message, message.getallrecipients ());
Ts.close (); /** * @Method: Createattachmail * @Description: Create a message with attachments * @Anthor: Lonely Wolf * @param session * @return * @thro WS Exception */public static MimeMessage Createattachmail (Sessions session) throws exception{mimemessage message = NE
W MimeMessage (session);
Set the basic information for the message//Sender Message.setfrom (new internetaddress ("gacl@sohu.com"));
Recipient Message.setrecipient (Message.RecipientType.TO, New internetaddress ("xdp_gacl@sina.cn"));
Message header message.setsubject ("JavaMail Mail Send Test");
Create the message body, in order to avoid the message body Chinese garbled problem, need to use Charset=utf-8 to specify the character encoding mimebodypart text = new MimeBodyPart ();
Text.setcontent ("Mail with attachments created using JavaMail", "text/html;charset=utf-8");
Create mail attachment MimeBodyPart attach = new MimeBodyPart (); DataHandler dh = new DataHandler (New FiledatasOurce ("src\\2.jpg"));
Attach.setdatahandler (DH); Attach.setfilename (Dh.getname ());
Create a container description data relationship Mimemultipart MP = new Mimemultipart ();
Mp.addbodypart (text);
Mp.addbodypart (attach);
Mp.setsubtype ("mixed");
Message.setcontent (MP);
Message.savechanges ();
Writes the created email to the E-disk storage Message.writeto ("E:\\attachmail.eml") (new FileOutputStream);
Returns the generated mail return message;
}
}
3.6, sending complex messages with inline pictures and attachments
Package me.gacl.main;
Import Java.io.FileOutputStream;
Import java.util.Properties;
Import Javax.activation.DataHandler;
Import Javax.activation.FileDataSource;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;
Import javax.mail.internet.MimeUtility; /** * @ClassName: Sendmail * @Description: Send email * @author: Lonely Wolf * @date: 2015-1-12 Afternoon 9:42:56 * * * * * * public class Sendmai L {/** * @param args * @throws Exception/public static void main (string[] args) throws Exception {Propertie
s prop = new Properties ();
Prop.setproperty ("Mail.host", "smtp.sohu.com");
Prop.setproperty ("Mail.transport.protocol", "SMTP");
Prop.setproperty ("Mail.smtp.auth", "true");
5 Steps to send a message using JavaMail/1, create session Sessions session = Session.getinstance (prop); Turn on debug mode for session so that you can view the running status of the program sending email
Session.setdebug (TRUE);
2, through session to get transport object Transport ts = Session.gettransport ();
3, connected to the mail server Ts.connect ("smtp.sohu.com", "GaCl", "Mailbox Password");
4, create mail message messages = Createmixedmail (session);
5, Send mail ts.sendmessage (message, message.getallrecipients ());
Ts.close (); /** * @Method: Createmixedmail * @Description: Generate a message with attachments and pictures * @Anthor: Lonely Wolf * @param session * @return * @t Hrows Exception */public static MimeMessage Createmixedmail (Session session) throws Exception {//create mail mimemessage m
Essage = new MimeMessage (session);
Set the basic information for the message Message.setfrom (new internetaddress ("gacl@sohu.com"));
Message.setrecipient (Message.RecipientType.TO, New internetaddress ("xdp_gacl@sina.cn"));
Message.setsubject ("Mail with Attachments and pictures");
Text MimeBodyPart text = new MimeBodyPart ();
Text.setcontent ("xxx This is the female xxxx<br/>
The above is the use of JavaMail API to create mail and send the entire content of the message, I hope to help you learn.