简体   繁体   English

MailKit-MimeKit - 如何复制到已发文件夹

[英]MailKit-MimeKit - How to copy to Sent folder

I ama able to sent SMTP emails using MailKit & MimeKit and outlook is the client tool receiving these mails.我能够使用 MailKit 和 MimeKit 发送 SMTP 封电子邮件,而 outlook 是接收这些邮件的客户端工具。 Below code has been used and my Inbox has emails received.以下代码已被使用,我的收件箱已收到电子邮件。

var email = new MimeMessage
{
    Sender = MailboxAddress.Parse("<<from>>")
};
email.To.Add(MailboxAddress.Parse("<<to>>"));

email.Subject = "Test mail from Jaish";
var builder = new BodyBuilder();
builder.TextBody = "This is a test mail from Jaish Mathews";
email.Body = builder.ToMessageBody();

using var smtp = new SmtpClient();
smtp.LocalDomain = "<<domain>>";
smtp.Timeout = 10000;

smtp.Connect("<<host>>", 25, SecureSocketOptions.None);
var mailboxes = email.To.Mailboxes;
//Sending email
await smtp.SendAsync(email);
//Disconnecting from smtp
smtp.Disconnect(true);

Issue is that my "Sent" folder isn't keeping any track of these emails sent.问题是我的“已发送”文件夹没有跟踪这些已发送的电子邮件。 How can I manually copy to my "Sent" folder"如何手动复制到我的“已发送”文件夹”

Before I explain how to save the message in your Sent IMAP folder, I first want to bring attention to a few things.在我解释如何将邮件保存在您的 Sent IMAP 文件夹中之前,我首先想提醒您注意几件事。

  1. smtp.Timeout = 10000; It's probably best to not override the default timeout (which I believe is 120,000. 10000 is 10 seconds).最好不要覆盖默认超时(我认为是 120,000。10000 是 10 秒)。
  2. You currently have a mix of sync and async calls to the SmtpClient.您目前混合调用了 SmtpClient 的同步和异步调用。 You should pick sync or async and stick with it (at least if it's all within the same method).您应该选择同步或异步并坚持使用它(至少如果它们都在同一方法中)。

Okay, now on to your question.好的,现在开始你的问题。

using var imap = new ImapClient ();

await imap.ConnectAsync ("<<host>>", 143 /* or 993 for SSL */, SecureSocketOptions.Auto).ConfigureAwait (false);
await imap.AuthenticateAsync ("username", "password").ConfigureAwait (false);

IMailFolder sent = null;
if (imap.Capabilities.HasFlag (ImapCapabilities.SpecialUse))
    sent = imap.GetFolder (SpecialFolder.Sent);

if (sent == null) {
    // get the default personal namespace root folder
    var personal = imap.GetFolder (imap.PersonalNamespaces[0]);

    // This assumes the sent folder's name is "Sent", but use whatever the real name is
    sent = await personal.GetSubfolderAsync ("Sent").ConfigureAwait (false);
}

await sent.AppendAsync (email, MessageFlags.Seen).ConfigureAwait (false);

await imap.DisconnectAsync (true).ConfigureAwait (false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM