Attachments are resources associated with a mail message, usually kept outside of the "like a text file, Spreadshee T, or image. As with common mail programs like Eudora and pine, can attach resources to your mail and the JavaMail API and Get those attachments to receive the message.
Sending attachments:
Sending attachments is quite like forwarding messages. You build the parts to make the complete message. After the "I", "your message text," Add other parts where the datahandler to your attachment, instead of The shared handler in the case of a forwarded message. If you are are reading the attachment from a file, your attachment data source is a filedatasource. Reading from a URL and it is a urldatasource. Once you have your DataSource, just pass it in to the DataHandler constructor, before finally attaching it to the BodyPart With Setdatahandler (). Assuming you want to retain the original filename of the attachment, the last thing to do are to set the filename associat Ed with the attachment and the Setfilename () method of BodyPart. All of this are shown here:
Define message
Message 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 message
Messagebodypart.settext ("Pardon Ideas");
Multipart Multipart = new Mimemultipart ();
Multipart.addbodypart (Messagebodypart);
Part Two is attachment
Messagebodypart = new MimeBodyPart ();
DataSource Source = new Filedatasource (filename);
Messagebodypart.setdatahandler (new DataHandler (source));
Messagebodypart.setfilename (filename);
Multipart.addbodypart (Messagebodypart);
Put parts in
Message.setcontent (multipart);
Send the message
Transport.send (message);
When including attachments with your messages, if your program is a servlet, your users must upload the attachment besides Telling where to send the message. Uploading each file can is handled with a form encoding type of multipart/form-data.
The Note:message size is limited by your SMTP server and not the JavaMail API. If you run into problems, consider increasing the Java heap size by setting the MS and MX parameters.
Exercise:
Exercise 5. How to send Attachments
Getting attachments:
Getting attachments out of your messages are a little more involved then sending them because MIME has no simple notion of Attachments. The content of your message was a Multipart object when it has attachments. You are then need to process the main content and the attachment (s). Parts marked with a disposition of part.attachment from part.getdisposition () are clearly. However, attachments can also come across with no disposition (and a non-text MIME type) or a disposition of part.inline. When the disposition is either part.attachment or Part.inline, you can save the content Just get the original filename with GetFileName () and the input stream with getInputStream ().
Multipart MP = (Multipart) message.getcontent ();
for (int i=0, N=multipart.getcount (); i<n; i++) {
Part part = Multipart.getbodypart (i));
The SaveFile () method is just creates a File from the filename, reads the bytes from the input stream, and writes them out to The file. In the case the file already exists, a number are added to the "end of" the filename until one is found that doesn ' t exist.
From SaveFile ()
File File = new file (filename);
for (int i=0; file.exists (); i++) {
File = new file (filename+i);
}
The code above covers the simplest case where message parts are flagged. To cover all cases, handle the ' disposition is null and get the MIME type of the ' part to handle accordingly.
if (disposition = = null) {
Check if plain
MimeBodyPart MBP = (mimebodypart) part;
if (Mbp.ismimetype ("Text/plain")) {
Handle Plain
} else {
Special non-attachment cases of image/gif, text/html, ...
}
...
}
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.