Esempio n. 1
0
 @Override
 public void send() throws MessagingException, UnsupportedEncodingException {
   long t0 = System.currentTimeMillis();
   try {
     if (iMail.getFrom() == null || iMail.getFrom().length == 0)
       setFrom(
           ApplicationProperty.EmailSenderAddress.value(),
           ApplicationProperty.EmailSenderName.value());
     if (iMail.getReplyTo() == null || iMail.getReplyTo().length == 0)
       setReplyTo(
           ApplicationProperty.EmailReplyToAddress.value(),
           ApplicationProperty.EmailReplyToName.value());
     iMail.setSentDate(new Date());
     iMail.setContent(iBody);
     iMail.saveChanges();
     Transport.send(iMail);
   } finally {
     long t = System.currentTimeMillis() - t0;
     if (t > 30000)
       sLog.warn(
           "It took "
               + new DecimalFormat("0.00").format(t / 1000.0)
               + " seconds to send an email.");
     else if (t > 5000)
       sLog.info(
           "It took "
               + new DecimalFormat("0.00").format(t / 1000.0)
               + " seconds to send an email.");
   }
 }
Esempio n. 2
0
  // -- MailService overrides
  @Override
  public void send(MailController controller) throws MessagingException {
    if (!controller.hasRecipients()) {
      LOG.warning("Not recipients. No email to send");
      return;
    }

    /* log */

    /* Open the Session */
    Properties properties = new Properties();
    Session session = Session.getInstance(properties, null);
    OptionService os = findService(OptionService.class);
    MimeMessage mm = createMimeMessage(controller, session, os);

    LOG.info("");
    LOG.info(" From:    " + toString(mm.getFrom()));
    LOG.info(" ReplyTo: " + toString(mm.getReplyTo()));
    LOG.info(" Sender:  " + mm.getSender());
    LOG.info(" To:      " + toString(mm.getRecipients(Message.RecipientType.TO)));
    LOG.info(" CC:      " + toString(mm.getRecipients(Message.RecipientType.CC)));
    LOG.info(" Bcc:     " + toString(mm.getRecipients(Message.RecipientType.BCC)));
    LOG.info(" Subject: " + mm.getSubject());
    LOG.info("");

    Transport.send(mm);
  }
Esempio n. 3
0
 @Override
 public void addReplyTo(String email, String name)
     throws UnsupportedEncodingException, MessagingException {
   if (email == null || email.isEmpty()) return;
   Address[] replyTo = iMail.getReplyTo();
   if (replyTo == null || replyTo.length == 0) {
     iMail.setReplyTo(new InternetAddress[] {new InternetAddress(email, name, "UTF-8")});
   } else {
     Address[] newReplyTo = new Address[replyTo.length + 1];
     for (int i = 0; i < replyTo.length; i++) newReplyTo[i] = replyTo[i];
     newReplyTo[replyTo.length] = new InternetAddress(email, name, "UTF-8");
     iMail.setReplyTo(newReplyTo);
   }
 }
  public void testDelayedEmailNotificationOnDeadline() throws Exception {
    Map vars = new HashMap();
    vars.put("users", users);
    vars.put("groups", groups);
    vars.put("now", new Date());

    DefaultEscalatedDeadlineHandler notificationHandler =
        new DefaultEscalatedDeadlineHandler(getConf());
    WorkItemManager manager = new DefaultWorkItemManager(null);
    notificationHandler.setManager(manager);

    MockUserInfo userInfo = new MockUserInfo();
    userInfo.getEmails().put(users.get("tony"), emailAddressTony);
    userInfo.getEmails().put(users.get("darth"), emailAddressDarth);

    userInfo.getLanguages().put(users.get("tony"), "en-UK");
    userInfo.getLanguages().put(users.get("darth"), "en-UK");
    notificationHandler.setUserInfo(userInfo);

    taskService.setEscalatedDeadlineHandler(notificationHandler);

    Reader reader =
        new InputStreamReader(
            getClass().getResourceAsStream(MvelFilePath.DeadlineWithNotification));
    Task task = (Task) eval(reader, vars);

    BlockingAddTaskResponseHandler addTaskResponseHandler = new BlockingAddTaskResponseHandler();
    client.addTask(task, null, addTaskResponseHandler);
    long taskId = addTaskResponseHandler.getTaskId();

    Content content = new Content();
    content.setContent("['subject' : 'My Subject', 'body' : 'My Body']".getBytes());
    BlockingSetContentResponseHandler setContentResponseHandler =
        new BlockingSetContentResponseHandler();
    client.setDocumentContent(taskId, content, setContentResponseHandler);
    long contentId = setContentResponseHandler.getContentId();
    BlockingGetContentResponseHandler getResponseHandler = new BlockingGetContentResponseHandler();
    client.getContent(contentId, getResponseHandler);
    content = getResponseHandler.getContent();
    assertEquals(
        "['subject' : 'My Subject', 'body' : 'My Body']", new String(content.getContent()));

    // emails should not be set yet
    assertEquals(0, getWiser().getMessages().size());
    Thread.sleep(100);

    // nor yet
    assertEquals(0, getWiser().getMessages().size());

    long time = 0;
    while (getWiser().getMessages().size() != 2 && time < 15000) {
      Thread.sleep(500);
      time += 500;
    }

    // 1 email with two recipients should now exist
    assertEquals(2, getWiser().getMessages().size());

    List<String> list = new ArrayList<String>(2);
    list.add(getWiser().getMessages().get(0).getEnvelopeReceiver());
    list.add(getWiser().getMessages().get(1).getEnvelopeReceiver());

    assertTrue(list.contains(emailAddressTony));
    assertTrue(list.contains(emailAddressDarth));

    MimeMessage msg = ((WiserMessage) getWiser().getMessages().get(0)).getMimeMessage();
    assertEquals("My Body", msg.getContent());
    assertEquals("My Subject", msg.getSubject());
    assertEquals("*****@*****.**", ((InternetAddress) msg.getFrom()[0]).getAddress());
    assertEquals("*****@*****.**", ((InternetAddress) msg.getReplyTo()[0]).getAddress());
    boolean tonyMatched = false;
    boolean darthMatched = false;
    for (int i = 0; i < msg.getRecipients(RecipientType.TO).length; ++i) {
      String emailAddress = ((InternetAddress) msg.getRecipients(RecipientType.TO)[i]).getAddress();
      if ("*****@*****.**".equals(emailAddress)) {
        tonyMatched = true;
      } else if ("*****@*****.**".equals(emailAddress)) {
        darthMatched = true;
      }
    }
    assertTrue("Could not find tony in recipients list.", tonyMatched);
    assertTrue("Could not find darth in recipients list.", darthMatched);
  }