// Define message Message 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 BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("Here's the file"); // Create a Multipart Multipart multipart = new MimeMultipart(); // Add part one multipart.addBodyPart(messageBodyPart); // // Part two is attachment // // Create second body part messageBodyPart = new MimeBodyPart(); // Get the attachment DataSource source = new FileDataSource(filename); // Set the data handler to the attachment messageBodyPart.setDataHandler(new DataHandler(source)); // Set the filename messageBodyPart.setFileName(filename); // Add part two multipart.addBodyPart(messageBodyPart); // Put parts in message message.setContent(multipart); // Send the message Transport.send(message); } }
Post a Comment