Send email not present in the sent mail folder.

This article shows how to append sent email to a sent mail folder.

Date Entered: 03/07/2019    Last Updated: 03/07/2019

When sending email using one of the SMTP-based components (SMTP, HTMLMailer, etc.), a copy of the sent email is not guaranteed to be present in a sent mail folder on the server. While some SMTP servers will automatically append a copy of sent email to a folder (e.g. Gmail), this functionality is not actually part of the SMTP protocol itself. For cases where this is not done automatically, the email would need to be manually appended to the sent mail folder using the IMAP APPEND command. This is supported in the IMAP component via the AppendToMailbox method.

First, a copy of the sent email will need to be saved by the sending component. There are a few configuration settings in the SMTP-based components that can help with this: MessageHeadersString and TransferText. The first contains the headers for the sent email, and the latter can be used in conjunction with the Transfer event to aggregate the sent email body. For example:

// ... StringBuilder message = new StringBuilder(); hm.OnTransfer += (sender, args) => { message.Append(hm.Config("TransferText")); }; hm.Send(); String header = hm.Config("MessageHeadersString"); String body = message.ToString();

After this data is saved, the IMAP component can be used to append the email to the sent mail folder. This is done by first setting MessageHeadersString and MessageText to the message headers and body, respectively. Then, select the desired sent mail folder prior to calling the AppendToMailbox method. For example:

// ... imap.Connect(); imap.MessageHeadersString = header; imap.MessageText = body; imap.Mailbox = "Sent Mail"; // The name of this folder will differ based on the IMAP server. imap.SelectMailbox(); imap.AppendToMailbox(); //Append to IMAP.

Note that the name of the sent mail folder will vary based on the IMAP server; a call to ListMailboxes can be used to help determine the name to use.


We appreciate your feedback.  If you have any questions, comments, or suggestions about this entry please contact our support team at kb@nsoftware.com.