Search in sources :

Example 6 with EOLConvertingOutputStream

use of com.fsck.k9.mail.filter.EOLConvertingOutputStream in project k-9 by k9mail.

the class SmtpTransport method sendMessageTo.

private void sendMessageTo(List<String> addresses, Message message) throws MessagingException {
    close();
    open();
    if (!m8bitEncodingAllowed) {
        Log.d(LOG_TAG, "Server does not support 8bit transfer encoding");
    }
    // the size of messages, count the message's size before sending it
    if (mLargestAcceptableMessage > 0 && message.hasAttachments()) {
        if (message.calculateSize() > mLargestAcceptableMessage) {
            throw new MessagingException("Message too large for server", true);
        }
    }
    boolean entireMessageSent = false;
    Address[] from = message.getFrom();
    try {
        String fromAddress = from[0].getAddress();
        if (m8bitEncodingAllowed) {
            executeCommand("MAIL FROM:<%s> BODY=8BITMIME", fromAddress);
        } else {
            executeCommand("MAIL FROM:<%s>", fromAddress);
        }
        for (String address : addresses) {
            executeCommand("RCPT TO:<%s>", address);
        }
        executeCommand("DATA");
        EOLConvertingOutputStream msgOut = new EOLConvertingOutputStream(new LineWrapOutputStream(new SmtpDataStuffing(mOut), 1000));
        message.writeTo(msgOut);
        msgOut.endWithCrLfAndFlush();
        // After the "\r\n." is attempted, we may have sent the message
        entireMessageSent = true;
        executeCommand(".");
    } catch (NegativeSmtpReplyException e) {
        throw e;
    } catch (Exception e) {
        MessagingException me = new MessagingException("Unable to send message", e);
        me.setPermanentFailure(entireMessageSent);
        throw me;
    } finally {
        close();
    }
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) SmtpDataStuffing(com.fsck.k9.mail.filter.SmtpDataStuffing) SocketAddress(java.net.SocketAddress) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) Address(com.fsck.k9.mail.Address) Inet6Address(java.net.Inet6Address) LineWrapOutputStream(com.fsck.k9.mail.filter.LineWrapOutputStream) MessagingException(com.fsck.k9.mail.MessagingException) URISyntaxException(java.net.URISyntaxException) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) GeneralSecurityException(java.security.GeneralSecurityException) SSLException(javax.net.ssl.SSLException) SocketException(java.net.SocketException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException)

Example 7 with EOLConvertingOutputStream

use of com.fsck.k9.mail.filter.EOLConvertingOutputStream in project k-9 by k9mail.

the class WebDavFolder method appendWebDavMessages.

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;
            long size = message.getSize();
            if (size > Integer.MAX_VALUE) {
                throw new MessagingException("message size > Integer.MAX_VALUE!");
            }
            out = new ByteArrayOutputStream((int) size);
            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);
            store.sendRequest(messageURL, "PUT", bodyEntity, null, true);
            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;
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) Message(com.fsck.k9.mail.Message) MessagingException(com.fsck.k9.mail.MessagingException) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) StringEntity(org.apache.http.entity.StringEntity) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with EOLConvertingOutputStream

use of com.fsck.k9.mail.filter.EOLConvertingOutputStream in project k-9 by k9mail.

the class SmtpTransport method sendMessage.

@Override
public void sendMessage(Message message) throws MessagingException {
    Set<String> addresses = new LinkedHashSet<>();
    for (Address address : message.getRecipients(RecipientType.TO)) {
        addresses.add(address.getAddress());
    }
    for (Address address : message.getRecipients(RecipientType.CC)) {
        addresses.add(address.getAddress());
    }
    for (Address address : message.getRecipients(RecipientType.BCC)) {
        addresses.add(address.getAddress());
    }
    message.removeHeader("Bcc");
    if (addresses.isEmpty()) {
        return;
    }
    close();
    open();
    // the size of messages, count the message's size before sending it
    if (largestAcceptableMessage > 0 && message.hasAttachments()) {
        if (message.calculateSize() > largestAcceptableMessage) {
            throw new MessagingException("Message too large for server", true);
        }
    }
    boolean entireMessageSent = false;
    try {
        String mailFrom = constructSmtpMailFromCommand(message.getFrom(), is8bitEncodingAllowed);
        if (isPipeliningSupported) {
            Queue<String> pipelinedCommands = new LinkedList<>();
            pipelinedCommands.add(mailFrom);
            for (String address : addresses) {
                pipelinedCommands.add(String.format("RCPT TO:<%s>", address));
            }
            executePipelinedCommands(pipelinedCommands);
            readPipelinedResponse(pipelinedCommands);
        } else {
            executeCommand(mailFrom);
            for (String address : addresses) {
                executeCommand("RCPT TO:<%s>", address);
            }
        }
        executeCommand("DATA");
        EOLConvertingOutputStream msgOut = new EOLConvertingOutputStream(new LineWrapOutputStream(new SmtpDataStuffing(outputStream), 1000));
        message.writeTo(msgOut);
        msgOut.endWithCrLfAndFlush();
        // After the "\r\n." is attempted, we may have sent the message
        entireMessageSent = true;
        executeCommand(".");
    } catch (NegativeSmtpReplyException e) {
        throw e;
    } catch (Exception e) {
        throw new MessagingException("Unable to send message", entireMessageSent, e);
    } finally {
        close();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) SocketAddress(java.net.SocketAddress) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) Address(com.fsck.k9.mail.Address) Inet6Address(java.net.Inet6Address) MessagingException(com.fsck.k9.mail.MessagingException) LinkedList(java.util.LinkedList) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) SocketException(java.net.SocketException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) SmtpDataStuffing(com.fsck.k9.mail.filter.SmtpDataStuffing) LineWrapOutputStream(com.fsck.k9.mail.filter.LineWrapOutputStream)

Aggregations

EOLConvertingOutputStream (com.fsck.k9.mail.filter.EOLConvertingOutputStream)8 IOException (java.io.IOException)8 MessagingException (com.fsck.k9.mail.MessagingException)6 Message (com.fsck.k9.mail.Message)3 URISyntaxException (java.net.URISyntaxException)3 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 Address (com.fsck.k9.mail.Address)2 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)2 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)2 LineWrapOutputStream (com.fsck.k9.mail.filter.LineWrapOutputStream)2 SmtpDataStuffing (com.fsck.k9.mail.filter.SmtpDataStuffing)2 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)2 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 Inet6Address (java.net.Inet6Address)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2