예제 #1
0
  private void sendMessageTo(ArrayList<String> addresses, Message message)
      throws MessagingException {
    boolean possibleSend = false;

    close();
    open();

    message.setEncoding(m8bitEncodingAllowed ? "8bit" : null);
    // If the message has attachments and our server has told us about a limit on
    // the size of messages, count the message's size before sending it
    if (mLargestAcceptableMessage > 0 && ((LocalMessage) message).hasAttachments()) {
      if (message.calculateSize() > mLargestAcceptableMessage) {
        MessagingException me = new MessagingException("Message too large for server");
        me.setPermanentFailure(possibleSend);
        throw me;
      }
    }

    Address[] from = message.getFrom();
    try {
      // TODO: Add BODY=8BITMIME parameter if appropriate?
      executeSimpleCommand("MAIL FROM:" + "<" + from[0].getAddress() + ">");
      for (String address : addresses) {
        executeSimpleCommand("RCPT TO:" + "<" + address + ">");
      }
      executeSimpleCommand("DATA");

      EOLConvertingOutputStream msgOut =
          new EOLConvertingOutputStream(
              new SmtpDataStuffing(
                  new LineWrapOutputStream(new BufferedOutputStream(mOut, 1024), 1000)));

      message.writeTo(msgOut);

      // We use BufferedOutputStream. So make sure to call flush() !
      msgOut.flush();

      possibleSend = true; // After the "\r\n." is attempted, we may have sent the message
      executeSimpleCommand("\r\n.");
    } catch (Exception e) {
      MessagingException me = new MessagingException("Unable to send message", e);

      // "5xx text" -responses are permanent failures
      String msg = e.getMessage();
      if (msg != null && msg.startsWith("5")) {
        Log.w(K9.LOG_TAG, "handling 5xx SMTP error code as a permanent failure");
        possibleSend = false;
      }

      me.setPermanentFailure(possibleSend);
      throw me;
    } finally {
      close();
    }
  }
    @Override
    public void setFlags(List<? extends Message> messages, final Set<Flag> flags, boolean value)
        throws MessagingException {
      if (!value || !flags.contains(Flag.DELETED)) {
        /*
         * The only flagging we support is setting the Deleted flag.
         */
        return;
      }
      List<String> uids = new ArrayList<String>();
      try {
        for (Message message : messages) {
          uids.add(message.getUid());
        }

        indexUids(uids);
      } catch (IOException ioe) {
        throw new MessagingException("Could not get message number for uid " + uids, ioe);
      }
      for (Message message : messages) {

        Integer msgNum = mUidToMsgNumMap.get(message.getUid());
        if (msgNum == null) {
          MessagingException me =
              new MessagingException(
                  "Could not delete message "
                      + message.getUid()
                      + " because no msgNum found; permanent error");
          me.setPermanentFailure(true);
          throw me;
        }
        executeSimpleCommand(String.format(DELE_COMMAND + " %s", msgNum));
      }
    }