Android™ Requires Chilkat v11.0.0+
Android™
Reply to an email and preserve its thread
See more Email Object Examples
Create a reply to with-attachments.eml. Address it to Reply-To when supplied, or to the original author otherwise. Link the reply to the original Message-ID, replace its body with a short answer, and print the destination before saving the draft.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkEmail original = new CkEmail();
success = original.LoadEml("with-attachments.eml");
if (success != true) {
Log.i(TAG, original.lastErrorText());
return;
}
CkEmail reply = new CkEmail();
success = original.ToReply(reply);
if (success != true) {
Log.i(TAG, original.lastErrorText());
return;
}
reply.put_From("Alex Rivera <alex@example.net>");
// This is a reply to the author, not a reply-all. Set the destination explicitly.
reply.ClearTo();
reply.ClearCC();
reply.ClearBcc();
String destination = original.getHeaderField("Reply-To");
if (destination == "") {
destination = original.ck_from();
}
success = reply.AddMultipleTo(destination);
if (success != true) {
Log.i(TAG, reply.lastErrorText());
return;
}
// Link this answer to its parent and preserve the earlier conversation references.
String parentId = original.getHeaderField("Message-ID");
if (parentId != "") {
reply.AddHeaderField("In-Reply-To",parentId);
CkStringBuilder refs = new CkStringBuilder();
refs.Append(original.getHeaderField("References"));
if (refs.get_Length() > 0) {
refs.Append(" ");
}
refs.Append(parentId);
reply.AddHeaderField("References",refs.getAsString());
}
// A short text reply has no use for the original inline artwork or nested correspondence.
reply.RemoveAttachedMessages();
reply.RemoveHtmlAlternative();
reply.DropRelatedItems();
reply.SetTextBody("Thank you for your message.","text/plain");
if (reply.get_NumTo() < 1) {
Log.i(TAG, "Reply has no destination. Review the original sender headers.");
return;
}
Log.i(TAG, "Reply goes to: " + reply.getToAddr(0));
Log.i(TAG, "In-Reply-To: " + reply.getHeaderField("In-Reply-To"));
success = reply.SaveEml("reply.eml");
if (success != true) {
Log.i(TAG, reply.lastErrorText());
return;
}
Log.i(TAG, "Saved reply.eml.");
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}