Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.0.0+

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 B4X Downloads

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim original As ChilkatEmail
original.Initialize
success = original.LoadEml("with-attachments.eml")
If success <> True Then
    Log(original.LastErrorText)
    Return
End If

Dim reply As ChilkatEmail
reply.Initialize
success = original.ToReply(reply)
If success <> True Then
    Log(original.LastErrorText)
    Return
End If

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
Dim destination As String = original.GetHeaderField("Reply-To")
If destination = "" Then
    destination = original.From
End If

success = reply.AddMultipleTo(destination)
If success <> True Then
    Log(reply.LastErrorText)
    Return
End If

'  Link this answer to its parent and preserve the earlier conversation references.
Dim parentId As String = original.GetHeaderField("Message-ID")
If parentId <> "" Then
    reply.AddHeaderField("In-Reply-To", parentId)
    Dim refs As ChilkatStringBuilder
    refs.Initialize
    refs.Append(original.GetHeaderField("References"))
    If refs.Length > 0 Then
        refs.Append(" ")
    End If

    refs.Append(parentId)
    reply.AddHeaderField("References", refs.GetAsString)
End If

'  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 Then
    Log("Reply has no destination. Review the original sender headers.")
    Return
End If

Log("Reply goes to: " & reply.GetToAddr(0))
Log("In-Reply-To: " & reply.GetHeaderField("In-Reply-To"))

success = reply.SaveEml("reply.eml")
If success <> True Then
    Log(reply.LastErrorText)
    Return
End If

Log("Saved reply.eml.")