Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a notes application and each user that signs up is assigned a email address. I want them to be able to send an email and for vb.net to be able to read it. So far I've managed to read the subject line and the first line of the email it's self but I'm running into some problems. This is the code I'm using at the moment. It's a bit messy but it works.

VB
Dim Server As New Utilities.FTP.FTPclient("ftp://scopeproductivity.com/mail/scopeproductivity.com/", "username", "password")

        Dim ReadBox As New TextBox
        For Each Note As String In Server.ListDirectory("/testuser" & "/new/")
            If Not Note.StartsWith(".") Then
                ReadBox.Text = ReadFTP("ftp://scopeproductivity.com/mail/scopeproductivity.com/testuser/new/" & Note)
                Dim AddStr As String = String.Empty
                For Each t As String In ReadBox.Lines
                    If t.ToLower.StartsWith("subject: ") Then

                        AddStr = (t.Remove(0, 8))

                    ElseIf t.ToLower.StartsWith("note: ") Then

                        AddStr += (t.Remove(0, 6))

                    End If
                Next

                ListBox1.Items.Add(AddStr)

            End If
        Next

This code will read the emails that are stored in a directory on the FTP server. This is the code I'm using to read the email from the server.

VB
Public Function ReadFTP(url As String) As String

       Dim filename As String = url
       Dim ftpReq As FtpWebRequest = CType(WebRequest.Create(filename), FtpWebRequest)

       ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
       ftpReq.Credentials = New NetworkCredential("username", "password")
       ftpReq.UsePassive = False
       Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
       Dim ftpRespStream As Stream = ftpResp.GetResponseStream

       Dim reader As StreamReader
       reader = New StreamReader(ftpRespStream, System.Text.Encoding.UTF8)

       Return reader.ReadToEnd

       reader.Close()

   End Function


This is what is being return from the server.

Return-path: <sendersemail@mail.com>
Envelope-to: testuser@scopeproductivity.com
Delivery-date: Mon, 23 Sep 2013 14:47:03 -0600
Received: from [209.85.217.169] (port=54099 helo=mail-lb0-f169.google.com)
	by just25.justhost.com with esmtps (TLSv1:RC4-SHA:128)
	(Exim 4.80)
	(envelope-from <sendersemail@mail.com>)
	id 1VOD26-0007jj-Nn
	for testuser@scopeproductivity.com; Mon, 23 Sep 2013 14:47:02 -0600
Received: by mail-lb0-f169.google.com with SMTP id z5so3073205lbh.14
        for <testuser@scopeproductivity.com>; Mon, 23 Sep 2013 13:47:00 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:date:message-id:subject:from:to:content-type;
        bh=GqVtqOM/cGYFMPA8UMvJniBEl/b716v83U3uhoPGd88=;
        b=BFTXxp/aOb9mOBlM61vgpq68E8eJ+6NMvbD59m91pNEHlMCHyYrwLmPnZSJbhRaQ1H
         5FQStXu7j5ZAMGafvbNbpUgJZs7HslZb5ED/xVXWQlr7oLvjUQ5DpZtva6OboE6/FFrf
         h3OQfs9IrAgfZSW3YehJmTE2JTiiAJ3zAIiwA3I8LuvfZjHbUtS1UEr6Rk+tS+oNNlYk
         /GfMXLZN6U7Op2lH7QBTQ3VWki7FfmSpAcqysnqarhf/ceSLnp8PSxa62DygmY02ffX5
         NETMnRMy8O6CO2vv+AWjvvUQlsr1IY1Bbw1SPUBDFH86l5QWvmjqyO/YJT6C1DJloddD
         zziA==
MIME-Version: 1.0
X-Received: by 10.112.126.37 with SMTP id mv5mr21220385lbb.20.1379969220433;
 Mon, 23 Sep 2013 13:47:00 -0700 (PDT)
Received: by 10.112.57.8 with HTTP; Mon, 23 Sep 2013 13:47:00 -0700 (PDT)
Received: by 10.112.57.8 with HTTP; Mon, 23 Sep 2013 13:47:00 -0700 (PDT)
Date: Mon, 23 Sep 2013 21:47:00 +0100
Message-ID: <CAJb-9ebbDhw=ZiGDoB8YdbHdSV1UNPUPcUOsj7ELKecJRToQuw@mail.gmail.com>
Subject: Science notes
From: Sender Name <sendersemail@mail.com>
To: testuser@scopeproductivity.com
Content-Type: multipart/alternative; boundary=001a11c373e42f9d6c04e7131fda

--001a11c373e42f9d6c04e7131fda
Content-Type: text/plain; charset=ISO-8859-1

Note: Homework due in tomorrow. Look up the life cycle of owls. Allowed to
use the internet to research (no copying and pasting)

--001a11c373e42f9d6c04e7131fda
Content-Type: text/html; charset=ISO-8859-1

<p dir="ltr">Note: Homework due in tomorrow. Look up the life cycle of owls. Allowed to use the internet to research (no copying and pasting)</p>

--001a11c373e42f9d6c04e7131fda--


Each email has the same format. I need a better way to extract the information being returned. I need to get the subject and the note it's self. If there's a way to extract and images and/or attachments that would be a bonus.

I have replaced some of the sensitive info with fake info for this example.

Thanks for any help!

This was my final code that worked for me:
VB
' Covert raw message string into stream and create instance of SharpMessage from SharpMimeTools library
                  Dim messageBytes = Encoding.ASCII.GetBytes(ReadBox.Text)
                  Dim messageStream = New MemoryStream(messageBytes)
                  'Dim message = New SharpMessage(messageStream)

                  Dim message As New OpenPop.Mime.Message(messageBytes)

                  ' Get decoded message and subject

                  NewMessage.Label1.Text = message.Headers.Subject.ToString
                  NewMessage.Label2.Text = String.Empty
                  Dim Body As String = message.MessagePart.MessageParts(0).GetBodyAsText()

                  Dim parts As String() = Body.Split(New String() {Environment.NewLine},
                                      StringSplitOptions.None)

                  For Each item As String In parts

                      NewMessage.Label2.Text = NewMessage.Label2.Text & item

                  Next
Posted
Updated 25-Sep-13 23:59pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Sep-13 17:32pm    
Why FTP? How come your mail is accessible via FTP (is it, really?)?
—SA
Kieran Crown 23-Sep-13 18:43pm    
Yeah it is, that's just the way JustHost do it I suppose, none the less, makes it very easy to access as long as you have a FTP login

1 solution

"Extract" probably mean parsing of raw mail, which is actually a plain text with MIME mail parts, encoded in different ways, usually base64 (if any). This open-source implementation may help you:
http://hpop.sourceforge.net/[^].

Not only it can be used as a POP3 client, but also to parse raw mail.

—SA
 
Share this answer
 
Comments
Kieran Crown 23-Sep-13 22:10pm    
Thank you very much. I'll check it out and let you know!

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