Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Download email from Pop3 server
Author:
Nilesh Vankani
E-mail:
Click to e-mail author
Submitted:
6/20/2005
Version:
VB.NET 2003
Compatibility:
VB.NET 2003, VB.NET 2005
Category:
Internet Programming
Views:
110551
I'm a beginner to vb.net, i wrote the code for downloading email from POP3 server but i cant find single code in any of site...So i make my self and i want to give you this code without any charge....coz i know so many programmers are not able to buy component. With this code you download emails from pop3 server. It's very easy to understand.......IF you think you want any change change and if that change is good so pls send me mail of updating copy.
Declarations:
Imports System Imports System.Net.Sockets Imports System.Text Imports System.IO
Code:
Module POP3Message1 Dim Server As TcpClient Dim NetStrm As NetworkStream Dim RdStrm As StreamReader Public Function connect() As Integer Dim POP3Account As String POP3Account = "mail.mavensystems.biz" If POP3Account.Trim = "" Then Exit Function Try Server = New TcpClient(POP3Account.Trim, 110) NetStrm = Server.GetStream RdStrm = New StreamReader(Server.GetStream) Catch exc As Exception MsgBox(exc.Message) Exit Function End Try Dim user As String user = "test@mavensystems.biz" Dim data As String = "USER " + user.Trim + vbCrLf Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray()) NetStrm.Write(szData, 0, szData.Length) Dim POPResponse As String POPResponse = RdStrm.ReadLine If POPResponse.Substring(0, 4) = "-ERR" Then MsgBox("Invalid user Name") Return -1 End If Dim password As String password = "test1234" data = "PASS " & password & vbCrLf szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray()) NetStrm.Write(szData, 0, szData.Length) POPResponse = RdStrm.ReadLine If POPResponse.Substring(0, 4) = "-ERR" Then MsgBox("Invalid Passwprd") Return (-1) End If data = "STAT" + vbCrLf szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray()) NetStrm.Write(szData, 0, szData.Length) POPResponse = RdStrm.ReadLine If POPResponse.Substring(0, 4) = "-ERR" Then MsgBox("could not log your in") Return -1 End If data = "Stat" + vbCrLf szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray()) NetStrm.Write(szData, 0, szData.Length) POPResponse = RdStrm.ReadLine If POPResponse.Substring(0, 4) = "-ERR" Then MsgBox("could not log your in") Return -1 End If Dim parts() As String parts = POPResponse.Split(" ") Dim messages, totsize As Integer 'messages = parts(3) messages = CInt(parts(1)) Return messages End Function Public Function DeleteMessage(ByVal msgIndex As Integer) Dim data As String = "DELE " & msgIndex.ToString & vbCrLf Dim SzData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray()) NetStrm.Write(SzData, 0, SzData.Length) Dim tmpString As String = RdStrm.ReadLine() If tmpString.Substring(0, 4) = "-ERR" Then MsgBox("Could Not Delete Message") Return (-1) Else Return 11 End If End Function Public Function Quit() Dim data As String = "Quit " & vbCrLf Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray()) NetStrm.Write(szData, 0, szData.Length) Dim tmpString As String = RdStrm.ReadLine() End Function Public Structure Message Dim _From As String Dim _To As String Dim _Date As String Dim _Subject As String Dim _CC As String Dim _BCC As String Dim _Body As String Dim _Received As String End Structure Public Function CreateFromText(ByVal strMessage As String) As Message Dim Mssg As New Message Dim brkPos As Integer Dim Header As String Dim Headers() As String Dim Body As String Dim vField As Object Dim strHeader As String Dim HeaderName As String Dim HeaderValue As String brkPos = InStr(1, strMessage, vbCrLf & vbCrLf) If brkPos Then Header = strMessage.Substring(0, brkPos - 1) Body = strMessage.Substring(brkPos + 1, _ strMessage.Length - Header.Length - 3) Mssg._Body = Body Else Throw New Exception("Invalid Message Format") Exit Function End If Headers = Split(Header, vbCrLf) Dim _header As String For Each _header In Headers brkPos = _header.IndexOf(":") If brkPos >= 0 Then HeaderName = _header.Substring(0, brkPos) Else HeaderName = "" End If HeaderValue = _header.Substring(brkPos + 1) Select Case HeaderName.ToLower Case "received" Mssg._Received = HeaderValue Case "from" Mssg._From = HeaderValue Case "to" Mssg._To = HeaderValue Case "cc" Mssg._CC = HeaderValue Case "bcc" Mssg._BCC = HeaderValue Case "subject" Mssg._Subject = HeaderValue Case "date" Mssg._Date = HeaderValue End Select Next Return Mssg End Function Function GetMessage(ByVal msgindex As Integer) As String Dim tmpString As String Dim Data As String Dim SzData() As Byte Dim msg As String Try Data = "RETR " & msgindex.ToString & vbCrLf SzData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()) NetStrm.Write(SzData, 0, SzData.Length) tmpString = RdStrm.ReadLine() If tmpString.Substring(0, 4) <> "-ERR" Then While (tmpString <> ".") msg = msg & tmpString & vbCrLf tmpString = RdStrm.ReadLine End While End If Catch exc As InvalidOperationException MsgBox("Message Retrival Failed: " & vbCrLf & Err.ToString()) End Try Return msg End Function End Module 'Add new module in your project and add this code in to your module only copy and paste no need to do any of changes.... '---------------------------------------------------------------------------------- YOu add new form in your project gave name Frminbox and copy and paste this code. Imports System Imports System.Net.Sockets Imports System.Text Imports System.IO Public Class FrmInbox Inherits System.Windows.Forms.Form Dim Server As TcpClient Dim NetStrm As NetworkStream Dim RdStrm As StreamReader Private Sub CmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDownload.Click Dim i As Integer Dim msg As POP3Message1.Message Dim msgstring As String Dim messages1 As Integer Me.Cursor = Cursors.WaitCursor messages1 = connect() If messages1 = -1 Then Me.Cursor = Cursors.Default Exit Sub End If Dim originalCaption As String = Me.Text For i = 1 To messages1 Me.Text = "Downloading Message " & i.ToString & "/" & _ messages1.ToString Dim msgitem As New ListViewItem msgstring = GetMessage(i) msg = CreateFromText(msgstring) msgitem.Text = msg._From msgitem.SubItems.Add(msg._Subject) msgitem.SubItems.Add(msg._Date) ListView1.Items.Add(msgitem) TextBox1.AppendText(msg._Body & vbCrLf) Next Me.Text = originalCaption Me.Cursor = Cursors.Default End Sub Private Sub ListView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim messages As Integer 'Dim objPOP3 As POP3Message TextBox1.Text = GetMessage(ListView1.SelectedIndices(0) + 1) End Sub Private Sub ListView1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyUp If e.KeyCode = Keys.Delete Then If DeleteMessage(ListView1.SelectedIndices(0) + 1) >= 0 Then ListView1.Items(ListView1.SelectedIndices(0)).Text = "DELETED" ListView1.Items(ListView1.SelectedIndices(0)).SubItems.Clear() MsgBox("Your Email Message is Deleted", MsgBoxStyle.Information, "Delete Message") End If End If End Sub Private Sub CmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdClose.Click Call Quit() ListView1.Items.Clear() End Sub End Class ' this is the my first submission if you find any mistake or you want any change so you change but dont forget to send a copy to me on this email id n_vankani@yahoo.com
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2023 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement