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 FiletTachment = 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.to,
NEW InternetAddress (to));
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
Messagebodypart = new mimebodypart ();
DataSource Source =
New FileDataSource (FileAtTachment);
MessageBodypart.SetDataHandler
New DataHandler (SOURCE));
MessageBodypart.setFileName (FileAtTachment);
Multipart.addbodypart (MessageBodypart);
// Put Parts in Message
Message.setContent (Multipart);
// send the message
TRANSPORT.SEND (Message);
}
}