Example #1
0
  public AttachmentHandler getAttachment(long attachmentId) throws IOException, PortalException {

    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());
    }
  }
Example #2
0
  public void synchronizeMessage(long messageId) throws PortalException {
    Message message = MessageLocalServiceUtil.getMessage(messageId);

    long remoteMessageId = message.getRemoteMessageId();

    if (remoteMessageId == 0) {
      return;
    }

    try {
      _imapAccessor.storeContents(message.getFolderId(), new long[] {message.getRemoteMessageId()});
    } catch (IOException ioe) {
      throw new MailException(ioe);
    }
  }
Example #3
0
  public void moveMessages(long folderId, long[] messageIds) throws PortalException {

    for (long messageId : messageIds) {
      Message message = MessageLocalServiceUtil.getMessage(messageId);

      Account account = AccountLocalServiceUtil.getAccount(message.getAccountId());

      long sourceFolderId = message.getFolderId();

      if ((account.getDraftFolderId() == sourceFolderId)
          || (account.getSentFolderId() == sourceFolderId)) {

        throw new MailException(MailException.FOLDER_INVALID_DESTINATION);
      }

      _imapAccessor.moveMessages(sourceFolderId, folderId, new long[] {messageId}, true);
    }
  }
Example #4
0
  public void sendMessage(long accountId, long messageId) throws PortalException {

    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<>();

    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);
  }
Example #5
0
  public Message saveDraft(
      long accountId,
      long messageId,
      String to,
      String cc,
      String bcc,
      String subject,
      String body,
      List<MailFile> mailFiles)
      throws PortalException {

    Account account = AccountLocalServiceUtil.getAccount(accountId);

    StringBundler sb = new StringBundler();

    sb.append(user.getFullName());
    sb.append(" <");
    sb.append(account.getAddress());
    sb.append(StringPool.GREATER_THAN);

    String sender = sb.toString();

    Address[] toAddresses = parseAddresses(to);
    Address[] ccAddresses = parseAddresses(cc);
    Address[] bccAddresses = parseAddresses(bcc);

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

      throw new MailException(MailException.MESSAGE_HAS_NO_RECIPIENTS);
    }

    Message message = null;

    if (messageId != 0) {
      message =
          MessageLocalServiceUtil.updateMessage(
              messageId,
              account.getDraftFolderId(),
              sender,
              InternetAddressUtil.toString(toAddresses),
              InternetAddressUtil.toString(ccAddresses),
              InternetAddressUtil.toString(bccAddresses),
              null,
              subject,
              body,
              String.valueOf(MailConstants.FLAG_DRAFT),
              0);
    } else {
      message =
          MessageLocalServiceUtil.addMessage(
              user.getUserId(),
              account.getDraftFolderId(),
              sender,
              to,
              cc,
              bcc,
              null,
              subject,
              body,
              String.valueOf(MailConstants.FLAG_DRAFT),
              0,
              null);
    }

    if (mailFiles == null) {
      return message;
    }

    for (MailFile mailFile : mailFiles) {
      AttachmentLocalServiceUtil.addAttachment(
          user.getUserId(),
          message.getMessageId(),
          null,
          mailFile.getFileName(),
          mailFile.getSize(),
          mailFile.getFile());
    }

    return message;
  }