Send meeting invite mail as multipart/alternative

* Turns out that Exchange 2003 requires the ics attachment to be in a
  multipart/alternative, rather than a multipart/mixed MIME message
* Exchange 2007 accepts both types
* Therefore, we change our output for this particular situation, i.e.
  a single attachment that is an ics file, to multipart/alternative
* Rename FLAG_SUPPRESS_CONTENT_DISPOSITION to FLAG_ICS_ALTERNATIVE_PART
  and make this flag do double duty - 1) suppress the Content-Disposition
  header (also required by Exchange) and 2) send the message as
  multipart/alternative
* Add unit tests for Rfc822Output to check that mime parts are composed
  properly

Bug: 2516394
Change-Id: I60e26f57b8ecaf01d0340e7828533334e0e7d45a
This commit is contained in:
Marc Blank 2010-03-16 13:38:47 -07:00
parent e281ebc4e2
commit 0ed690bfb4
5 changed files with 186 additions and 33 deletions

View File

@ -156,11 +156,13 @@ public class Rfc822Output {
Attachment.CONTENT_PROJECTION, null, null, null);
try {
boolean mixedParts = attachmentsCursor.getCount() > 0;
String mixedBoundary = null;
int attachmentCount = attachmentsCursor.getCount();
boolean multipart = attachmentCount > 0;
String multipartBoundary = null;
String multipartType = "mixed";
// Simplified case for no multipart - just emit text and be done.
if (!mixedParts) {
if (!multipart) {
if (text != null) {
writeTextWithHeaders(writer, stream, text);
} else {
@ -169,31 +171,41 @@ public class Rfc822Output {
} else {
// continue with multipart headers, then into multipart body
writeHeader(writer, "MIME-Version", "1.0");
multipartBoundary = "--_com.android.email_" + System.nanoTime();
// Move to the first attachment; this must succeed because multipart is true
attachmentsCursor.moveToFirst();
if (attachmentCount == 1) {
// If we've got one attachment and it's an ics "attachment", we want to send
// this as multipart/alternative instead of multipart/mixed
int flags = attachmentsCursor.getInt(Attachment.CONTENT_FLAGS_COLUMN);
if ((flags & Attachment.FLAG_ICS_ALTERNATIVE_PART) != 0) {
multipartType = "alternative";
}
}
mixedBoundary = "--_com.android.email_" + System.nanoTime();
writeHeader(writer, "Content-Type",
"multipart/mixed; boundary=\"" + mixedBoundary + "\"");
"multipart/" + multipartType + "; boundary=\"" + multipartBoundary + "\"");
// Finish headers and prepare for body section(s)
writer.write("\r\n");
// first multipart element is the body
if (text != null) {
writeBoundary(writer, mixedBoundary, false);
writeBoundary(writer, multipartBoundary, false);
writeTextWithHeaders(writer, stream, text);
}
// Write out the attachments
while (attachmentsCursor.moveToNext()) {
writeBoundary(writer, mixedBoundary, false);
// Write out the attachments until we run out
do {
writeBoundary(writer, multipartBoundary, false);
Attachment attachment =
Attachment.getContent(attachmentsCursor, Attachment.class);
writeOneAttachment(context, writer, stream, attachment);
writer.write("\r\n");
}
} while (attachmentsCursor.moveToNext());
// end of multipart section
writeBoundary(writer, mixedBoundary, true);
writeBoundary(writer, multipartBoundary, true);
}
} finally {
attachmentsCursor.close();
@ -213,7 +225,7 @@ public class Rfc822Output {
writeHeader(writer, "Content-Transfer-Encoding", "base64");
// Most attachments (real files) will send Content-Disposition. The suppression option
// is used when sending calendar invites.
if ((attachment.mFlags & Attachment.FLAG_SUPPRESS_DISPOSITION) == 0) {
if ((attachment.mFlags & Attachment.FLAG_ICS_ALTERNATIVE_PART) == 0) {
writeHeader(writer, "Content-Disposition",
"attachment;"
+ "\n filename=\"" + attachment.mFileName + "\";"

View File

@ -1680,8 +1680,10 @@ public abstract class EmailContent {
};
// Bits used in mFlags
// Instruct RFC822 output code to supress "content-disposition". Used for calendar invites.
public static final int FLAG_SUPPRESS_DISPOSITION = 1<<0;
// Instruct Rfc822Output to 1) not use Content-Disposition and 2) use multipart/alternative
// with this attachment. This is only valid if there is one and only one attachment and
// that attachment has this flag set
public static final int FLAG_ICS_ALTERNATIVE_PART = 1<<0;
/**
* no public constructor since this is a utility class

View File

@ -1469,7 +1469,7 @@ public class CalendarUtilities {
att.mFileName = "invite.ics";
att.mSize = att.mContentBytes.length;
// We don't send content-disposition with this attachment
att.mFlags = Attachment.FLAG_SUPPRESS_DISPOSITION;
att.mFlags = Attachment.FLAG_ICS_ALTERNATIVE_PART;
// Add the attachment to the message
msg.mAttachments = new ArrayList<Attachment>();

View File

@ -16,9 +16,26 @@
package com.android.email.mail.transport;
import com.android.email.mail.MessagingException;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.EmailContent.Attachment;
import com.android.email.provider.EmailContent.Message;
import android.test.AndroidTestCase;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.message.Body;
import org.apache.james.mime4j.message.Entity;
import org.apache.james.mime4j.message.Header;
import org.apache.james.mime4j.message.Multipart;
import android.content.Context;
import android.test.ProviderTestCase2;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Tests of the Rfc822Output (used for sending mail)
@ -26,7 +43,7 @@ import android.test.AndroidTestCase;
* You can run this entire test case with:
* runtest -c com.android.email.mail.transport.Rfc822OutputTests email
*/
public class Rfc822OutputTests extends AndroidTestCase {
public class Rfc822OutputTests extends ProviderTestCase2<EmailProvider> {
private static final String SENDER = "sender@android.com";
private static final String REPLYTO = "replyto@android.com";
private static final String RECIPIENT_TO = "recipient-to@android.com";
@ -38,6 +55,18 @@ public class Rfc822OutputTests extends AndroidTestCase {
private static final String REPLY_BODY_SHORT = "\n\n" + SENDER + " wrote:\n\n";
private static final String REPLY_BODY = REPLY_BODY_SHORT + ">" + BODY;
private Context mMockContext;
public Rfc822OutputTests () {
super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY);
}
@Override
public void setUp() throws Exception {
super.setUp();
mMockContext = getMockContext();
}
// TODO Create more tests here. Specifically, we should test to make sure that forward works
// properly instead of just reply
@ -63,16 +92,16 @@ public class Rfc822OutputTests extends AndroidTestCase {
msg.mFlags = Message.FLAG_TYPE_REPLY;
msg.mTextReply = BODY;
msg.mIntroText = REPLY_BODY_SHORT;
msg.save(getContext());
msg.save(mMockContext);
String body = Rfc822Output.buildBodyText(getContext(), msg, true);
String body = Rfc822Output.buildBodyText(mMockContext, msg, true);
assertEquals(REPLY_BODY, body);
// Save a different message with no reply body (so we reset the id)
msg.mId = -1;
msg.mTextReply = null;
msg.save(getContext());
body = Rfc822Output.buildBodyText(getContext(), msg, true);
msg.save(mMockContext);
body = Rfc822Output.buildBodyText(mMockContext, msg, true);
assertEquals(REPLY_BODY_SHORT, body);
}
@ -89,16 +118,126 @@ public class Rfc822OutputTests extends AndroidTestCase {
msg.mFlags = Message.FLAG_TYPE_REPLY;
msg.mTextReply = BODY;
msg.mIntroText = REPLY_BODY_SHORT;
msg.save(getContext());
msg.save(mMockContext);
String body = Rfc822Output.buildBodyText(getContext(), msg, false);
String body = Rfc822Output.buildBodyText(mMockContext, msg, false);
assertEquals(TEXT + REPLY_BODY_SHORT, body);
// Save a different message with no reply body (so we reset the id)
msg.mId = -1;
msg.mTextReply = null;
msg.save(getContext());
body = Rfc822Output.buildBodyText(getContext(), msg, false);
msg.save(mMockContext);
body = Rfc822Output.buildBodyText(mMockContext, msg, false);
assertEquals(TEXT + REPLY_BODY_SHORT, body);
}
}
public void testWriteToText() throws IOException, MessagingException {
// Create a simple text message
Message msg = new Message();
msg.mText = TEXT;
msg.mFrom = SENDER;
// Save this away
msg.save(mMockContext);
// Write out an Rfc822 message
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
Rfc822Output.writeTo(mMockContext, msg.mId, byteStream, false, false);
// Get the message and create a mime4j message from it
// We'll take advantage of its parsing capabilities
ByteArrayInputStream messageInputStream =
new ByteArrayInputStream(byteStream.toByteArray());
org.apache.james.mime4j.message.Message mimeMessage =
new org.apache.james.mime4j.message.Message(messageInputStream);
// Make sure its structure is correct
assertFalse(mimeMessage.isMultipart());
assertEquals("text/plain", mimeMessage.getMimeType());
}
@SuppressWarnings("unchecked")
public void testWriteToAlternativePart() throws IOException, MessagingException {
// Create a message with alternative part
Message msg = new Message();
msg.mText = TEXT;
msg.mFrom = SENDER;
msg.mAttachments = new ArrayList<Attachment>();
// Attach a meeting invitation, which needs to be sent as multipart/alternative
Attachment att = new Attachment();
att.mContentBytes = "__CONTENT__".getBytes("UTF-8");
att.mFlags = Attachment.FLAG_ICS_ALTERNATIVE_PART;
att.mMimeType = "text/calendar";
att.mFileName = "invite.ics";
msg.mAttachments.add(att);
// Save this away
msg.save(mMockContext);
// Write out an Rfc822 message
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
Rfc822Output.writeTo(mMockContext, msg.mId, byteStream, false, false);
// Get the message and create a mime4j message from it
// We'll take advantage of its parsing capabilities
ByteArrayInputStream messageInputStream =
new ByteArrayInputStream(byteStream.toByteArray());
org.apache.james.mime4j.message.Message mimeMessage =
new org.apache.james.mime4j.message.Message(messageInputStream);
// Make sure its structure is correct
assertTrue(mimeMessage.isMultipart());
Header header = mimeMessage.getHeader();
Field contentType = header.getField("content-type");
assertTrue(contentType.getBody().contains("multipart/alternative"));
Multipart multipart = (Multipart)mimeMessage.getBody();
List<Body> partList = multipart.getBodyParts();
assertEquals(2, partList.size());
Entity part = (Entity)partList.get(0);
assertEquals("text/plain", part.getMimeType());
part = (Entity)partList.get(1);
assertEquals("text/calendar", part.getMimeType());
header = part.getHeader();
assertNull(header.getField("content-disposition"));
}
public void testWriteToMixedPart() throws IOException, MessagingException {
// Create a message with a mixed part
Message msg = new Message();
msg.mText = TEXT;
msg.mFrom = SENDER;
msg.mAttachments = new ArrayList<Attachment>();
// Attach a simple html "file"
Attachment att = new Attachment();
att.mContentBytes = "<html>Hi</html>".getBytes("UTF-8");
att.mMimeType = "text/html";
att.mFileName = "test.html";
msg.mAttachments.add(att);
// Save this away
msg.save(mMockContext);
// Write out an Rfc822 message
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
Rfc822Output.writeTo(mMockContext, msg.mId, byteStream, false, false);
// Get the message and create a mime4j message from it
// We'll take advantage of its parsing capabilities
ByteArrayInputStream messageInputStream =
new ByteArrayInputStream(byteStream.toByteArray());
org.apache.james.mime4j.message.Message mimeMessage =
new org.apache.james.mime4j.message.Message(messageInputStream);
// Make sure its structure is correct
assertTrue(mimeMessage.isMultipart());
Header header = mimeMessage.getHeader();
Field contentType = header.getField("content-type");
assertTrue(contentType.getBody().contains("multipart/mixed"));
Multipart multipart = (Multipart)mimeMessage.getBody();
List<Body> partList = multipart.getBodyParts();
assertEquals(2, partList.size());
Entity part = (Entity)partList.get(0);
assertEquals("text/plain", part.getMimeType());
part = (Entity)partList.get(1);
assertEquals("text/html", part.getMimeType());
header = part.getHeader();
assertNotNull(header.getField("content-disposition"));
}
}

View File

@ -233,8 +233,8 @@ public class CalendarUtilitiesTests extends AndroidTestCase {
Attachment att = msg.mAttachments.get(0);
// And that the attachment has the correct elements
assertEquals("invite.ics", att.mFileName);
assertEquals(Attachment.FLAG_SUPPRESS_DISPOSITION,
att.mFlags & Attachment.FLAG_SUPPRESS_DISPOSITION);
assertEquals(Attachment.FLAG_ICS_ALTERNATIVE_PART,
att.mFlags & Attachment.FLAG_ICS_ALTERNATIVE_PART);
assertEquals("text/calendar; method=REPLY", att.mMimeType);
assertNotNull(att.mContentBytes);
assertEquals(att.mSize, att.mContentBytes.length);
@ -272,8 +272,8 @@ public class CalendarUtilitiesTests extends AndroidTestCase {
Attachment att = msg.mAttachments.get(0);
// And that the attachment has the correct elements
assertEquals("invite.ics", att.mFileName);
assertEquals(Attachment.FLAG_SUPPRESS_DISPOSITION,
att.mFlags & Attachment.FLAG_SUPPRESS_DISPOSITION);
assertEquals(Attachment.FLAG_ICS_ALTERNATIVE_PART,
att.mFlags & Attachment.FLAG_ICS_ALTERNATIVE_PART);
assertEquals("text/calendar; method=REQUEST", att.mMimeType);
assertNotNull(att.mContentBytes);
assertEquals(att.mSize, att.mContentBytes.length);
@ -337,8 +337,8 @@ public class CalendarUtilitiesTests extends AndroidTestCase {
Attachment att = msg.mAttachments.get(0);
// And that the attachment has the correct elements
assertEquals("invite.ics", att.mFileName);
assertEquals(Attachment.FLAG_SUPPRESS_DISPOSITION,
att.mFlags & Attachment.FLAG_SUPPRESS_DISPOSITION);
assertEquals(Attachment.FLAG_ICS_ALTERNATIVE_PART,
att.mFlags & Attachment.FLAG_ICS_ALTERNATIVE_PART);
assertEquals("text/calendar; method=REQUEST", att.mMimeType);
assertNotNull(att.mContentBytes);