Sent email not present in the sent mail folder.
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();
htmlmailer.OnTransfer += (sender, args) => {
message.Append(htmlmailer.Config("TransferText"));
};
htmlmailer.Send();
String header = htmlmailer.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 article please contact our support team at support@nsoftware.com.