Link to home
Start Free TrialLog in
Avatar of sudipta_
sudipta_

asked on

Parsing e-mail received using POP3 protocol (vb.net)

hi,
I am using the tcpclient object to receive mail using POP3 in VB.net. I save the mail contents in a text file. I want to know if there is a way to retrieve the various headers( To, From, Subject, etc) and decode attatchments if any & retrieve embedded images. Is there any inbuilt support for receiving mail in .net?  or is parsing the mail line by line the only way out?

thanks in advance,
sudipta

ps.
this is what a mail i retrieved looks like:

+OK 2259 octets

Received: from webtech15 [203.199.141.93] by webtechdevelopers.com with ESMTP

  (SMTPD32-6.06) id AA0E20015E; Mon, 24 Mar 2003 00:39:26 -0500

Message-ID: <000a01c2f1c6$76b49540$5d8dc7cb@web1.com>

Reply-To: "Sudipta Bhowmik" <sudipta@webtechdevelopers.com>

From: "Sudipta Bhowmik" <sudipta@webtechdevelopers.com>

To: "sudipta" <sudipta@webtechdevelopers.com>

Subject: test

Date: Mon, 24 Mar 2003 11:00:09 +0530

MIME-Version: 1.0

Content-Type: multipart/related;

     type="multipart/alternative";

     boundary="----=_NextPart_000_0006_01C2F1F4.89260640"

X-Priority: 3

X-MSMail-Priority: Normal

X-Mailer: Microsoft Outlook Express 6.00.2600.0000

X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

X-RCPT-TO: <sudipta@webtechdevelopers.com>

X-UIDL: 342029223

Status: U



This is a multi-part message in MIME format.



------=_NextPart_000_0006_01C2F1F4.89260640

Content-Type: multipart/alternative;

     boundary="----=_NextPart_001_0007_01C2F1F4.89260640"





------=_NextPart_001_0007_01C2F1F4.89260640

Content-Type: text/plain;

     charset="iso-8859-1"

Content-Transfer-Encoding: quoted-printable





testing



------=_NextPart_001_0007_01C2F1F4.89260640

Content-Type: text/html;

     charset="iso-8859-1"

Content-Transfer-Encoding: quoted-printable



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD>

<META http-equiv=3DContent-Type content=3D"text/html; =

charset=3Diso-8859-1">

<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>

<STYLE></STYLE>

</HEAD>

<BODY bgColor=3D#ffffff>

<DIV><FONT face=3DArial size=3D2><IMG alt=3D"" hspace=3D0=20

src=3D"cid:000501c2f1c6$6f67afc0$5d8dc7cb@web1.com" align=3Dbaseline=20

border=3D0></FONT></DIV>

<DIV><FONT face=3DArial size=3D2>testing</FONT></DIV></BODY></HTML>



------=_NextPart_001_0007_01C2F1F4.89260640--



------=_NextPart_000_0006_01C2F1F4.89260640

Content-Type: image/gif;

     name="bold.gif"

Content-Transfer-Encoding: base64

Content-ID: <000501c2f1c6$6f67afc0$5d8dc7cb@web1.com>



R0lGODlhFwAWAJEAAEpNSu/v3gAAAAAAACwAAAAAFwAWAAAIQgADCBxIsKDBgwgTKlzIsKHDhxAT

AphIcSJEigIxOtSosSFHixsrggwJIONIhh9LkjSpEqXIkxFjypxJs6bNmwECAgA7



------=_NextPart_000_0006_01C2F1F4.89260640--






Avatar of naveenkohli
naveenkohli

There is no builtin support for parsing POP3 mails. Check on w3c.org site. I think they have a project on building of various networking clients and POP3 is one of them. You can use that.
Avatar of sudipta_

ASKER

yes i figured that out after searching all day:) i did manage to parse out all the headers but now i'm facing another prob in decoding the attachments
basically i found some vb6 code for base64 decoding & converted it to vb.net but it just doesnt work!
i've realised where the prob is but cant solve it. it would be really nice if you could give me a routine for base64 decoding in VB.net or tell me where to find it.
thanks for your time,
sudipta
.Net has a class that does base64 encoding/decoding

Convert.FromBase64String
Convert.ToBase64String
i've written this function using the convert class & while it works perfectly for decoding jpeg's & bmp's, it fails if the
encoded string is of a .xml, or .doc or any non-image file. could you pls take a look at my code & tell me where i'm going wrong?

public function decodeattachment(strattname as string, str2decode as string)
        Dim bytarr As Byte()
        Dim i as integer
        Dim fstream As System.IO.FileStream
        Dim binwriter As System.IO.BinaryWriter

       'create a filestream object initialize with        filename of the attachment eg. sample.jpg
        fstream = New System.IO.FileStream("C:\" + strattname, IO.FileMode.Create)

       'create a binarywriter object & initialize with the file stream object
        binwriter = New System.IO.BinaryWriter(fstream)
       
       'put converted base64 string in a byte array
        bytIn = Convert.FromBase64String(str2Decode)

       'write byte by byte to the file
        For i = 0 To bytIn.GetUpperBound(0) - 1
            binwriter.Write(bytIn(i))
        Next
       
        binwriter.Close()
        fstream.Close()
End Function

thanks a lot
sudipta
I've been scouring the Internet for weeks for an answer to this same problem, and the best answer I've found so far is here: http://www.quiksoft.com/emdotnet/parse/ 

Unfortunately it's $499. We haven't bought it yet but are planning to in the next week or so.
too bad u have to shell out that much, i'll have to lease my place to buy that component so i have to do it the hard way, anyways, i've found out whats wrong with my decodeattachment function--- the last byte simply wasnt getting written!

the for loop should read-
  For i = 0 To bytIn.GetUpperBound(0)                 binwriter.Write(bytIn(i))
       Next
it seems that it desnt matter if the last byte in an image file is present or not, in case text documents, it has to be present thats the reason why image files were getting displayed but others were not.
i have another doubt, since i'm using the tcpclient object & the POP3 protocol to issue mail commands, i am able to retrieve mails only sequentially
 i.e say the stat command returns count of new mails as 3, i issue RETR 1 to fetch the 1st mail but cannot issue RETR 2 unless RETR 1 returns. this makes the process very slow. i can issue the RETR in a thread but since i have only one connection open, i cannot do so.
is there another way of doing this?
Avatar of Bob Learned
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/Refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Lunchy
Lunchy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial