73
Sending email with attachment in Java
For sending email with attachment, JavaMail API provides some useful classes like BodyPart, MimeBodyPart etc.
For better understanding of this example, learn the steps of sending email using JavaMail API first. |
For sending the email using JavaMail API, you need to load the two jar files:
download these jar files (or) go to the Oracle site to download the latest version. |
Sending email with attachment using JavaMail API
There are total 7 steps for sending attachment with email. They are:
- Get the session object
- compose message
- create MimeBodyPart object and set your message text
- create new MimeBodyPart object and set DataHandler object to this object
- create Multipart object and add MimeBodyPart objects to this object
- set the multiplart object to the message object
- send message
Example of sending email with attachment in Java
As you can see in the above example, total 7 steps are followed to send email with attachment. Now run this program by :
Load the jar file | c:> set classpath=mail.jar;activation.jar;.; |
compile the source file | c:> javac SendAttachment.java |
run by | c:> java SendAttachment |