parsing - Parse IMAP message and extract header information -
i trying extract header , body information email, following code retrieves header , body in raw form. have email object contains fields from
, subject
, date
, , body
. extract these values email , assign them email object. how around it? have tried several ways getting header info , using streamreader.readline()
line got illegal path exceptions
. know can use library need achieve way.
what mean this, imap command returns header information. , want extract subject
value, date
value, sender
e-amil, etc. , assign them email objects corresponding values like
emailobject.subject = "subjectvalue" public class imap { static void main(string[] args) { try { path = environment.currentdirectory + "\\emailresponse.txt"; if (system.io.file.exists(path)) system.io.file.delete(path); sw = new system.io.streamwriter(system.io.file.create(path)); tcpc = new system.net.sockets.tcpclient("imap.gmail.com", 993); ssl = new system.net.security.sslstream(tcpc.getstream()); ssl.authenticateasclient("imap.gmail.com"); receiveresponse(""); console.writeline("username : "); username = console.readline(); console.writeline("password : "); password = console.readline(); receiveresponse("$ login " + username + " " + password + " \r\n"); console.clear(); receiveresponse("$ list " + "\"\"" + " \"*\"" + "\r\n"); receiveresponse("$ select inbox\r\n"); receiveresponse("$ status inbox (messages)\r\n"); console.writeline("enter email number fetch :"); int number = int.parse(console.readline()); console.writeline("*************header************"); console.writeline(""); // receiveresponse("$ fetch " + number + " body[header]\r\n"); // body.peek[header.fields (subject)] // stringbuilder sb = receiveresponse("$ fetch " + number + " body.peek[header.fields (from subject date)]\r\n"); stringbuilder sb= receiveresponse("$ fetch " + number + " body.peek[header]\r\n"); console.writeline(sb); console.writeline(""); console.writeline("body"); sb = new stringbuilder(); sb=receiveresponse("$ fetch " + number + " body[text]\r\n"); system.text.asciiencoding enc = new system.text.asciiencoding(); byte[] serverbuff = new byte[1024]; int count = 0; string retval = enc.getstring(serverbuff, 0, count); console.writeline(sb.tostring()); receiveresponse("$ logout\r\n"); } catch (exception ex) { console.writeline("error: " + ex.message); } { if (sw != null) { sw.close(); sw.dispose(); } if (ssl != null) { ssl.close(); ssl.dispose(); } if (tcpc != null) { tcpc.close(); } } console.readkey(); } static stringbuilder receiveresponse(string command) { sb = new stringbuilder(); try { if (command != "") { if (tcpc.connected) { dummy = encoding.ascii.getbytes(command); ssl.write(dummy, 0, dummy.length); } else { throw new applicationexception("tcp connection disconnected"); } } ssl.flush(); buffer = new byte[2048]; bytes = ssl.read(buffer, 0, 2048); sb.append(encoding.ascii.getstring(buffer)); // console.writeline(sb.tostring()); sw.writeline(sb.tostring()); // sb = new stringbuilder(); return sb; } catch (exception ex) { throw new applicationexception(ex.message); } }
you said not want use imap library. means have implement own. should start reading rfc 3501 because there no chance protocol right without reading docs carefuly. in particular, you're issuing status
command on selected mailbox, explicitly forbidden protocol specification. rest of code supports assumption have not read rfc yet.
Comments
Post a Comment