Skip to content

Instantly share code, notes, and snippets.

@biscuitrainbow
Created April 9, 2017 13:00
Show Gist options
  • Save biscuitrainbow/b391e91f5674d75734e2ff4a82320098 to your computer and use it in GitHub Desktop.
Save biscuitrainbow/b391e91f5674d75734e2ff4a82320098 to your computer and use it in GitHub Desktop.
6. ระบบสามารถส่งไฟล์ข้อมูลไปยัง E-Mail Address ได้
private MimeMessage createMessageWithAttachment(Email email) throws MessagingException {
Properties properties = new Properties();
Session session = Session.getDefaultInstance(properties, null);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(email.getSender()));
mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email.getReceiver()));
mimeMessage.setSubject(email.getSubject());
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(email.getBody(), "text/plain");
MimeMultipart multipart = new MimeMultipart();
DataSource source = new FileDataSource(email.getAttachment());
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(email.getAttachment().getName());
multipart.addBodyPart(bodyPart);
mimeMessage.setContent(multipart);
return mimeMessage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment