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;
  }
Esempio n. 2
0
  public void sendMail(Email email) throws MessagingException {
    MimeMessage message = mailSender.createMimeMessage();

    MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
    messageHelper.setSubject(email.getSubject());
    messageHelper.setTo(email.getReceiver());
    messageHelper.setFrom(sender);
    messageHelper.setText(email.getContent(), true);
    mailSender.send(message);
  }
Esempio n. 3
0
 private Message composeMessage(Email email, String contentType) throws EmailCompositionException {
   Message message = new MimeMessage(session);
   try {
     message.setFrom(createInternetAddress(email.getFrom()));
     setRecipients(email, message);
     message.setSubject(email.getSubject());
     message.setContent(email.getMessageText(), contentType);
   } catch (Exception e) {
     throw new EmailCompositionException(e);
   }
   return message;
 }
Esempio n. 4
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++;
   }
 }
Esempio n. 5
0
 @Override
 @Transactional
 public void sendAdminChangePasswordNotification(Map<String, Object> obj) {
   try {
     this.simpleMailMessage.setTo(obj.get("emailId").toString());
     Email mail = this.getMailTemplate(CHANGE_PASSWORD_BY_ADMIN);
     this.simpleMailMessage.setSubject(mail.getSubject());
     String text = "Text mode not supported, please turn on Standard mode.";
     String text1 = String.format(mail.getValue(), obj.get("empname"), obj.get("newpassword"));
     this.sendMail(text, text1, null);
   } catch (Exception ex) {
     ex.printStackTrace();
     logger.error("Exception in Method:sendAdminChangePasswordNotification", ex);
   }
 }
Esempio n. 6
0
  @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);
    }
  }
Esempio n. 7
0
 @Override
 public void sendTsApproveNotificationToAll(String[] obj, Calendar cal) {
   try {
     if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
       this.simpleMailMessage.setTo(obj);
       Email mail = null;
       mail = this.getMailTemplate(TS_APPROVE_ALERT_MGR);
       this.simpleMailMessage.setSubject(mail.getSubject());
       String text = "Text mode not supported, please turn on Standard mode.";
       String text1 = String.format(mail.getValue());
       this.sendMail(text, text1, null);
     }
   } catch (Exception ex) {
     logger.error("Exception in Method:sendTsApproveNotificationToAll", ex);
   }
 }
Esempio n. 8
0
 @Override
 @Transactional
 public void sendTSSubmitNotificationToMgr(Map<String, Object> obj, int status) {
   try {
     this.simpleMailMessage.setTo(obj.get("toemailid").toString());
     this.simpleMailMessage.setCc(obj.get("ccemailid").toString());
     Email mail = new Email();
     String text1 = new String();
     if (status == 4) {
       mail = this.getMailTemplate(TIMESHEET_SUBMIT_NOTIFICATION);
       text1 =
           String.format(
               mail.getValue(),
               obj.get("toname"),
               obj.get("ccname"),
               obj.get("dateweek"),
               obj.get("status"));
     }
     if (status == 6) {
       mail = this.getMailTemplate(TIMESHEET_ARROVAL_NOTIFICATION);
       text1 =
           String.format(
               mail.getValue(),
               obj.get("ccname"),
               obj.get("toname"),
               obj.get("dateweek"),
               obj.get("status"));
     }
     if (status == 5) {
       mail = this.getMailTemplate(TIMESHEET_REJECTED_NOTIFICATION);
       text1 =
           String.format(
               mail.getValue(),
               obj.get("ccname"),
               obj.get("toname"),
               obj.get("dateweek"),
               obj.get("status"));
     }
     this.simpleMailMessage.setSubject(mail.getSubject());
     String text = "Text mode not supported, please turn on Standard mode.";
     this.sendMail(text, text1, null);
   } catch (Exception ex) {
     ex.printStackTrace();
     logger.error("Exception in Method:sendTSSubmitNotificationToMgr", ex);
   }
 }
Esempio n. 9
0
 @Override
 @Transactional
 public void sendCreateUserNotification(Map<String, Object> user) {
   try {
     this.simpleMailMessage.setTo(user.get("emailId").toString());
     Email mail = this.getMailTemplate(CREATE_USER_TPL);
     this.simpleMailMessage.setSubject(mail.getSubject());
     String text = "Text mode not supported, please turn on Standard mode.";
     String text1 =
         String.format(
             mail.getValue(),
             user.get("fname") + " , " + user.get("lname"),
             user.get("username"),
             user.get("password_copy"));
     this.sendMail(text, text1, null);
   } catch (Exception ex) {
     logger.error("Exception in Method:sendCreateUserNotification", ex);
   }
 }
