Пример #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();
    }
  }
Пример #2
0
  public List<? extends Message> appendWebDavMessages(List<? extends Message> messages)
      throws MessagingException {
    List<Message> retMessages = new ArrayList<Message>(messages.size());

    WebDavHttpClient httpclient = store.getHttpClient();

    for (Message message : messages) {
      HttpGeneric httpmethod;
      HttpResponse response;
      StringEntity bodyEntity;
      int statusCode;

      try {
        ByteArrayOutputStream out;

        out = new ByteArrayOutputStream(message.getSize());

        open(Folder.OPEN_MODE_RW);
        EOLConvertingOutputStream msgOut =
            new EOLConvertingOutputStream(new BufferedOutputStream(out, 1024));
        message.writeTo(msgOut);
        msgOut.flush();

        bodyEntity = new StringEntity(out.toString(), "UTF-8");
        bodyEntity.setContentType("message/rfc822");

        String messageURL = mFolderUrl;
        if (!messageURL.endsWith("/")) {
          messageURL += "/";
        }
        messageURL += encodeUtf8(message.getUid() + ":" + System.currentTimeMillis() + ".eml");

        Log.i(LOG_TAG, "Uploading message as " + messageURL);

        httpmethod = new HttpGeneric(messageURL);
        httpmethod.setMethod("PUT");
        httpmethod.setEntity(bodyEntity);

        String mAuthString = store.getAuthString();

        if (mAuthString != null) {
          httpmethod.setHeader("Authorization", mAuthString);
        }

        response = httpclient.executeOverride(httpmethod, store.getContext());
        statusCode = response.getStatusLine().getStatusCode();

        if (statusCode < 200 || statusCode > 300) {

          // TODO: Could we handle a login timeout here?

          throw new IOException(
              "Error with status code "
                  + statusCode
                  + " while sending/appending message.  Response = "
                  + response.getStatusLine().toString()
                  + " for message "
                  + messageURL);
        }
        WebDavMessage retMessage = new WebDavMessage(message.getUid(), this);

        retMessage.setUrl(messageURL);
        retMessages.add(retMessage);
      } catch (Exception e) {
        throw new MessagingException("Unable to append", e);
      }
    }
    return retMessages;
  }