Saving Sent Messages to the Sent Folder with the IMAP Component


The SMTP protocol only delivers messages, so messages sent with the SMTP component (or HTMLMailer) are not automatically saved in the sender's mailbox. To keep a record of sent messages, the complete message can be captured after it is sent, and then added to the Sent folder on the mail server using the IMAP component's AppendToMailbox method.

Note that some mail providers (such as Gmail) automatically save messages sent through their SMTP servers to the Sent folder. Check whether the provider already does this before appending messages, to avoid creating duplicate copies.

Capturing the Sent Message

After calling the Send method, the complete message (exactly as it was sent) is available through two configuration settings: MessageHeadersString contains the message's headers, and TransferText contains its body. Combining them produces the full message in RFC 822 format.

SMTP smtp = new SMTP(); smtp.MailServer = "smtp.example.com"; smtp.User = "user@example.com"; smtp.Password = "PASSWORD"; smtp.From = "user@example.com"; smtp.SendTo = "recipient@example.com"; smtp.Subject = "Quarterly Report"; smtp.MessageText = "The quarterly report is ready for review."; smtp.Send(); // Capture the complete message as it was sent. string sentMessage = smtp.Config("MessageHeadersString") + smtp.Config("TransferText");

Appending the Message to the Sent Folder

To add the captured message to the Sent folder, connect to the IMAP server, set the Mailbox property to the name of the Sent folder, set MessageText to the captured message, and call the AppendToMailbox method. Setting the MessageFlags property to \Seen marks the copy as read, so that it does not appear as a new message.

IMAP imap = new IMAP(); imap.MailServer = "imap.example.com"; imap.User = "user@example.com"; imap.Password = "PASSWORD"; imap.Connect(); // The name of the Sent folder varies between servers. imap.Mailbox = "Sent"; imap.MessageText = sentMessage; imap.MessageFlags = "\\Seen"; // Mark the copy as read. imap.AppendToMailbox(); imap.Disconnect();

By default, the server sets the date of the appended message to the time it was appended. To use a different date, set the AppendToMailboxDateTime configuration setting to a date-time in the format used by the IMAP protocol (e.g., 22-Jan-2026 14:30:00 -0500).

Finding the Sent Folder

The name of the Sent folder is not standardized, and varies between servers and providers (e.g., Sent, Sent Items, Sent Messages, or [Gmail]/Sent Mail). Many servers identify their special folders using the \Sent mailbox attribute, which is reported in the Flags parameter of the MailboxList event when the mailboxes are listed.

string sentFolder = ""; imap.OnMailboxList += (s, e) => { if (e.Flags.Contains("\\Sent")) sentFolder = e.Mailbox; }; // List all of the mailboxes on the server. imap.Mailbox = "*"; imap.ListMailboxes(); Console.WriteLine("Sent folder: " + sentFolder);

If the server does not report this attribute, list the mailboxes and select the appropriate folder by name instead.

Notes

  • The same approach works with the HTMLMailer component, which also provides the MessageHeadersString and TransferText configuration settings.
  • Connecting to most mail servers requires TLS, which is configured through the components' SSLStartMode property, and many providers (such as Gmail and Microsoft 365) require OAuth authentication. For more information, please see our Using OAuth for authentication to connect with SMTP, IMAP, and POP3 article.

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