public AttachmentHandler getAttachment(long attachmentId)
      throws IOException, PortalException, SystemException {

    Attachment attachment = AttachmentLocalServiceUtil.getAttachment(attachmentId);

    Message message = MessageLocalServiceUtil.getMessage(attachment.getMessageId());

    if (account.getDraftFolderId() == attachment.getFolderId()) {
      return new DefaultAttachmentHandler(
          AttachmentLocalServiceUtil.getInputStream(attachmentId), null);
    } else {
      return _imapAccessor.getAttachment(
          attachment.getFolderId(), message.getRemoteMessageId(), attachment.getContentPath());
    }
  }
  protected long getSize(long messageId, String body) throws SystemException {
    if (Validator.isNull(body)) {
      return 0;
    }

    long size = body.getBytes().length;

    List<Attachment> attachments = attachmentPersistence.findByMessageId(messageId);

    for (Attachment attachment : attachments) {
      size += attachment.getSize();
    }

    return size;
  }
  public void sendMessage(long accountId, long messageId) throws PortalException, SystemException {

    Account account = AccountLocalServiceUtil.getAccount(accountId);

    Message message = MessageLocalServiceUtil.getMessage(messageId);

    Address[] toAddresses = parseAddresses(message.getTo());
    Address[] ccAddresses = parseAddresses(message.getCc());
    Address[] bccAddresses = parseAddresses(message.getBcc());

    if ((toAddresses.length == 0) && (ccAddresses.length == 0) && (bccAddresses.length == 0)) {

      throw new MailException(MailException.MESSAGE_HAS_NO_RECIPIENTS);
    }

    List<Attachment> attachments = AttachmentLocalServiceUtil.getAttachments(messageId);

    List<MailFile> mailFiles = new ArrayList<MailFile>();

    for (Attachment attachment : attachments) {
      File file = AttachmentLocalServiceUtil.getFile(attachment.getAttachmentId());

      MailFile mailFile = new MailFile(file, attachment.getFileName(), attachment.getSize());

      mailFiles.add(mailFile);
    }

    _imapAccessor.sendMessage(
        account.getPersonalName(),
        account.getAddress(),
        toAddresses,
        ccAddresses,
        bccAddresses,
        message.getSubject(),
        message.getBody(),
        mailFiles);

    MessageLocalServiceUtil.deleteMessage(messageId);
  }
  /**
   * Adds the attachment to the database. Also notifies the appropriate model listeners.
   *
   * @param attachment the attachment
   * @return the attachment that was added
   * @throws SystemException if a system exception occurred
   */
  @Indexable(type = IndexableType.REINDEX)
  public Attachment addAttachment(Attachment attachment) throws SystemException {
    attachment.setNew(true);

    return attachmentPersistence.update(attachment, false);
  }
  /**
   * Updates the attachment in the database or adds it if it does not yet exist. Also notifies the
   * appropriate model listeners.
   *
   * @param attachment the attachment
   * @param merge whether to merge the attachment with the current session. See {@link
   *     com.liferay.portal.service.persistence.BatchSession#update(com.liferay.portal.kernel.dao.orm.Session,
   *     com.liferay.portal.model.BaseModel, boolean)} for an explanation.
   * @return the attachment that was updated
   * @throws SystemException if a system exception occurred
   */
  @Indexable(type = IndexableType.REINDEX)
  public Attachment updateAttachment(Attachment attachment, boolean merge) throws SystemException {
    attachment.setNew(false);

    return attachmentPersistence.update(attachment, merge);
  }