Results 1 to 12 of 12

Thread: Outlook Inegration

Hybrid View

  1. #1
    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

  2. #2

    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

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