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.
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.
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:
Dim messageBytes = Encoding.ASCII.GetBytes(ReadBox.Text)
Dim messageStream = New MemoryStream(messageBytes)
Dim message As New OpenPop.Mime.Message(messageBytes)
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