Email mit POP3 abrufen

  • VB.NET

Es gibt 11 Antworten in diesem Thema. Der letzte Beitrag () ist von Leseratte.

    Email mit POP3 abrufen

    Hallo, ich möchte ein eigenes E-Mail Programm schreiben, und E-Mails kann ich damit auch schon verschicken, nur bekomme ichs nicht hin, die E-Mails von einem POP3-Server abzuholen, und auf Wunsch danach dort zu löschen. Mein Code:

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.Net
    3. Imports System.Net.Sockets
    4. Imports System.Text
    5. Imports System.io
    6. Imports System.Threading
    7. Public Class Form1
    8. Inherits System.Windows.Forms.Form
    9. Public stream As NetworkStream
    10. Public sr As StreamReader Private Sub cmdPOP3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPOP3.Click
    11. Dim server As String = "pop.googlemail.com"
    12. Dim user As String = "*****"
    13. Dim password As String = "*****"
    14. Dim message As String
    15. Dim send As String
    16. Dim client As New TcpClient(server, 995)
    17. stream = client.GetStream()
    18. sr = New StreamReader(stream)Dim txtResponse As String
    19. txtResponse += sendCommand("?")
    20. txtResponse += sendCommand("USER " + user)
    21. txtResponse += sendCommand("PASS " + password)
    22. txtResponse += sendCommand("STAT" + vbCrLf)
    23. txtResponse += sendCommand("RETR 1" + vbCrLf) 'hole erstes Mail
    24. txtResponse += sendCommand("QUIT" + vbCrLf)MsgBox(txtResponse) 'Die ist leer
    25. End Sub
    26. Private Function sendCommand(ByVal send As String) As String
    27. send += vbCrLf
    28. Dim data As [Byte]() = Encoding.ASCII.GetBytes(send)
    29. stream.Write(data, 0, data.Length)
    30. sr = New StreamReader(stream)
    31. Dim buffer As New StringBuilder
    32. Do While sr.Peek() > 0
    33. Thread.Sleep(100)
    34. buffer.Append(sr.ReadLine + vbCrLf)
    35. Loop
    36. Return buffer.ToString
    37. End Function
    38. End Class

    Allerdings ist die Messagebox, die eig. die Mail anzeigen soll, leer, und im Einzelschritt wird diese Schleife

    VB.NET-Quellcode

    1. Do While sr.Peek() > 0
    2. Thread.Sleep(100)
    3. buffer.Append(sr.ReadLine + vbCrLf)
    4. Loop
    Nicht ausgeführt.

    Auch mit anderen Codes von Google bekomme ich Fehler.

    Was ist an meinem Code falsch?

    Vielen Dank!

    Leseratte

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Leseratte“ () aus folgendem Grund: Tippfehler

    Hab ich ausprobiert, leider tut sich bei dem Code auch nach 10 minuten nix, das Programm hängt und lässt sich nur noch über die IDE beenden.

    EDIT: Hab das [VB 2008] Email Empfang über POP3 mal versucht, nach 2-3 Minuten kommt aber
    Spoiler anzeigen
    System.NullReferenceException wurde nicht behandelt.
    Message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
    Source=OpenPOP
    StackTrace:
    bei OpenPOP.POP3.POPClient.IsOkResponse(String strResponse)
    bei OpenPOP.POP3.POPClient.Connect(String strHost, Int32 intPort)
    bei ***.Form1.cmdPOP_Click(Object sender, EventArgs e) in C:\Users\***\documents\visual studio 2010\Projects\***\***\Form1.vb:Zeile 54.
    bei System.Windows.Forms.Control.OnClick(EventArgs e)
    bei System.Windows.Forms.Button.OnClick(EventArgs e)
    bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    bei System.Windows.Forms.Control.WndProc(Message& m)
    bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
    bei System.Windows.Forms.Button.WndProc(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    bei TrackNTrace.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
    bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    bei System.Threading.ThreadHelper.ThreadStart()
    InnerException:

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Leseratte“ ()

    VB.NET-Quellcode

    1. Dim popServer As String = "pop.gmail.com"
    2. Dim port As Integer = 995
    3. Dim useSSL As Boolean = True
    4. _popClient = New POPClient()
    5. _popClient.Connect(popServer, port, useSSL)
    6. _popClient.Authenticate("username", "password")

    Hab das dazu gefunden.