Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
original: HCkEmail;
reply: HCkEmail;
destination: PWideChar;
parentId: PWideChar;
refs: HCkStringBuilder;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
original := CkEmail_Create();
success := CkEmail_LoadEml(original,'with-attachments.eml');
if (success <> True) then
begin
Memo1.Lines.Add(CkEmail__lastErrorText(original));
Exit;
end;
reply := CkEmail_Create();
success := CkEmail_ToReply(original,reply);
if (success <> True) then
begin
Memo1.Lines.Add(CkEmail__lastErrorText(original));
Exit;
end;
CkEmail_putFrom(reply,'Alex Rivera <alex@example.net>');
// This is a reply to the author, not a reply-all. Set the destination explicitly.
CkEmail_ClearTo(reply);
CkEmail_ClearCC(reply);
CkEmail_ClearBcc(reply);
destination := CkEmail__getHeaderField(original,'Reply-To');
if (destination = '') then
begin
destination := CkEmail__from(original);
end;
success := CkEmail_AddMultipleTo(reply,destination);
if (success <> True) then
begin
Memo1.Lines.Add(CkEmail__lastErrorText(reply));
Exit;
end;
// Link this answer to its parent and preserve the earlier conversation references.
parentId := CkEmail__getHeaderField(original,'Message-ID');
if (parentId <> '') then
begin
CkEmail_AddHeaderField(reply,'In-Reply-To',parentId);
refs := CkStringBuilder_Create();
CkStringBuilder_Append(refs,CkEmail__getHeaderField(original,'References'));
if (CkStringBuilder_getLength(refs) > 0) then
begin
CkStringBuilder_Append(refs,' ');
end;
CkStringBuilder_Append(refs,parentId);
CkEmail_AddHeaderField(reply,'References',CkStringBuilder__getAsString(refs));
end;
// A short text reply has no use for the original inline artwork or nested correspondence.
CkEmail_RemoveAttachedMessages(reply);
CkEmail_RemoveHtmlAlternative(reply);
CkEmail_DropRelatedItems(reply);
CkEmail_SetTextBody(reply,'Thank you for your message.','text/plain');
if (CkEmail_getNumTo(reply) < 1) then
begin
Memo1.Lines.Add('Reply has no destination. Review the original sender headers.');
Exit;
end;
Memo1.Lines.Add('Reply goes to: ' + CkEmail__getToAddr(reply,0));
Memo1.Lines.Add('In-Reply-To: ' + CkEmail__getHeaderField(reply,'In-Reply-To'));
success := CkEmail_SaveEml(reply,'reply.eml');
if (success <> True) then
begin
Memo1.Lines.Add(CkEmail__lastErrorText(reply));
Exit;
end;
Memo1.Lines.Add('Saved reply.eml.');
CkEmail_Dispose(original);
CkEmail_Dispose(reply);
CkStringBuilder_Dispose(refs);