Esempio n. 10
0
 @Override
 public void sendTSSubmissionNotificationToAll(String[] obj, Calendar cal) {
   try {
     if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY
         || cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
       this.simpleMailMessage.setTo(obj);
       Email mail = null;
       if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)
         mail = this.getMailTemplate(TS_SUB_NOTIC_TO_DEFALUTER);
       else mail = this.getMailTemplate(TS_SUB_NOTIC_TO_ALL);
       this.simpleMailMessage.setSubject(mail.getSubject());
       String text = "Text mode not supported, please turn on Standard mode.";
       String text1 = String.format(mail.getValue());
       this.sendMail(text, text1, null);
     }
   } catch (Exception ex) {
     logger.error("Exception in Method:sendTSSubmissionNotificationToAll", ex);
   }
 }
Esempio n. 11
0
  @Override
  public void sendNewBugNotification(Map<String, Object> issue) {

    try {
      String[] str = new String[2];
      str[0] = "*****@*****.**";
      str[1] = "*****@*****.**";
      this.simpleMailMessage.setTo(str);
      this.simpleMailMessage.setCc(issue.get("emailId").toString());
      Email mail = this.getMailTemplate(ADD_BUG_NOTIFICATION);
      this.simpleMailMessage.setSubject(mail.getSubject());
      String text = "Text mode not supported, please turn on Standard mode.";
      String text1 =
          String.format(
              mail.getValue(), issue.get("fname"), issue.get("title"), issue.get("createdOn"));
      this.sendMail(text, text1, null);
    } catch (Exception ex) {
      logger.error("Exception in Method:sendCreateUserNotification", ex);
    }
  }
Esempio n. 12
0
  public HttpEntity buildBody(Email email) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    // We are using an API key
    if (this.username != null) {
      builder.addTextBody("api_user", this.username);
      builder.addTextBody("api_key", this.password);
    }

    String[] tos = email.getTos();
    String[] tonames = email.getToNames();
    String[] ccs = email.getCcs();
    String[] bccs = email.getBccs();

    // If SMTPAPI Header is used, To is still required. #workaround.
    if (tos.length == 0) {
      builder.addTextBody(
          String.format(PARAM_TO, 0), email.getFrom(), ContentType.create(TEXT_PLAIN, UTF_8));
    }
    for (int i = 0, len = tos.length; i < len; i++)
      builder.addTextBody(PARAM_TO, tos[i], ContentType.create("text/plain", "UTF-8"));
    for (int i = 0, len = tonames.length; i < len; i++)
      builder.addTextBody(PARAM_TONAME, tonames[i], ContentType.create("text/plain", "UTF-8"));
    for (int i = 0, len = ccs.length; i < len; i++)
      builder.addTextBody(PARAM_CC, ccs[i], ContentType.create("text/plain", "UTF-8"));
    for (int i = 0, len = bccs.length; i < len; i++)
      builder.addTextBody(PARAM_BCC, bccs[i], ContentType.create(TEXT_PLAIN, UTF_8));
    // Files
    if (email.getAttachments().size() > 0) {
      Iterator it = email.getAttachments().entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        builder.addBinaryBody(
            String.format(PARAM_FILES, entry.getKey()), (InputStream) entry.getValue());
      }
    }

    if (email.getContentIds().size() > 0) {
      Iterator it = email.getContentIds().entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        builder.addTextBody(
            String.format(PARAM_CONTENTS, entry.getKey()), (String) entry.getValue());
      }
    }

    if (email.getHeaders().size() > 0)
      builder.addTextBody(
          PARAM_HEADERS,
          new JSONObject(email.getHeaders()).toString(),
          ContentType.create(TEXT_PLAIN, UTF_8));

    if (email.getFrom() != null && !email.getFrom().isEmpty())
      builder.addTextBody(PARAM_FROM, email.getFrom(), ContentType.create(TEXT_PLAIN, UTF_8));

    if (email.getFromName() != null && !email.getFromName().isEmpty())
      builder.addTextBody(
          PARAM_FROMNAME, email.getFromName(), ContentType.create(TEXT_PLAIN, UTF_8));

    if (email.getReplyTo() != null && !email.getReplyTo().isEmpty())
      builder.addTextBody(PARAM_REPLYTO, email.getReplyTo(), ContentType.create(TEXT_PLAIN, UTF_8));

    if (email.getSubject() != null && !email.getSubject().isEmpty())
      builder.addTextBody(PARAM_SUBJECT, email.getSubject(), ContentType.create(TEXT_PLAIN, UTF_8));

    if (email.getHtml() != null && !email.getHtml().isEmpty())
      builder.addTextBody(PARAM_HTML, email.getHtml(), ContentType.create(TEXT_PLAIN, UTF_8));

    if (email.getText() != null && !email.getText().isEmpty())
      builder.addTextBody(PARAM_TEXT, email.getText(), ContentType.create(TEXT_PLAIN, UTF_8));

    String tmpString = email.smtpapi.jsonString();
    if (!tmpString.equals("{}"))
      builder.addTextBody(PARAM_XSMTPAPI, tmpString, ContentType.create(TEXT_PLAIN, UTF_8));

    return builder.build();
  }