Exemplo n.º 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());
    }
  }
Exemplo n.º 2
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);
  }
Exemplo n.º 3
0
 public void deleteAttachment(long attachmentId) throws PortalException {
   AttachmentLocalServiceUtil.deleteAttachment(attachmentId);
 }
Exemplo n.º 4
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;
  }