Hi All
I have a Form called "Form1" with a ListView called "Listview1".
Can anyone give me some code on how i can fill "Listview1" in a details view with my Inbox item from my Outlook inbox.
Thanks
Printable View
Hi All
I have a Form called "Form1" with a ListView called "Listview1".
Can anyone give me some code on how i can fill "Listview1" in a details view with my Inbox item from my Outlook inbox.
Thanks
Reading the data directly from your POP3 server would be easier, really )))
You'll need a TCPClient connected to your POP3 server through port 110 (default)
vb.net Code:
Private Sub POP3Demo(ByVal server As String, ByVal user As String, ByVal pass As String) Dim client As Net.Sockets.TcpClient Dim sr As IO.StreamReader Dim sw As IO.StreamWriter Dim ns As Net.Sockets.NetworkStream Dim response As String 'connecting to a server client = New Net.Sockets.TcpClient Try client.Connect(server, 110) Catch e As Exception MessageBox.Show("Failed to connect: " & e.ToString, "Error") Return End Try ns = client.GetStream sr = New IO.StreamReader(ns, Encoding.ASCII, True) ' this sends our commands to the server sw = New IO.StreamWriter(ns) ' and this will read server's response response = sr.ReadLine() ' reading the response ' At this point the response should contain a string +OK and some text after it. ' Also it sends CRLF at the end of each string ' We should send: ' user [username] & ControlChars.CrLf ' The server should respond: ' +OK [some text] - a user exists you may continue ' -ERR [some text] - either some error or user was not found sw.WriteLine("USER " & user & ControlChars.CrLf) sw.Flush() response = sr.ReadLine() If response.Substring(0, 3) = "-ER" Then MessageBox.Show("Could not login", "Error") Return End If ' We then should send a password ' And the response is also either +OK or -ERR ' Sending password sw.WriteLine("PASS " & pass & Chr(13) & Chr(10)) sw.Flush() response = sr.ReadLine If response.Substring(0, 3) = "-ER" Then MessageBox.Show("Password is incorrect", "Error") Return End If ' We logged in then we should get the number of messages and their size ' We should send 'STAT' ' The response would be: ' +OK [number] [size] Dim stats() As String Dim totalmessages, totalsize As Integer sw.WriteLine("STAT") sw.Flush() response = sr.ReadLine() stats = response.Split(" ") totalmessages = CInt(stats(1)) totalsize = CInt(stats(2)) ' Now we should determine messages indices and the size of each message ' Send: ' LIST ' Response (example): ' +OK 2 messages 320 ' 1 120 ' 2 200 ' The list is terminated by a new line and "." character Dim i As Integer Dim Messages() As String ' getting messages identifiers sw.WriteLine("LIST") sw.Flush() response = sr.ReadLine() stats = response.Split(" ") ReDim Messages(totalmessages - 1) For i = 0 To CInt(stats(1)) - 1 Messages(i) = (sr.ReadLine()) Next ' Now we can manipulate messages the following way ' To delete a message you should send DELE [x] where x is a message index (see above) ' The response is as usual either +OK [some text] (operation successful) ' or -ERR [some text] (error) ' This deletes message number 1 from the server (uncomment) ' sw.WriteLine("DELE 1") ' sw.Flush() ' response = sr.ReadLine() ' To retrieve (download) a message you should use RETR command ' Send (x is a message index): ' RETR [x] ' The message will be sent as plain text and will be terminated by a empty line and the "." character ' Again, possible answers are: +OK [some text] or -ERR [some text] ' For example: ' +OK 120 octets ' [message header] ' [message body] ' . ' Let's get all messages: sr = New IO.StreamReader(ns, Encoding.Default, True) For i = 0 To (totalmessages - 1) sw.WriteLine("RETR " & (i + 1)) sw.Flush() ' We just dump the messages to the screen: Do While response <> "." response = sr.ReadLine Console.WriteLine(response & Environment.NewLine) Loop Next ' Quit gracefully: sw.WriteLine("QUIT") ' Disconnecting ' Closing the client ' client.Close End Sub
Additional POP3 commands
Command:
TOP [message index] [n]
where:
[message index] - is a message index
[n] - non-negative number (required)
If the server response is non-negative it will then send the header of a required message and
n lines of the message body
Possible responses:
+OK top of message follows
-ERR no such message
Command:
UIDL [message index]
where:
[message index] - is a message index
If a message index was specified the server will send an unique identifier for this message.
If no argument was specified then the identifiers will be sent for all messages that were not
marked for deletion.
Possible responses:
+OK unique-id listing follows
-ERR no such message
Examples:
C: UIDL S: +OK
S: 1 whqtswO00WBw418f9t5JxYwZ
S: 2 QhdPYR:00WBw1Ph7x7
S: . ...
C: UIDL 2
S: +OK 2 QhdPYR:00WBw1Ph7x7 ...
C: UIDL 3
S: -ERR no such message, only 2 messages in maildrop
Command:
LOOP
This comman simply says to the server that we are still connected so that he keep the connection alive
Hi
Thanks for the code. The only problem i have with directly getting emails via POP3 then it will not download into my outlook as well.
Is there a way I can just connect to my inbox in outlook?
Thanks
You can leave them on the server and the next time outlook is run they will be downloaded
RETR will only retrieve the message. To delete it you should use the DELE command also.
Well, first you need to download and install
Redistributable Primary Interop Assemblies for the MS Office version you are using.
Then you should add a reference to the outlook interop assembly.
Then try this code. It's been ported from VB6 so there might be some inconsitencies.
vb.net Code:
Public Sub ProcessInbox() Dim oOutlook As Outlook.Application Dim oNs As Outlook.NameSpace Dim oFldr As Outlook.MAPIFolder Dim oAttachments As Outlook.Attachments Dim oAttachment As Outlook.Attachment Dim iMsgCount As Integer Dim oMessage As Outlook.MailItem Dim iCtr As Long, iAttachCnt As Long Dim sFileNames As String Dim aFileNames() As String 'get reference to inbox oOutlook = New Outlook.Application oNs = oOutlook.GetNamespace("MAPI") oFldr = oNs.GetDefaultFolder(olFolderInbox) Debug.WriteLine ("Total Items: " & oFldr.Items.Count) Debug.WriteLine("Total Unread items = " & oFldr.UnReadItemCount) For Each oMessage In oFldr.Items With oMessage 'basic info about message Debug.WriteLine(.To) Debug.WriteLine(.CC) Debug.WriteLine(.Subject) Debug.WriteLine(.Body) If .UnRead Then Debug.WriteLine("Message has not been read") Else Debug.WriteLine("Message has been read") End If iMsgCount = iMsgCount + 1 'reference and save all attachments With oMessage.Attachments iAttachCnt = .Count If iAttachCnt > 0 Then For iCtr = 1 To iAttachCnt .Item(iCtr).SaveAsFile "C:\" & .Item(iCtr).FileName Next iCtr End If End With End With Application.DoEvents Next oMessage End Sub
Thank You my friend
It's an enumeration member. Try 6 instead of olFolderInbox or see what Intellisense suggests.
I repeat - this code was ported by me from some example I've found for VB6. It might be non working at all.