Results 1 to 12 of 12

Thread: Outlook Inegration

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Outlook Inegration

    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

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: Outlook Inegration

    Quote Originally Posted by cicatrix View Post
    Reading the data directly from your POP3 server would be easier, really )))
    Great - how can I do this?

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Outlook Inegration

    You'll need a TCPClient connected to your POP3 server through port 110 (default)
    vb.net Code:
    1. Private Sub POP3Demo(ByVal server As String, ByVal user As String, ByVal pass As String)
    2.     Dim client As Net.Sockets.TcpClient
    3.     Dim sr As IO.StreamReader
    4.     Dim sw As IO.StreamWriter
    5.     Dim ns As Net.Sockets.NetworkStream
    6.     Dim response As String
    7.  
    8.     'connecting to a server
    9.     client = New Net.Sockets.TcpClient
    10.     Try
    11.         client.Connect(server, 110)
    12.     Catch e As Exception
    13.         MessageBox.Show("Failed to connect: " & e.ToString, "Error")
    14.         Return
    15.     End Try
    16.     ns = client.GetStream
    17.     sr = New IO.StreamReader(ns, Encoding.ASCII, True) ' this sends our commands to the server
    18.     sw = New IO.StreamWriter(ns)  ' and this will read server's response
    19.     response = sr.ReadLine()          ' reading the response
    20.  
    21.  
    22.     '  At this point the response should contain a string +OK and some text after it.
    23.     '  Also it sends CRLF at the end of each string
    24.  
    25.     ' We should send:
    26.     ' user [username] & ControlChars.CrLf
    27.  
    28.     ' The server should respond:
    29.     ' +OK [some text] - a user exists you may continue
    30.     ' -ERR [some text] - either some error or user was not found
    31.  
    32.     sw.WriteLine("USER " & user & ControlChars.CrLf)
    33.     sw.Flush()
    34.  
    35.     response = sr.ReadLine()
    36.  
    37.     If response.Substring(0, 3) = "-ER" Then
    38.         MessageBox.Show("Could not login", "Error")
    39.         Return
    40.     End If
    41.  
    42.     ' We then should send a password
    43.     ' And the response is also either +OK or  -ERR
    44.  
    45.     ' Sending password
    46.     sw.WriteLine("PASS " & pass & Chr(13) & Chr(10))
    47.     sw.Flush()
    48.     response = sr.ReadLine
    49.        
    50.     If response.Substring(0, 3) = "-ER" Then
    51.         MessageBox.Show("Password is incorrect", "Error")
    52.         Return
    53.     End If
    54.  
    55.     ' We logged in then we should get the number of messages and their size
    56.     ' We should send 'STAT'
    57.     ' The response would be:
    58.     ' +OK [number] [size]
    59.  
    60.     Dim stats() As String
    61.     Dim totalmessages, totalsize As Integer
    62.    
    63.     sw.WriteLine("STAT")
    64.     sw.Flush()
    65.     response = sr.ReadLine()
    66.     stats = response.Split(" ")
    67.     totalmessages = CInt(stats(1))
    68.     totalsize = CInt(stats(2))
    69.    
    70.     ' Now we should determine messages indices and the size of each message
    71.  
    72.     ' Send:
    73.     ' LIST
    74.     ' Response (example):
    75.     ' +OK 2 messages 320
    76.     ' 1 120
    77.     ' 2 200
    78.     ' The list is terminated by a new line and "." character
    79.  
    80.  
    81.     Dim i As Integer
    82.     Dim Messages() As String
    83.  
    84.     ' getting messages identifiers
    85.  
    86.     sw.WriteLine("LIST")
    87.     sw.Flush()
    88.     response = sr.ReadLine()
    89.        
    90.     stats = response.Split(" ")
    91.     ReDim Messages(totalmessages - 1)
    92.  
    93.     For i = 0 To CInt(stats(1)) - 1
    94.         Messages(i) = (sr.ReadLine())
    95.     Next
    96.  
    97.  
    98.     ' Now we can manipulate messages the following way
    99.     ' To delete a message you should send DELE [x] where x is a message index (see above)
    100.     ' The response is as usual either +OK [some text] (operation successful)
    101.     ' or -ERR [some text] (error)
    102.  
    103.     ' This deletes message number 1 from the server (uncomment)
    104.     ' sw.WriteLine("DELE 1")
    105.     ' sw.Flush()
    106.     ' response = sr.ReadLine()
    107.  
    108.     ' To retrieve (download) a message you should use RETR command
    109.     ' Send (x is a message index):
    110.     ' RETR [x]
    111.     ' The message will be sent as plain text and will be terminated by a empty line and the "." character
    112.     ' Again, possible answers are: +OK [some text] or -ERR [some text]
    113.    
    114.     ' For example:
    115.     ' +OK 120 octets
    116.     ' [message header]
    117.     ' [message body]
    118.     ' .
    119.    
    120.  
    121.     ' Let's get all messages:
    122.  
    123.      sr = New IO.StreamReader(ns, Encoding.Default, True)
    124.  
    125.      For i = 0 To (totalmessages - 1)
    126.          sw.WriteLine("RETR " & (i + 1))
    127.          sw.Flush()
    128.  
    129.          ' We just dump the messages to the screen:
    130.    
    131.          Do While response <> "."
    132.              response = sr.ReadLine
    133.              Console.WriteLine(response & Environment.NewLine)
    134.          Loop
    135.      Next
    136.  
    137.      ' Quit gracefully:
    138.      sw.WriteLine("QUIT") ' Disconnecting
    139.  
    140.      ' Closing the client
    141.      ' client.Close
    142. 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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: Outlook Inegration

    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

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Outlook Inegration

    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.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: Outlook Inegration

    Quote Originally Posted by cicatrix View Post
    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.
    Hi

    It all seems too much, all i need is the messages that are in my outlook.

    is there a way just to get all the mail from my current outlook folder i.e. inbox etc?

    thanks

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Outlook Inegration

    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:
    1. Public Sub ProcessInbox()
    2.     Dim oOutlook As Outlook.Application
    3.     Dim oNs As Outlook.NameSpace
    4.     Dim oFldr As Outlook.MAPIFolder
    5.     Dim oAttachments As Outlook.Attachments
    6.     Dim oAttachment As Outlook.Attachment
    7.     Dim iMsgCount As Integer
    8.  
    9.     Dim oMessage As Outlook.MailItem
    10.  
    11.     Dim iCtr As Long, iAttachCnt As Long
    12.  
    13.     Dim sFileNames As String
    14.     Dim aFileNames() As String
    15.  
    16.  
    17.      'get reference to inbox
    18.      oOutlook = New Outlook.Application
    19.      oNs = oOutlook.GetNamespace("MAPI")
    20.      oFldr = oNs.GetDefaultFolder(olFolderInbox)
    21.      Debug.WriteLine ("Total Items: " & oFldr.Items.Count)
    22.      Debug.WriteLine("Total Unread items = " & oFldr.UnReadItemCount)
    23.  
    24.  
    25.      For Each oMessage In oFldr.Items
    26.        
    27.          With oMessage
    28.          'basic info about message
    29.              Debug.WriteLine(.To)
    30.              Debug.WriteLine(.CC)
    31.              Debug.WriteLine(.Subject)
    32.              Debug.WriteLine(.Body)
    33.              If .UnRead Then
    34.                  Debug.WriteLine("Message has not been read")
    35.              Else
    36.                  Debug.WriteLine("Message has been read")
    37.              End If
    38.              iMsgCount = iMsgCount + 1
    39.              
    40.              'reference and save all attachments
    41.              With oMessage.Attachments
    42.                 iAttachCnt = .Count
    43.                 If iAttachCnt > 0 Then
    44.                     For iCtr = 1 To iAttachCnt
    45.                        .Item(iCtr).SaveAsFile "C:\" & .Item(iCtr).FileName
    46.                     Next iCtr
    47.                 End If
    48.             End With
    49.         End With
    50.         Application.DoEvents
    51.     Next oMessage
    52.    
    53. End Sub

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: Outlook Inegration

    Thank You my friend

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: Outlook Inegration

    Quote Originally Posted by cicatrix View Post
    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:
    1. Public Sub ProcessInbox()
    2.     Dim oOutlook As Outlook.Application
    3.     Dim oNs As Outlook.NameSpace
    4.     Dim oFldr As Outlook.MAPIFolder
    5.     Dim oAttachments As Outlook.Attachments
    6.     Dim oAttachment As Outlook.Attachment
    7.     Dim iMsgCount As Integer
    8.  
    9.     Dim oMessage As Outlook.MailItem
    10.  
    11.     Dim iCtr As Long, iAttachCnt As Long
    12.  
    13.     Dim sFileNames As String
    14.     Dim aFileNames() As String
    15.  
    16.  
    17.      'get reference to inbox
    18.      oOutlook = New Outlook.Application
    19.      oNs = oOutlook.GetNamespace("MAPI")
    20.      oFldr = oNs.GetDefaultFolder(olFolderInbox)
    21.      Debug.WriteLine ("Total Items: " & oFldr.Items.Count)
    22.      Debug.WriteLine("Total Unread items = " & oFldr.UnReadItemCount)
    23.  
    24.  
    25.      For Each oMessage In oFldr.Items
    26.        
    27.          With oMessage
    28.          'basic info about message
    29.              Debug.WriteLine(.To)
    30.              Debug.WriteLine(.CC)
    31.              Debug.WriteLine(.Subject)
    32.              Debug.WriteLine(.Body)
    33.              If .UnRead Then
    34.                  Debug.WriteLine("Message has not been read")
    35.              Else
    36.                  Debug.WriteLine("Message has been read")
    37.              End If
    38.              iMsgCount = iMsgCount + 1
    39.              
    40.              'reference and save all attachments
    41.              With oMessage.Attachments
    42.                 iAttachCnt = .Count
    43.                 If iAttachCnt > 0 Then
    44.                     For iCtr = 1 To iAttachCnt
    45.                        .Item(iCtr).SaveAsFile "C:\" & .Item(iCtr).FileName
    46.                     Next iCtr
    47.                 End If
    48.             End With
    49.         End With
    50.         Application.DoEvents
    51.     Next oMessage
    52.    
    53. End Sub
    sorry one last thing -im trying to modify the code but it says "olFolderInbox" is not declared - what type shall i declare this as?

  11. #11
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Outlook Inegration

    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.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: Outlook Inegration

    Quote Originally Posted by cicatrix View Post
    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.
    thanks it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width