Javamail (Java Mail Service) API (3)

Source: Internet
Author: User

5. Reply to the email
Reply email
The method is simple: Use the reply () method of the message class to configure the recipient address and topic of the reply mail (if the subject is not provided, the system uses "re:" As the mail by default.
You do not need to set any email content here, as long as you copy the sender or reply-to the new recipient. The Boolean parameter in the reply () method indicates whether to send emails
Reply to the sender (the parameter value is false) or to the owner (the parameter value is true ).
In addition, the reply-to address must be set using the setreplyto () method during mail sending.
Mimemessage reply = (mimemessage) message. reply (false); reply. setfrom (New internetaddress ("president@whitehouse.gov"); reply. settext ("thanks"); transport. send (reply );

6. Forward mail
The Mail forwarding process is not as simple as the previous reply mail. It will create a forward mail, which is not a method.
Each
Mails are composed of multiple parts. Each part is called a body part and a bodypart Class Object. For mime-type mails, It is a mimebodypart class object. This
Some email bodies are contained in the container for multipart. For mime-type emails, they are mimemultipart class objects. When forwarding emails, we create a text email body
And a forwarded text body, and then put the two bodies in a multipart. Note: The method to copy the content of one email to another is to copy it only.
Datahandler. This is a class defined by JavaBeans activation framework, which provides operations on mail content
The command accesses and manages mail content operations. It is a consistent interface between different data sources and data formats.
// Create the message to forwardmessage forward = new mimemessage (session); // fill in headerforward. setsubject ("FWD:" + message. getsubject (); forward. setfrom (New internetaddress (from); forward. addrecipient (message. recipienttype. to, new internetaddress (to); // create your new message partbodypart messagebodypart = new mimebodypart (); messagebodypart. settext ("Here you go with the original message:/n"); // create a multi-part to combine the partsmultipart multipart = new mimemultipart (); multipart. addbodypart (messagebodypart); // create and fill part for the forwarded contentmessagebodypart = new mimebodypart (); messagebodypart. setdatahandler (message. getdatahandler (); // Add part to multi partmultipart. addbodypart (messagebodypart); // associate multi-part with messageforward. setcontent (multipart); // send messagetransport. send (forward );

7. attachments
Attachments are often used as email-related resources in text, tables, images, and other formats, such as popular mail clients, we can use javamail API to retrieve attachments from emails or send emails with attachments.

A. send an email with an attachment
The process of sending emails with attachments is similar to forwarding emails. We need to create a complete mail body section, after the first section (that is, the text of our mail content, add an attachment with datahandler, instead of copying the first part of datahandler when forwarding the mail.

If we send a file as an attachment, we need to create a filedatasource type object as the attachment data source. If the data is read from the URL and sent as an attachment, we will create a urldatasource type object as the attachment data source.

Then, the data source (filedatasource or urldatasource) object is passed in as a parameter of the datahandler class constructor method to create a datahandler object as the data source's datahandler.

Set the datahandler to the datahandler of the mail body. In this way, the association between the email body and the attachment is completed. The following work is the setfilename () method of the bodypart to set the attachment name to the original file name.

Finally, put the two email bodies in the multipart, set the mail content to the container multipart, and send the mail.
// Define messagemessage message = new mimemessage (session); message. setfrom (New internetaddress (from); message. addrecipient (message. recipienttype. to, new internetaddress (to); message. setsubject ("Hello javamail attachment"); // create the message part bodypart messagebodypart = new mimebodypart (); // fill the messagemessagebodypart. settext ("pardon ideas"); multipart = new mimemultipart (); multipart. addbodypart (messagebodypart); // Part Two is attachmentmessagebodypart = new mimebodypart (); datasource source = new filedatasource (filename); messagebodypart. setdatahandler (New datahandler (source); messagebodypart. setfilename (filename); multipart. addbodypart (messagebodypart); // put parts in messagemessage. setcontent (multipart); // send the messagetransport. send (Message );
If you use servlet to send emails with attachments, you must upload the attachments to the servlet. In this case, note that the encoding type set in the form on the submission page should be multipart/form-data.

Hello

"+" "; Message. setcontent (htmltext," text/html "));
Note: The image here is not embedded in the email, but defined in the URL. The email recipient can only see it online.
When receiving emails, if we use javamail API to receive emails, the content of the emails cannot be displayed in HTML. Because the content of the javamail API mail is considered as a binary stream. Therefore, to display HTML content emails, we must use jeditorpane or a third-party HTML display component.

The following code shows how to use jeditorpane to display mail content:
If (message. getcontenttype (). equals ("text/html") {string content = (string) message. getcontent (); jframe frame = new jframe (); jeditorpane text = new jeditorpane ("text/html", content); text. seteditable (false); jscrollpane pane = new jscrollpane (text); frame. getcontentpane (). add (PANE); frame. setsize (300,300); frame. setdefaclocloseoperation (jframe. dispose_on_close); frame. show ();}

B. include images in the email
For example
If we use HTML as the content in an email, we 'd better use the image in HTML as part of the email so that the image in HTML will be correctly displayed no matter whether it is online or not. The solution is
The image used in HTML is used as an email attachment and a special cid url is used as an image reference. This CID is a reference to the content-ID header of the Image Attachment.
Processing embedded images is like adding attachments to an email. The difference is that we must set the content-ID in the header of the email body of the Image Attachment as a random string, set IMG to this string in the SRC mark of IMG in HTML. In this way, the association between image attachments and HTML is completed.
String file = ...; // create the messagemessage message = new mimemessage (session); // fill its headersmessage. setsubject ("embedded image"); message. setfrom (New internetaddress (from); message. addrecipient (message. recipienttype. to, new internetaddress (to); // create your new message partbodypart messagebodypart = new mimebodypart (); string htmltext ="
Hello

"+" "; Messagebodypart. setcontent (htmltext, "text/html"); // create a related multi-part to combine the partsmimemultipart multipart = new mimemultipart ("related"); multipart. addbodypart (messagebodypart); // create part for the imagemessagebodypart = new mimebodypart (); // fetch the image and associate to partdatasource FDS = new filedatasource (File); messagebodypart. setdatahandler (New datahandler (FDS); messagebodypart. setheader ("content-ID", ""); // Add part to multi-partmultipart.addBodyPart (messagebodypart); // associate multi-part with messagemessage. setcontent (multipart );


Related Article

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.