After I retrieve messages from mail box I want to separate message body from subject, date and other information. But I can't find wright algorithm. Here is my code:
// create an instance of TcpClient
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER and gmail uses port number 995 for POP
tcpclient.Connect("pop.gmail.com", 995);
// This is Secure Stream // opened the connection between client and POP Server
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
// authenticate as client
sslstream.AuthenticateAsClient("pop.gmail.com");
//bool flag = sslstream.IsAuthenticated; // check flag
// Asssigned the writer to stream
System.IO.StreamWriter sw = new StreamWriter(sslstream);
// Assigned reader to stream
System.IO.StreamReader reader = new StreamReader(sslstream);
// refer POP rfc command, there very few around 6-9 command
sw.WriteLine("USER my_login");
// sent to server
sw.Flush();
sw.WriteLine("PASS my_pass");
sw.Flush();
// this will retrive your first email
sw.WriteLine("RETR 1");
sw.Flush();
string str = string.Empty;
string strTemp = string.Empty;
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
// close the connection
sw.WriteLine("Quit ");
sw.Flush();
richTextBox2.Text = str;
I have to extract:
- The subject of message
- The author
- The date
- The message body
Can anyone tell me how to do this?
String which I receive (str) contains the subject Test message and the body This is the text of test message. It looks like:
+OK Gpop ready for requests from 46.55.3.85 s42mb37199022eev+OK send PASS+OK Welcome.+OK message followsReturn-Path: Received: from TMD-I31S3H51L29 (host-static-46-55-3-85.moldtelecom.md. [46.55.3.85]) by mx.google.com with ESMTPSA id o5sm61119999eeg.8.2014.04.16.13.48.20
for (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 16 Apr 2014 13:48:21 -0700 (PDT)Message-ID: <[email protected]>MIME-Version: 1.0From: [email protected]: [email protected]: Wed, 16 Apr 2014 13:48:21 -0700 (PDT)Subject: Test messageContent-Type: text/plain; charset=us-asciiContent-Transfer-Encoding: quoted-printableThis is the text of test message
Thank you very much!
String.Splitin your code. The crucial question is: How does the string look, that you want to analyse? - Olivier Jacot-Descombes.ReadLine()will read the entire line, sostrTemp == "."will only be true if"."is the only thing on the line. Can you do areader.ReadToEnd()for now and post some example data? - zmarks22strTempis the current line of text, you can look forstrTemp.IndexOf("Subject")(or whatever it's formatted to be) within thewhile()loop. Of course, we'll need to know whether theSubjectline is on its own line or nestled along with the subject header. - OnlineCopSystem.Diagnostics.Debug.WriteLine()to your code or use the debugger or write it to a file or whatever. Nobody can tell you how to split an unknown string. - Olivier Jacot-Descombes