Attachment - Java Mail

Posted by Jagadeesh VP | Posted in


Session session = createMailSession(); // Create session with your
authentication data
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@vpj.com"));
message.setRecipients(new InternetAddress("to@vpj.com"));
message.setSubject("[SUBJECT]");

BodyPart textPart = new MimeBodyPart();
textPart.setText(emailBody);

Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(textPart);

DataSource source = new FileDataSource("[FILE_PATH]");

BodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName("[FILE_NAME]");

multiPart.addBodyPart(attachmentPart);

message.setContent(multiPart);

Transport.send(message);

Comments (0)

Post a Comment