private MimeMessage getMessage(Email email, Session session)
      throws AddressException, MessagingException {
    MimeMessage result = new MimeMessage(session);

    result.setFrom(new InternetAddress(email.getFrom()));
    result.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email.getTo()));
    result.setSubject(email.getSubject());

    if (email.getAttachment() != null) {
      // message body part
      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText(email.getMsg());

      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);

      MimeBodyPart attachmentPart = new MimeBodyPart();
      DataSource attachmentSource =
          new ByteArrayDataSource(
              email.getAttachment().getContent(), email.getAttachment().getAttachmentType());
      attachmentPart.setDataHandler(new DataHandler(attachmentSource));
      attachmentPart.setFileName(email.getAttachment().getFileName());

      multipart.addBodyPart(attachmentPart);

      result.setContent(multipart);
    } else {
      result.setContent(email.getMsg(), "text/plain");
    }

    return result;
  }
Example #2
0
 public void printContents() {
   int i = 0;
   for (Email e : emails) {
     System.out.println(
         i
             + "--> \nTo: "
             + e.getTo()
             + "\nSubject: "
             + e.getSubject()
             + "\nDate: "
             + e.getTimestamp().getTime());
     i++;
   }
 }
  @Override
  public void send(Email email) {
    Message message = createMessage();
    try {
      message.setSubject(email.getSubject());
      message.setContent(email.getContent(), "text/html;charset=utf-8");

      message.setRecipients(Message.RecipientType.TO, toAddress(email.getTo()));
      message.setRecipients(Message.RecipientType.CC, toAddress(email.getCc()));

      Transport.send(message);
    } catch (MessagingException e) {
      logger.error("InstallUtils executeSQL erro", e);
    }
  }