From 360cae0cabc58c777ad0a7ebfeb0beef56325087 Mon Sep 17 00:00:00 2001 From: Andy Edinborough Date: Tue, 13 Mar 2012 03:13:03 -0500 Subject: [PATCH] better support for AppendMail #44. There is still much work left to do. The `MailMessage.Save(...)` method doesn't yet support extended characters or attachments. --- ImapClient.cs | 18 +++++++++++++----- Tests/Clients.cs | 13 +++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/ImapClient.cs b/ImapClient.cs index ec4cef8..27231cd 100644 --- a/ImapClient.cs +++ b/ImapClient.cs @@ -179,18 +179,26 @@ protected override void OnDispose() { } } - public void AppendMail(string mailbox, MailMessage email) { + public void AppendMail(MailMessage email, string mailbox = null) { IdlePause(); string flags = String.Empty; - string size = (email.Body.Length - 1).ToString(); + var body = new StringBuilder(); + using (var txt = new System.IO.StringWriter(body)) + email.Save(txt); + + string size = body.Length.ToString(); if (email.RawFlags.Length > 0) { - flags = string.Concat("(", string.Join(" ", email.Flags), ")"); + flags = " (" + string.Join(" ", email.Flags) + ")"; } - string command = GetTag() + "APPEND " + mailbox.QuoteString() + " " + flags + " {" + size + "}"; + + if (mailbox == null) CheckMailboxSelected(); + mailbox = mailbox ?? _SelectedMailbox; + + string command = GetTag() + "APPEND " + (mailbox ?? _SelectedMailbox).QuoteString() + flags + " {" + size + "}"; string response = SendCommandGetResponse(command); if (response.StartsWith("+")) { - response = SendCommandGetResponse(email.Body); + response = SendCommandGetResponse(body.ToString()); } IdleResume(); } diff --git a/Tests/Clients.cs b/Tests/Clients.cs index 2e694c0..3fb26ac 100644 --- a/Tests/Clients.cs +++ b/Tests/Clients.cs @@ -128,6 +128,19 @@ public void TestIssue49() { } } + [TestMethod] + public void TestAppendMail() { + using (var client = GetClient()) { + var msg = new MailMessage { + Subject = "TEST", + Body = "Appended!" + }; + msg.Date = DateTime.Now; + + client.AppendMail("Inbox", msg); + } + } + [TestMethod] public void TestParseImapHeader() { var header = @"X-GM-THRID 1320777376118077475 X-GM-MSGID 1320777376118077475 X-GM-LABELS () UID 8286 RFC822.SIZE 9369 FLAGS (\Seen) BODY[] {9369}";