i'm using mimekit for receive and send mail for my project. I'm sending received mails with some modifications (to & from parts). And now i need to modify in body section. I'll replace specific word with asterix chars. Specific text different for every mail. Mail may be any format. You can see i found what i want but i don't know how can i replace without any error?
Asked
Active
Viewed 3,024 times
5
-
If the Text property is writable, I guess the only way to find out is to actually try modifying it. – Alex Jan 14 '16 at 13:07
-
yes Text property seems writable. But; MimeMessage mesaj; mesaj.body is not a kind of list type. So i cant access and modfiy mesaj.body[0].Text. I can't found the way of Text property. – Ercument Eskar Jan 14 '16 at 13:17
1 Answers
6
MimeMessage.Body
is a tree structure, like MIME, so you'll have to navigate to the MimePart
that contains the content that you want to modify.
In this case, since you want to modify a text/* MimePart
, it will actually be a subclass of MimePart
called TextPart
which is what has the .Text
property (which is writable).
I've written documentation on how to traverse the MIME structure of a message to find the part that you are looking for here: http://www.mimekit.org/docs/html/WorkingWithMessages.htm
A very simple solution might be:
var part = message.BodyParts.OfType<TextPart> ().FirstOrDefault ();
part.Text = part.Text.Replace ("x", "y");
But keep in mind that that logic assumes that the first text/* part you find is the one you are looking for.

jstedfast
- 35,744
- 5
- 97
- 110
-
Thanks but in first line: `System.Collections.GenericIEnumerable
does not contain a definition for 'OfType' and not extension method 'OfType' accepting a first argument of type System.Collections.GenericIEnumerable – Ercument Eskar Jan 15 '16 at 09:34could by found (are you missing a using directive or an assembly reference?)` I'm using MimeKit.1.2.0.0 -
That's a LINQ statement. You need to add `using System.Linq;` to your list of usings. – jstedfast Jan 15 '16 at 12:00
-
Ok, when i see mesaj.body in debug screen, it seems changed. But when i send mail, receiver take unchanged version. is it need a like rebuild or something? – Ercument Eskar Jan 15 '16 at 13:14
-
There's no rebuild. I don't know why you are seeing the behavior that you are seeing. I'd have to see your code. – jstedfast Jan 15 '16 at 16:09
-
The problem is that your message has 2 copies of the message body. One version in `text/plain` and another in `text/html`. You've only replaced the text in the `text/plain` version, but not the `text/html` version. You need to replace both. – jstedfast Jan 15 '16 at 17:37
-
Thank you very much ! It's working. Is BodyParts & Body different? Should i loop both of them? Or should i loop Multipart like TextPart? Final Code : foreach (var part in ((System.Collections.IEnumerable)mesaj.Body).OfType
()) { part.Text = part.Text.Replace("@", "[at]"); } – Ercument Eskar Jan 15 '16 at 20:48 -
1`BodyParts` is just a convenience property that iterates over the `Body` property in a list-like fashion instead of a tree. – jstedfast Jan 15 '16 at 20:50