Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final original = CkEmail();
try {
original.loadEml('with-attachments.eml');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final reply = CkEmail();
try {
original.toReply(reply);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
reply.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();
var destination = original.getHeaderField('Reply-To');
if (destination == '') {
destination = original.from;
}
try {
reply.addMultipleTo(destination);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Link this answer to its parent and preserve the earlier conversation references.
final parentId = original.getHeaderField('Message-ID');
if (parentId != '') {
reply.addHeaderField('In-Reply-To', parentId);
final refs = CkStringBuilder();
refs.append(original.getHeaderField('References'));
if (refs.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.numTo < 1) {
print('Reply has no destination. Review the original sender headers.');
return;
}
print('Reply goes to: ${reply.getToAddr(0)}');
print('In-Reply-To: ${reply.getHeaderField('In-Reply-To')}');
try {
reply.saveEml('reply.eml');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Saved reply.eml.');
}