Mailkit: Sending Drafts
Asked Answered
I

1

3

Im trying to find a method in mailkit that executes the command "Execute append" in IMAP, in C# i would do it like:

MailMessage mg = null;

using (ImapClient cl = new ImapClient("imap.gmail.com"))
{
    cl.Port = 993;
    cl.Ssl = true;
    cl.UserName = "xxxxx";
    cl.Password = "yyyyy";
    var bl = cl.Authenticate();
    if (bl == true)
    {
        //Add Draft
         var smg = new SmtpMessage("[email protected]", "[email protected]","[email protected]", "This is a test mail.", "Hi.Is it correct??");
         cl.ExecuteAppend("GMail/Drafts", smg.GetDataText(), "\\Draft",DateTimeOffset.Now);
    }
}

However observing MailKit ImapClient, i dont have this option..
How can i execute append in MailKit IMAP?

Isaak answered 27/10, 2015 at 10:11 Comment(0)
I
3

After some hours searching....

  using (var client = new ImapClient())
        {
            try
            {
                client.Connect(ConfigurationManager.AppSettings["ImapServer"], int.Parse(ConfigurationManager.AppSettings["ImapPort"]), SecureSocketOptions.Auto);

                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                // MailKit uses by default ntlm authentication
                client.Authenticate("username", "password");


                var draftFolder = client.GetFolder(SpecialFolder.Drafts);
                if (draftFolder != null)
                {
                    draftFolder.Open(FolderAccess.ReadWrite);

                    draftFolder.Append(message, MessageFlags.Draft);
                   draftFolder.Expunge();
                }
                else
                {
                    var toplevel = client.GetFolder(client.PersonalNamespaces[0]);
                    var DraftFolder = toplevel.Create(SpecialFolder.Drafts.ToString(), true);

                    DraftFolder.Open(FolderAccess.ReadWrite);
                    DraftFolder.Append(message, MessageFlags.Draft);
                    DraftFolder.Expunge();
                }

            }
            catch (Exception ex)
            {
                throw new ApplicationException("IMAPException has occured: " + ex.Message);
            }

            client.Disconnect(true);
        }
Isaak answered 27/10, 2015 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.