I'm using the MailKit library to make a simple WPF app to send automatic emails. I hired hosting that uses Cpanel to create emails with Roundcube, where I am using the information to connect, log in, etc. When I manually logged into Roundcube, I noticed that the test emails I sent were not in the sent folder. I can send emails perfectly with the following code:
public static async Task SendMail(BilletInfo info, string m)
{
try
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(Server.Name, Server.Login));
message.To.Add(new MailboxAddress(info.ClientName, info.Mail));
message.Subject = MessageController.GetChargeTitle(info);
var bodyBuilder = new BodyBuilder();
bodyBuilder.TextBody = m;
bodyBuilder.Attachments.Add(info.AttachmentPath);
message.Body = bodyBuilder.ToMessageBody();
using(var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect(Server.ServerName, Server.SMTPPort, SecureSocketOptions.Auto);
client.Authenticate(Server.Login, Server.Password);
await client.SendAsync(message);
client.Disconnect(true);
}
}
catch (Exception ex)
{
MessageBoxController.ShowError($ "{ex.Message}");
}
}
However, it does not appear in the Roundcube client. Is there any way to save messages sent by MailKit on the server in a way that appears in the Roundcube client?
You'll need to use the ImapClient to save the message to an IMAP "Sent" folder.
using (var client = new ImapClient()) {
client.Connect(host, port, SecureSocketOptions.Auto);
client.Authenticate(username, password);
// Hopefully your server supports the SPECIAL-USE or XLIST extensions
// to make getting the Sent folder easy.
IMailFolder sent = null;
if (client.Capabilities.HasFlag(ImapCapabilities.SpecialUse) ||
client.Capabilities.HasFlag(ImapCapabilities.XList)) {
sent = client.GetFolder(SpecialFolder.Sent);
}
if (sent == null) {
var personal = client.GetFolder(client.PersonalNamespaces[0]);
sent = personal.GetSubfolders().FirstOrDefault(subfolder => subfolder.Name.StartsWith("Sent"));
}
sent?.Append(message);
client.Disconnect(true);
}