It is not difficult to see the example of javamail sending attachments

Source: Internet
Author: User
Here is the code to send an attachment:
Import java. util. Properties;
Import javax. mail .*;
Import javax. mail. internet .*;
Import javax. activation .*;
Public class AttachExample {
Public static void main (String args [])
Throws Exception {
String host = args [0];
String from = args [1];
String to = args [2];
String fileAttachment = args [3];
// Get system properties
Properties props = System. getProperties ();
// Setup mail server
Props. put ("mail. smtp. host", host );
// Get session
Session session =
Session. getInstance (props, null );
// Define message
MimeMessage message =
New MimeMessage (session );
Message. setFrom (
New InternetAddress (from ));
Message. addRecipient (
Message. RecipientType.,
New InternetAddress ());
Message. setSubject (
"Hello JavaMail Attachment ");
// Create the message part
MimeBodyPart messageBodyPart =
New MimeBodyPart ();
// Fill message
MessageBodyPart. setText ("Hi ");
Multipart multipart = new MimeMultipart ();
Multipart. addBodyPart (messageBodyPart );
// Part two is attachment

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: 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.