从我的服务器读取电子邮件并在vb.net中显示它们 [英] Reading emails from my server and displaying them in vb.net

查看:83
本文介绍了从我的服务器读取电子邮件并在vb.net中显示它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作笔记应用程序,并且每个注册的用户都会被分配一个电子邮件地址。我希望他们能够发送电子邮件,并让vb.net能够阅读它。到目前为止,我已经设法阅读了主题行和电子邮件的第一行,但我遇到了一些问题。这是我目前正在使用的代码。它有点乱,但它的工作原理。



  Dim 服务器作为  Utilities.FTP.FTPclient( < span class =code-string> ftp://scopeproductivity.com/mail/scopeproductivity.com/,  username 密码

Dim ReadBox As TextBox
对于 每个注意作为 字符串 Server.ListDirectory( / testuser& / new /
如果 Note.StartsWith( 然后
ReadBox.Text = ReadFTP( ftp://scopeproductivity.com/mail/scopeproductivity.com / testuser / new /&注)
Dim AddStr As String = 字符串 .Empty
对于 每个 t 作为 字符串 ReadBox.Lines
如果 t.ToLower.StartsWith( subject:然后

AddStr =(t.Remove( 0 8 ))

ElseIf t.ToLower .StartsWith( note:然后

添加Str + =(t.Remove( 0 6 ))

< span class =code-keyword>结束 如果
下一步

ListBox1.Items.Add(AddStr)

结束 如果
下一步



此代码将读取存储在目录中的电子邮件FTP服务器。这是我用来从服务器读取电子邮件的代码。



 公开 功能 ReadFTP(url  As   String 作为 字符串 

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

ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
ftpReq.Credentials = NetworkCredential( 用户名 passwo rd
ftpReq.UsePassive = False
Dim ftpResp 作为 FtpWebResponse = ftpReq.GetResponse
Dim ftpRespStream 正如 Stream = ftpResp.GetResponseStream

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

返回 reader.ReadToEnd

reader.Close()

结束 功能





这是从服务器返回的内容。



返回路径:< sendersemail@mail.com> 
Envelope-to:testuser@scopeproductivity.com
交货日期:2013年9月23日星期一14:47:03 -0600
收到:来自[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;星期一,2013年9月23日14:47:02 -0600
收到:邮件:mail-lb0-f169.google.com,SMTP id为z5so3073205lbh.14
for< testuser@scopeproductivity.com> ;;星期一,2013年9月23日13:47:00 -0700(PDT)
DKIM-Signature:v = 1; A = RSA-SHA256; C =轻松/放松;
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,SMTP id mv5mr21220385lbb.20.1379969220433;
星期一,2013年9月23日13:47:00 -0700(PDT)
收到:10.112.57.8 with HTTP;星期一,2013年9月23日13:47:00 -0700(PDT)
收到:10.112.57.8 with HTTP;星期一,2013年9月23日13:47:00 -0700(PDT)
日期:星期一,2013年9月23日21:47:00 +0100
消息ID:< CAJb-9ebbDhw = ZiGDoB8YdbHdSV1UNPUPcUOsj7ELKecJRToQuw @ mail .gmail.com>
主题:科学笔记
来自:发件人姓名< sendersemail@mail.com>
To:testuser@scopeproductivity.com
内容类型:multipart / alternative; boundary = 001a11c373e42f9d6c04e7131fda

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

注:明天要完成的作业。查看猫头鹰的生命周期。允许$​​ b $ b使用互联网进行研究(不复制和粘贴)

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

< p dir =ltr>注意:明天要完成的作业。查看猫头鹰的生命周期。允许使用互联网进行研究(不复制和粘贴)< / p>

--001a11c373e42f9d6c04e7131fda -





每封电子邮件的格式相同。我需要一种更好的方法来提取返回的信息。我需要得到主题和说明它是自己的。如果有一种提取和图像和/或附件的方法,这将是一个额外的奖励。



我用这个例子的假信息替换了一些敏感信息。



感谢您的帮助!



这是我的最终代码对我有用:

 ' 将原始消息字符串转换为流并从SharpMimeTools创建SharpMessage实例库 
Dim messageBytes = Encoding.ASCII.GetBytes(ReadBox.Text)
Dim messageStream = MemoryStream(messageBytes)
' Dim message = New SharpMessage(messageStream)

Dim 消息作为 新 OpenPop.Mime.Message(messageBytes)

' 获取已解码的消息和主题

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

Dim 部分作为 字符串()= Body.Split( 字符串(){Environment.NewLine},
StringSplitOptions.None)

For 每个 As String 部分

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

Next

解决方案

< blockquote>Extract可能意味着解析原始邮件,它实际上是一个带有 MIME mail parts 的纯文本,以不同的方式编码,通常是base64(如果有的话)。这个开源实现可以帮助您:

http://hpop.sourceforge.net/ [< a href =http://hpop.sourceforge.net/target =_ blanktitle =New Window> ^ ]。



它不仅可以用作POP3客户端,还可以解析原始邮件。



-SA

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:

' 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

解决方案

"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


这篇关于从我的服务器读取电子邮件并在vb.net中显示它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