1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-08-13 17:03:48 -04:00

Use 8bit transfer encoding for the text body if SMTP server advertises 8BITMIME. In all other cases base64 is still used (including saved copies in IMAP "Sent" folder).

Feel free to revert this if anything breaks.
This commit is contained in:
cketti 2010-02-04 23:37:50 +00:00
parent 6480e78b97
commit 45036ae5c8
5 changed files with 73 additions and 5 deletions

View File

@ -143,4 +143,6 @@ public abstract class Message implements Part, Body
}
public abstract void saveChanges() throws MessagingException;
public abstract void setEncoding(String encoding);
}

View File

@ -3,6 +3,9 @@ package com.fsck.k9.mail;
import java.util.ArrayList;
import com.fsck.k9.mail.internet.MimeHeader;
import com.fsck.k9.mail.internet.TextBody;
public abstract class Multipart implements Body
{
protected Part mParent;
@ -55,4 +58,25 @@ public abstract class Multipart implements Body
{
this.mParent = parent;
}
public void setEncoding(String encoding)
{
for (BodyPart part : mParts)
{
try
{
Body body = part.getBody();
if (body instanceof TextBody)
{
part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
((TextBody)body).setEncoding(encoding);
}
}
catch (MessagingException e)
{
// Ignore
}
}
}
}

View File

@ -369,9 +369,9 @@ public class MimeMessage extends Message
{
this.mBody = body;
setHeader("MIME-Version", "1.0");
if (body instanceof com.fsck.k9.mail.Multipart)
if (body instanceof Multipart)
{
com.fsck.k9.mail.Multipart multipart = ((com.fsck.k9.mail.Multipart)body);
Multipart multipart = ((Multipart)body);
multipart.setParent(this);
setHeader(MimeHeader.HEADER_CONTENT_TYPE, multipart.getContentType());
}
@ -431,6 +431,19 @@ public class MimeMessage extends Message
return null;
}
public void setEncoding(String encoding)
{
if (mBody instanceof Multipart)
{
((Multipart)mBody).setEncoding(encoding);
}
else if (mBody instanceof TextBody)
{
setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
((TextBody)mBody).setEncoding(encoding);
}
}
class MimeMessageBuilder implements ContentHandler
{
private Stack stack = new Stack();

View File

@ -9,7 +9,8 @@ import java.io.*;
public class TextBody implements Body
{
String mBody;
private String mBody;
private String mEncoding;
public TextBody(String body)
{
@ -18,10 +19,17 @@ public class TextBody implements Body
public void writeTo(OutputStream out) throws IOException, MessagingException
{
if (mBody!=null)
if (mBody != null)
{
byte[] bytes = mBody.getBytes("UTF-8");
out.write(Base64.encodeBase64Chunked(bytes));
if ("8bit".equals(mEncoding))
{
out.write(bytes);
}
else
{
out.write(Base64.encodeBase64Chunked(bytes));
}
}
}
@ -57,4 +65,9 @@ public class TextBody implements Body
return null;
}
}
public void setEncoding(String encoding)
{
mEncoding = encoding;
}
}

View File

@ -57,6 +57,7 @@ public class SmtpTransport extends Transport
PeekableInputStream mIn;
OutputStream mOut;
private boolean m8bitEncodingAllowed;
/**
* smtp://user:password@server:port CONNECTION_SECURITY_NONE
@ -184,6 +185,8 @@ public class SmtpTransport extends Transport
List<String> results = executeSimpleCommand("EHLO " + localHost);
m8bitEncodingAllowed = results.contains("8BITMIME");
/*
* TODO may need to add code to fall back to HELO I switched it from
* using HELO on non STARTTLS connections because of AOL's mail
@ -285,10 +288,19 @@ public class SmtpTransport extends Transport
{
close();
open();
if (m8bitEncodingAllowed)
{
//TODO: Make sure that we don't have lines that are longer than
// 998 bytes (don't count characters!)
message.setEncoding("8bit");
}
Address[] from = message.getFrom();
boolean possibleSend = false;
try
{
//TODO: Add BODY=8BITMIME parameter if appropriate?
executeSimpleCommand("MAIL FROM: " + "<" + from[0].getAddress() + ">");
for (Address address : message.getRecipients(RecipientType.TO))
{
@ -304,9 +316,13 @@ public class SmtpTransport extends Transport
}
message.setRecipients(RecipientType.BCC, null);
executeSimpleCommand("DATA");
//TODO: Data stuffing (RFC 821 4.5.2.)
EOLConvertingOutputStream msgOut = new EOLConvertingOutputStream(
new BufferedOutputStream(mOut, 1024));
message.writeTo(msgOut);
// We use BufferedOutputStream. So make sure to call flush() !
msgOut.flush();