jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to edit a message and Save it on the IMAP?

xx7Ahmed7xx opened this issue · comments

Since morning I have been trying to figure this out using the documentation, code, BardAI and all but nothing.
I have come this far:

client.Connect("server", 993, true);
client.Authenticate("email", "password");
client.Inbox.Open(FolderAccess.ReadWrite);
var message = client.Inbox.GetMessage(UniqueId_Here);
var part = message.BodyParts.OfType<TextPart>().First();
var oldText = part.Text;
part.Text = part.Text.Replace(oldText, "New Body text here Please");
message.BodyParts.OfType<TextPart>().First().Text = part.Text;

How to save the new body for same message on the IMAP server

Here is how to edit Body of subject of the message:

var message = client.Inbox.GetMessage(MessageUId);
ReplaceTextPart(message.Body, "New message body here");

And this is ReplaceTextPart method:

void ReplaceTextPart(MimeEntity entity, string newText)
{
    if (entity is Multipart multipart)
    {
        foreach (var part in multipart)
        {
            ReplaceTextPart(part, newText);
        }
    }
    else if (entity is TextPart textPart)
    {
        textPart.Text = newText;
    }
}