Ejemplo n.º 1
0
  /**
   * @param rundata
   * @param context
   * @param msgList
   * @return
   */
  @Override
  protected boolean loadFormData(RunData rundata, Context context, List<String> msgList) {

    mailType.setValue(rundata.getParameters().getInt(WebMailUtils.MAIL_TYPE, TYPE_NEW_MAIL));

    try {
      ALLocalMailMessage msg =
          (ALLocalMailMessage)
              WebMailUtils.getSelectedLocalMailMessage(
                  rundata, context, (int) getMailType().getValue());
      if (msg == null) {
        return false;
      }

      String tmpSubject = null;
      if (getMailType().getValue() == TYPE_NEW_MAIL) {
        // unreachable flow
        // TO
        Address[] tos = msg.getRecipients(Message.RecipientType.TO);
        this.setTo(ALMailUtils.getAddressString(tos));

        // CC
        Address[] ccs = msg.getRecipients(Message.RecipientType.CC);
        this.setCc(ALMailUtils.getAddressString(ccs));
        // BCC
        Address[] bccs = msg.getRecipients(Message.RecipientType.BCC);
        this.setBcc(ALMailUtils.getAddressString(bccs));

        tmpSubject = msg.getSubject();
      } else if (getMailType().getValue() == TYPE_REPLY_MAIL) {
        // TO
        this.setTo(ALMailUtils.getFromDelegateExtract(msg));
        tmpSubject = "Re: " + msg.getSubject();
      } else if (getMailType().getValue() == TYPE_FORWARD_MAIL) {
        tmpSubject = "Fwd: " + msg.getSubject();
      } else if (getMailType().getValue() == TYPE_REPLY_ALL_MAIL) {
        // TO
        Address[] from = ALMailUtils.getFromDelegateExtractForAddress(msg);
        Address[] to = msg.getRecipients(Message.RecipientType.TO, false);

        EipMMailAccount myaccount = ALMailUtils.getMailAccount(userId, accountId);
        String myaddress = myaccount.getMailAddress();

        List<Address> allList = new ArrayList<Address>();
        allList.addAll(Arrays.asList(from));
        allList.addAll(Arrays.asList(to));
        List<Address> replayList = new ArrayList<Address>();
        for (Address address : allList) {
          if (address instanceof InternetAddress) {
            InternetAddress internetAddress = (InternetAddress) address;
            String email = internetAddress.getAddress();
            if (email != null
                && !email.equalsIgnoreCase(myaddress)
                && !email.contains("<" + myaddress + ">")) {
              replayList.add(address);
            }
          }
        }
        this.setTo(
            ALMailUtils.getAddressString(replayList.toArray(new Address[replayList.size()])));

        // CC
        Address[] ccs = msg.getRecipients(Message.RecipientType.CC, false);
        this.setCc(ALMailUtils.getAddressString(ccs));

        // BCC
        Address[] bccs = msg.getRecipients(Message.RecipientType.BCC);
        this.setBcc(ALMailUtils.getAddressString(bccs));

        tmpSubject = "Re: " + msg.getSubject();
      }

      // Subject
      this.setSubject(tmpSubject);

      // 返信の原文付与
      String[] tmp2 = msg.getBodyTextArray();

      StringBuffer replies = new StringBuffer();
      replies.append("\r\n\r\n\r\n" + "------Original Message-------\r\n");
      if (tmp2 != null) {
        for (String factor : tmp2) {
          replies.append("> " + factor + "\r\n");
        }
      }

      // Body
      try {
        // オブジェクトモデルを取得
        EipMMailAccount account = ALMailUtils.getMailAccount(userId, accountId);
        // 署名と返信とを本文に追加
        if (account.getSignature() != null && !"".equals(account.getSignature())) {
          body.setValue(replies + "\r\n\r\n\r\n" + account.getSignature());
        } else {
          body.setValue(replies.toString());
        }
      } catch (Exception ex) {
        logger.error("webmail", ex);
      }

      if (getMailType().getValue() == TYPE_FORWARD_MAIL) {
        msg =
            (ALLocalMailMessage)
                WebMailUtils.getSelectedLocalMailMessage(rundata, context, TYPE_NEW_MAIL);
        if (msg == null) {
          return false;
        }
        String[] filenames = msg.getAttachmentFileNameArray();
        if (filenames != null && filenames.length > 0) {
          /** 添付ファイルを含んだメールを転送する */
          if (folderName == null || folderName.equals("")) {
            folderName = "undefined";
          }

          for (int i = 0; i < filenames.length; i++) {
            /** 各々の添付ファイルを、一度ファイルに書き出して再度添付する */
            int fileId = Long.valueOf(System.nanoTime()).intValue();
            String newAttachmentFileName = String.valueOf(fileId);
            String realfilename = filenames[i];

            if (realfilename == null) {
              continue;
            }

            ALStorageService.createNewTmpFile(
                msg.getInputStream(i), userId, folderName, newAttachmentFileName, realfilename);

            FileuploadLiteBean filebean = new FileuploadLiteBean();
            filebean.initField();
            filebean.setFileId(fileId);
            filebean.setFileName(realfilename);
            filebean.setFolderName(folderName);
            fileuploadList.add(filebean);
          }
        }
      }
      return true;
    } catch (Exception e) {
      logger.error("webmail", e);
      return false;
    }
  }
