Click here to Skip to main content
15,907,000 members
Please Sign up or sign in to vote.
3.40/5 (5 votes)
See more:
Hi every one.

I am trying to retrieve email by using C#. I got every thing I required. But the problem is the message content is coming like..

--0016367fb7376bfdc30499a12567
Content-Type: text/plain; charset=ISO-8859-1

Your Text message.

--0016367fb7376bfdc30499a12567
Content-Type: text/html; charset=ISO-8859-1


I need to take only the content
Your Text message.

Can any one please help me out by giving the logic, how can I retrieve it.
The condition is

MIDL
--0016367fb7376bfdc30499a12567
Content-Type: text/plain; charset=ISO-8859-1


can be different for different mails but the same will be twice in that particular order.

Thanks in advance.
Posted
Updated 12-Jan-11 0:08am
v2

Add a "FIXED" code before and after your main message. for example:

"A0AKKmasaieTTYYIOIOIMddssPP...--++yyyYYYY"

Your message will be:

CSS
--0016367fb7376bfdc30499a12567Content-Type: text/plain; charset=ISO-8859-1


A0AKKmasaieTTYYIOIOIMddssPP...--++yyyYYYYYour Text messageA0AKKmasaieTTYYIOIOIMddssPP...--++yyyYYYY


.--0016367fb7376bfdc30499a12567Content-Type: text/html; charset=ISO-8859-1


Now you have start and end point of your string and it will be simple to delete wastes.
 
Share this answer
 
v3
Comments
arindamrudra 12-Jan-11 7:11am    
Great Idea. Thanks a lot.
Shahin Khorshidnia 12-Jan-11 9:58am    
Your're welcome Arindamrudra ;)
fjdiewornncalwe 12-Jan-11 10:55am    
And how does the OP get all the users creating emails to append this text before and after their message? This is utterly ridiculous.
Shahin Khorshidnia 12-Jan-11 16:01pm    
For appending that code there are various approaches. It depends on your client-side language. Like JavaScript.
Of course asp.net can also do it. Append the code to the text after Clicking on Send button. It's not hard.
Why do you think that this is utterly ridiculous? I tried it and It "did" meet my needs. Thank you for your attention.
I wrote your necessary method too :

C#
private string FixString(string message)
{
    string[] fixedCode =new string[1] {"A0AKKmasaieTTYYIOIOIMddssPP...--++yyyYYYY"};

    StringSplitOptions option = StringSplitOptions.None;
    string[] messages = message.Split(fixedCode, option);
    for (int i = 0; i < messages.Length;i++ )
    {
        if (i == 1)
            return messages[i];
    }

    return string.Empty;

}


Now, you can find your main message:

MSIL
string k = @"--0016367fb7376bfdc30499a12567Content-Type: text/plain; charset=ISO-8859-1
Your Text message.A0AKKmasaieTTYYIOIOIMddssPP...--++yyyYYYYHelloA0AKKmasaieTTYYIOIOIMddssPP...--++yyyYYYY--0016367fb7376bfdc30499a12567Content-Type: text/plain; charset=ISO-8859-1
Your Text message.";
           
Console.WriteLine(FixString(k));
 
Share this answer
 
v2
Comments
arindamrudra 12-Jan-11 7:44am    
Thanks a lot.
Although this seems solved:

You can simply take the first two lines and search for the next occurrence. The message will be in between.

--0016367fb7376bfdc30499a12567
Content-Type: text/plain; charset=ISO-8859-1


<big>Your Text message.</big>

--0016367fb7376bfdc30499a12567
Content-Type: text/html; charset=ISO-8859-1




Alternatively:
You can also start by looking for the first occurrence of: "Content-Type:". Then you take the line before that and that line and again search for the next occurrence. The message will, again also, be in between.

Good luck!
 
Share this answer
 
Comments
arindamrudra 12-Jan-11 23:33pm    
Thanks a lot.
If the format remain the same, you can split this string over Environment.NewLine and take the third item in the array.
 
Share this answer
 
Comments
arindamrudra 12-Jan-11 6:18am    
Thanks for the quick response. I am trying with that.
Dalek Dave 12-Jan-11 6:19am    
Good Answer.
arindamrudra 12-Jan-11 6:28am    
The message may contain two or more \n\r. If the message is multi-line, I think then it will rise a problem.
fjdiewornncalwe 12-Jan-11 10:57am    
This is much better than the fixed string suggestion from Shahin. Just go through the lines of the mail as though they were a Stream and the message would begin with the first line containing text.
Actually this is just because of Mailing protocols (IMAP, POP,..) for specify if the mail is in HTML form or in IMAGE or PLANE TEXT.

I guess you are using

System.Net.Mail.MailMessage msg;
string strMsg = msg.ToString();



if so, Don't do that, you should use:-


System.Net.Mail.MailMessage msg;
string strMsg = msg.Body;


msg.Body not contains

MIDL
--0016367fb7376bfdc30499a12567
Content-Type: text/plain; charset=ISO-8859-1
 
Share this answer
 
v3
Comments
arindamrudra 12-Jan-11 23:30pm    
I am using http://www.codeproject.com/KB/IP/popapp.aspx.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900