Use JavaMail to send attachments (turn)

Source: Internet
Author: User
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));
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.