Update header before writing a modified LocalMessage
Without this the changes SmtpTransport makes to the message before sending,
e.g. removing the BCC recipients, are not reflected in the writeTo() output.
@@ -2,6 +2,8 @@ package com.fsck.k9.mailstore;
import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
import java.util.Date;
import android.content.ContentValues;
@@ -40,6 +42,7 @@ public class LocalMessage extends MimeMessage {
private long messagePartId;
private String mimeType;
private PreviewType previewType;
+ private boolean headerNeedsUpdating = false;
private LocalMessage(LocalStore localStore) {
@@ -128,10 +131,12 @@ public class LocalMessage extends MimeMessage {
} else {
Log.d(K9.LOG_TAG, "No headers available for this message!");
}
+
+ headerNeedsUpdating = false;
}
@VisibleForTesting
- public void setMessagePartId(long messagePartId) {
+ void setMessagePartId(long messagePartId) {
this.messagePartId = messagePartId;
}
@@ -165,12 +170,14 @@ public class LocalMessage extends MimeMessage {
@Override
public void setSubject(String subject) {
mSubject = subject;
+ headerNeedsUpdating = true;
}
@Override
public void setMessageId(String messageId) {
mMessageId = messageId;
+ headerNeedsUpdating = true;
}
@Override
@@ -191,6 +198,7 @@ public class LocalMessage extends MimeMessage {
@Override
public void setFrom(Address from) {
this.mFrom = new Address[] { from };
+ headerNeedsUpdating = true;
}
@@ -201,6 +209,8 @@ public class LocalMessage extends MimeMessage {
} else {
mReplyTo = replyTo;
}
+
+ headerNeedsUpdating = true;
}
@@ -231,6 +241,8 @@ public class LocalMessage extends MimeMessage {
} else {
throw new IllegalArgumentException("Unrecognized recipient type.");
}
+
+ headerNeedsUpdating = true;
}
public void setFlagInternal(Flag flag, boolean set) throws MessagingException {
@@ -517,6 +529,7 @@ public class LocalMessage extends MimeMessage {
message.messagePartId = messagePartId;
message.mimeType = mimeType;
message.previewType = previewType;
+ message.headerNeedsUpdating = headerNeedsUpdating;
return message;
}
@@ -549,6 +562,33 @@ public class LocalMessage extends MimeMessage {
return "email://messages/" + getAccount().getAccountNumber() + "/" + getFolder().getName() + "/" + getUid();
}
+ @Override
+ public void writeTo(OutputStream out) throws IOException, MessagingException {
+ if (headerNeedsUpdating) {
+ updateHeader();
+ }
+
+ super.writeTo(out);
+ }
+
+ private void updateHeader() {
+ super.setSubject(mSubject);
+ super.setReplyTo(mReplyTo);
+ super.setRecipients(RecipientType.TO, mTo);
+ super.setRecipients(RecipientType.CC, mCc);
+ super.setRecipients(RecipientType.BCC, mBcc);
+
+ if (mFrom != null && mFrom.length > 0) {
+ super.setFrom(mFrom[0]);
+ }
+
+ if (mMessageId != null) {
+ super.setMessageId(mMessageId);
+ }
+
+ headerNeedsUpdating = false;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
