/**
   * This method is used to send emails to the agency
   *
   * @param invoices
   */
  @Override
  public void sendInvoicesViaEmail(List<ContractsGrantsInvoiceDocument> invoices)
      throws AddressException, MessagingException {
    LOG.debug("sendInvoicesViaEmail() starting.");

    Properties props = getConfigProperties();

    // Get session
    Session session = Session.getInstance(props, null);
    for (ContractsGrantsInvoiceDocument invoice : invoices) {
      List<InvoiceAddressDetail> invoiceAddressDetails = invoice.getInvoiceAddressDetails();
      for (InvoiceAddressDetail invoiceAddressDetail : invoiceAddressDetails) {
        if (ArConstants.InvoiceTransmissionMethod.EMAIL.equals(
            invoiceAddressDetail.getInvoiceTransmissionMethodCode())) {

          // KFSTI-48 Refactor to retrieve the note through noteService
          Note note = noteService.getNoteByNoteId(invoiceAddressDetail.getNoteId());

          if (ObjectUtils.isNotNull(note)) {

            MimeMessage message = new MimeMessage(session);

            // From Address
            String sender =
                parameterService.getParameterValueAsString(
                    ContractsGrantsInvoiceEmailReportsBatchStep.class,
                    ArConstants.CG_INVOICE_FROM_EMAIL_ADDRESS);
            message.setFrom(new InternetAddress(sender));
            // To Address
            CustomerAddress customerAddress = invoiceAddressDetail.getCustomerAddress();
            String recipients = customerAddress.getCustomerEmailAddress();
            if (StringUtils.isNotEmpty(recipients)) {
              InternetAddress[] recipientAddress = {new InternetAddress(recipients)};
              message.addRecipients(Message.RecipientType.TO, recipientAddress);
            } else {
              LOG.warn("No recipients indicated.");
            }

            // The Subject
            String subject =
                parameterService.getParameterValueAsString(
                    ContractsGrantsInvoiceEmailReportsBatchStep.class,
                    ArConstants.CG_INVOICE_EMAIL_SUBJECT);
            String bodyText =
                parameterService.getParameterValueAsString(
                    ContractsGrantsInvoiceEmailReportsBatchStep.class,
                    ArConstants.CG_INVOICE_EMAIL_BODY);
            Map<String, String> map = new HashMap<String, String>();
            getEmailParameterList(map, invoice, customerAddress);
            subject = replaceValuesInString(subject, map);
            bodyText = replaceValuesInString(bodyText, map);
            message.setSubject(subject);
            if (StringUtils.isEmpty(subject)) {
              LOG.warn("Empty subject being sent.");
            }

            // Now the message body.
            // create and fill the first message part
            MimeBodyPart body = new MimeBodyPart();
            body.setText(bodyText);

            // create and fill the second message part
            MimeBodyPart attachment = new MimeBodyPart();
            // Use setText(text, charset), to show it off !
            // create the Multipart and its parts to it
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(body);
            try {
              ByteArrayDataSource ds =
                  new ByteArrayDataSource(
                      note.getAttachment().getAttachmentContents(), "application/pdf");
              attachment.setDataHandler(new DataHandler(ds));
              attachment.setFileName(note.getAttachment().getAttachmentFileName());
              multipart.addBodyPart(attachment);
            } catch (IOException ex) {
              LOG.error("problem during AREmailServiceImpl.sendInvoicesViaEmail()", ex);
            }

            // add the Multipart to the message
            message.setContent(multipart);

            // Finally, send the message!
            Transport.send(message);
          }
        }
      }
      invoice.setMarkedForProcessing(ArConstants.INV_RPT_PRCS_SENT);
      documentService.updateDocument(invoice);
    }
  }