Ejemplo n.º 2
0
  /**
   * @param rundata
   * @param context
   * @param msgList
   * @return
   */
  @Override
  protected boolean insertFormData(RunData rundata, Context context, List<String> msgList) {

    String[] attachmentFilepaths = null;
    try {
      FileuploadLiteBean filebean = null;
      boolean hasAttachments = (fileuploadList != null && fileuploadList.size() > 0);
      if (hasAttachments) {
        int size = fileuploadList.size();
        attachmentFilepaths = new String[size];
        for (int i = 0; i < size; i++) {
          filebean = fileuploadList.get(i);
          attachmentFilepaths[i] =
              ALStorageService.getDocumentPath(
                      FileuploadUtils.FOLDER_TMP_FOR_ATTACHMENT_FILES,
                      userId + ALStorageService.separator() + folderName)
                  + ALStorageService.separator()
                  + filebean.getFileId();
        }
      }

      // 件名の値を検証
      if (subject.getValue() == null || subject.getValue().equals("")) {
        subject.setValue("無題");
      }

      // 返信メールの場合は,ヘッダを追加する.
      Map<String, String> map = null;
      if (getMailType().getValue() == TYPE_REPLY_MAIL
          || getMailType().getValue() == TYPE_REPLY_ALL_MAIL) {
        ALLocalMailMessage msg = null;
        try {
          msg =
              (ALLocalMailMessage)
                  WebMailUtils.getSelectedLocalMailMessage(
                      rundata, context, (int) getMailType().getValue());
          if (msg == null) {
            return false;
          }
        } catch (Exception e) {
          return false;
        }
        String in_reply_tos = msg.getMessageID();
        StringBuffer reference = new StringBuffer();
        String[] references = msg.getHeader("References");
        map = new LinkedHashMap<String, String>();
        if (references != null && references.length > 0) {
          reference.append(ALMailUtils.getOneString(references, " "));
        }
        if (in_reply_tos != null && (!in_reply_tos.equals(""))) {
          map.put("In-Reply-To", in_reply_tos);
          reference.append(" ").append(in_reply_tos);
        }

        map.put("References", reference.toString());
      }

      if (map != null && map.size() == 0) {
        map = null;
      }

      String delim = ",;";

      // オブジェクトモデルを取得
      EipMMailAccount account = ALMailUtils.getMailAccount(userId, accountId);

      ALMailHandler handler = ALMailFactoryService.getInstance().getMailHandler();
      // 送信サーバ情報
      ALMailSenderContext scontext = ALMailUtils.getALSmtpMailSenderContext(orgId, account);

      // 送信メッセージのコンテキスト
      ALSmtpMailContext mailcontext =
          ALMailUtils.getALSmtpMailContext(
              ALMailUtils.getTokens(ALStringUtil.unsanitizing(to.getValue()), delim),
              ALMailUtils.getTokens(ALStringUtil.unsanitizing(cc.getValue()), delim),
              ALMailUtils.getTokens(ALStringUtil.unsanitizing(bcc.getValue()), delim),
              account.getMailAddress(),
              ALStringUtil.unsanitizing(account.getMailUserName()),
              ALStringUtil.unsanitizing(subject.getValue()),
              ALStringUtil.unsanitizing(body.getValue()),
              attachmentFilepaths,
              map);

      int success_send = handler.send(scontext, mailcontext);

      if (success_send == ALSmtpMailSender.SEND_MSG_SUCCESS) {
        if (hasAttachments) {
          // 添付ファイル保存先のフォルダを削除
          ALStorageService.deleteTmpFolder(userId, folderName);
        }
      } else {
        if (success_send == ALSmtpMailSender.SEND_MSG_FAIL) {
          msgList.add("メールを送信できませんでした。アカウント設定が間違っている可能性があります。");
        } else if (success_send == ALSmtpMailSender.SEND_MSG_OVER_MAIL_MAX_SIZE) {
          msgList.add(
              String.valueOf(FileuploadUtils.getMaxFileSize()).concat("MB を超えるサイズのメールは送信できません。"));
        } else if (success_send == ALSmtpMailSender.SEND_MSG_FAIL_SMTP_AUTH) {
          msgList.add("メールを送信できませんでした。SMTP認証の認証に失敗しました。");
        }

        return false;
      }
    } catch (Exception e) {
      logger.error("webmail", e);
      msgList.add("メールを送信できませんでした。アカウント設定が間違っている可能性があります。");
      return false;
    }
    return true;
  }