Here is the code to send an attachment:
import javautilProperties;
import javaxmail*;
import javaxmailinternet*;
import javaxactivation*; public class AttachExample {
public static void main (String args[])
throws Exception {
String host = args[];
String from = args[];
String to = args[];
String fileAttachment = args[];
// Get system properties
Properties props = SystemgetProperties();
// Setup mail server
propsput(mailsmtphost host);
// Get session
Session session =
SessiongetInstance(props null);
// Define message
MimeMessage message =
new MimeMessage(session);
messagesetFrom(
new InternetAddress(from));
messageaddRecipient(
MessageRecipientTypeTO
new InternetAddress(to));
messagesetSubject(
Hello JavaMail Attachment);
// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();
//fill message
messageBodyPartsetText(Hi);
Multipart multipart = new MimeMultipart();
multipartaddBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPartsetDataHandler(
new DataHandler(source));
messageBodyPartsetFileName(fileAttachment);
multipartaddBodyPart(messageBodyPart);
// Put parts in message
messagesetContent(multipart);
// Send the message
Transportsend( message );
}
}