1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws MessagingException, IOException {
MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(sendMail, "Java Program", "UTF-8")); message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "Program", "UTF-8")); message.setSubject(MY_EMAIL_SUBJECT, "UTF-8");
MimeBodyPart image = new MimeBodyPart(); DataHandler dataHandler = new DataHandler(new FileDataSource("resource/Cute_pig.jpg")); image.setDataHandler(dataHandler); image.setContentID("image_Cut_pig_id");
MimeBodyPart text = new MimeBodyPart(); text.setContent(MY_EMAIL_CONTENT, "text/html;charset=UTF-8");
MimeMultipart mm_text_image = new MimeMultipart(); mm_text_image.addBodyPart(text); mm_text_image.addBodyPart(image); mm_text_image.setSubType("related");
MimeBodyPart text_image = new MimeBodyPart(); text_image.setContent(mm_text_image);
MimeBodyPart attachment = new MimeBodyPart(); DataHandler dataHandler2 = new DataHandler(new FileDataSource("resource/Love_Yourself.mp3")); attachment.setDataHandler(dataHandler2); attachment.setFileName(MimeUtility.encodeText(dataHandler2.getName()));
MimeMultipart mm_text_image_att = new MimeMultipart(); mm_text_image_att.addBodyPart(text_image); mm_text_image_att.addBodyPart(attachment); mm_text_image_att.setSubType("mixed");
message.setContent(mm_text_image_att);
message.setSentDate(new Date()); message.saveChanges();
return message;
}
|