/// <summary> /// Appends the specified mail message. /// </summary> /// <param name="mailMessage">The mail message.</param> public void Append(MailMessage <MailMessageHeader> mailMessage) { Imap4Client imap4Client = null; try { imap4Client = _imapClientProvider.GetImapClient(); var mailBox = imap4Client.SelectMailbox(SentItemMailBox); var imapMail = new Message { From = new Address(mailMessage.Header.FromAddress, mailMessage.Header.FromName), To = new AddressCollection { new Address(mailMessage.Header.ToAddress, mailMessage.Header.ToName) }, Subject = mailMessage.Header.Subject, BodyText = new MimeBody(BodyFormat.Text) { Text = mailMessage.BodyText } }; mailMessage.Attachments.ForEach( p => { if (p.ContentBytes != null) { imapMail.Attachments.Add(( byte[] )p.ContentBytes, p.FileName); } else { imapMail.Attachments.Add(Encoding.Default.GetBytes(( string )p.ContentString), p.FileName); } }); mailBox.Append(imapMail); // Do not use messageToAdd.Append(mailBox), it does not work } finally { if (imap4Client != null && imap4Client.IsConnected) { imap4Client.Disconnect(); } } }
private TResult DoImapWork <TResult>(Func <Imap4Client, TResult> workToDo) { Imap4Client client = null; try { client = _imapClientProvider.GetImapClient(); return(workToDo(client)); } finally { if (client != null && client.IsConnected) { client.Disconnect(); } } }