This project is read-only.

Receive email with Plain text format

Jul 30, 2012 at 8:06 PM
Edited Jul 30, 2012 at 8:07 PM

Hello guys,

Before I made a discussion about Receive email with HTML format but now I have another problem.

I want to know how I can receive mails that are in plain text format.

I use this code now:

 

                            if (mg.IsHtml == true)
                            {
                                htmlText = mg.BodyText;
                            }
                            else if (mg.IsHtml != true)
                            {
                                foreach (var ct in mg.Contents)
                                {
                                    if (ct.IsHtml == true)
                                    {
                                        htmlText = ct.BodyText;
                                        break;
                                    }
                                }
                            }

When I use this code, I could not get Plain test mails...
Please help me about this topic :)
I'm so sorry about my bad English writing... :(
Jul 31, 2012 at 12:09 AM

You can get both Plain,Html text by

-----------------------------------------------------

String text = mg.BodyText;

-----------------------------------------------------

regards

 

Jul 31, 2012 at 12:43 AM

Thank you for your reply :)

Yes I know that I can do it but I don't know how.

May you write some sample code to me, please?

For example how we can know that our text is a Plain? Or how we can show or save a plain message?

Aug 1, 2012 at 10:43 AM

You can get it like this

-----------------------------------------------------

String text = "";
if (mg.IsHtml == true)
{
    text = mg.BodyText;
}
else
{
    text = mg.BodyText;
}
-----------------------------------------------------

Sometimes received mail is formated as MailContent.
You can get all text by this.

-----------------------------------------------------
foreach(var ct in mg.Contents)
{
    if (ct.IsAttachment == true) { continue; }
    if (ct.IsHtml == true)
    {
        htmlText = ct.BodyText;
    }
    else
    {
        plainText = ct.BodyText;
    }
}
-----------------------------------------------------

You must check IsHtml property whether message is HTML or not.

regards

Aug 1, 2012 at 3:26 PM

Yes this work correct but when I get plain text I see a string like this:

<span dir=3D"LTR" style=3D"font-size:9.5pt;=
"></span></div></td></tr></tbody></table></td></tr></tbody></table></div><d=
iv class=3D"yiv2128628118MsoNormal"><span style=3D"border:solid windowtext =
1.0pt;padding:0in;"><img border=3D"0" width=3D"100" height=3D"100" id=3D"yi=
v2128628118Picture_x0020_2" src=3D"cid:1.191431149@web28701.mail.ir2.yahoo.=
com" alt=3D"Description: Image removed by sender. =D8=AF=D8=B1=DA=AF=D8=A7=
=D9=87 =D9=BE=D8=B1=D8=AF=D8=A7=D8=AE=D8=AA =D8=A7=DB=8C=D9=86=D8=AA=D8=B1=
=D9=86=D8=AA=DB=8C =D8=A8=D8=A7=D9=86=DA=A9 =D9=85=D9=84=DB=8C =D8=A7=DB=8C=
=D8=B1=D8=A7=D9=86"></span>

I want to know how I can see correct.

What are these codes? Do I must encode it? how?
thank you for your replies dude :) 

Aug 1, 2012 at 5:07 PM

You can parse it by Regex or HtmlAgilityPack.
It is beyond mail protocol.

Here is a hint of your requirement by regular expression.
----------------------------------------------------------------
var mc = Regex.Matchs(mg.BodyText, "<[^>]>");
foreach(var m in mc)
{
    //do something...
}
----------------------------------------------------------------

regards