Link to home
Start Free TrialLog in
Avatar of landerson999
landerson999

asked on

accessing gmail using pop in vb.net or c#

I am trying to use pop within vb.net to access and read my gmail emails. The problem is that for some reason it does not work, because the host (that would be gmail) always drops the connection ( right after I send the username trying to connect) Does anyone know why this is happening, and if there is a way around it?

I know they are not very nice to people who want to automate things on their site, but I would like to avoid using the browser to get my emails, and use an app i created in vb.net
I would attach a code snippet although it may not be necessary if they block this sort of thing.

If someone can give a working sample, that would be great, and not one using http request etc.... using the POP3 object from the .NET framework...
Avatar of sufianmehmood
sufianmehmood
Flag of Pakistan image

can you provide your code, so that i can have  a look into it??
Avatar of Nasir Razzaq
set the UseSSL property of the SMTPClient to true. Then set the UseDefaultCredentials to false. Then set the username and password. I wasted lots of time to reach here.
And some example (vb)
Imports System
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Threading
Imports System.ComponentModel
 
Public Class Form1
 
    Public Sub SendMessage(ByVal subject As String, ByVal messageBody As String, ByVal toAddress As String)
 
        Try
            Dim message As New MailMessage()
            With message
                .From = New MailAddress("YourAddress@gmail.com", "YourPassword")
                .To.Add(toAddress)
                .Subject = subject
                .Body = messageBody
            End With
 
            Dim networkCredentials As New Net.NetworkCredential()
            With networkCredentials
                .UserName = " YourAddress @gmail.com"
                .Password = " YourPassword "
 
            End With
 
            Dim client As New SmtpClient()
            With client
                .Credentials = networkCredentials
                .Host = "smtp.gmail.com"
                .EnableSsl = True
                .Port = 587
                .Send(message)
            End With
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendMessage("Test", "bla bla bla", " YourAddress@gmail.com")
    End Sub
 
End Class

Open in new window

Avatar of landerson999
landerson999

ASKER

I am adding the code, although like I said I was using the pop object....
Public Function connect() As Integer
        Dim POP3Account As String
        POP3Account = "pop.gmail.com"
        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 = "account@gmail.com"
        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 = "password"
        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

Open in new window

jpailino,
>                .Credentials = networkCredentials
>               .Host = "smtp.gmail.com"
>                .EnableSsl = True
>                .Port = 587
>                .Send(message)

Similar code did not work for me. I had to set UseDefaultCredentials=False and then set Credentials in this sequence to get it working.
I have reviewed widely the use of http request object to get your mail through the web using a browser like interaction...but I was more wanting to do this properly using the POP object as MS says you should do. If the case is that my ISP is blocking or Gmail is blocking this request, this is one thing, but if someone else is able to make this work, then I know it isn't the code.
>> Similar code did not work for me
Strange I have tested this and it was working. I can test it again later but ... NOTICE this is to send !!!
 
 
Yes i know this is for sending. But whether sending or receiving you need to be authenticated and when i used it your way it was not getting authenticated successfully.
I cannot test it now but I will try later (security issue)  : )
I then let you know if it's working for me. I remove this code from a demo project that I have made (a  long time ago) but ... things change. LOL
 
landerson999,
Have you looked to the first examples I have post it ? It's to read email as you need.
I see the discussion is going slowly off topic, but I know and am already using the smtp object for sending mail, my question was regarding using the POP3 object to recieve emails....and this is what is proving to be troublesome. It is fine when i call
>>Server = New TcpClient(POP3Account.Trim, 110)

as soon as I get to the part where i set my username, it lags, then takes 5 minutes and ends the connection with an error stating the host has closed the connection....but it was connected before...i must be doing something wrong???

I am however thinking maybe the router??? Is there anyone that can use this code as per my code snippet, and actually read messages from their account? If so, then it is more so my connection that is no good, otherwise, the code.
Try to disable the antivirus to try it.
I already did and also zonealarm, it still didn't have the desired result, my question is though if someone else can make the same code work....without modification???
I also tried switching from the standard 110 to the new 995 for ssl at gmail...but didn't get access
I even sent gmail an email to ask for their help, in case i may need extra validation or credentials or something
I'm sorry but I cannot try your code now !!!  :(
I can try later if you don't get any answer for that.
 
Thanks for the effort guys, jpaulion, yes I have read those articles and wanted to avoid using http requests and/or atom feeds as these are not the same technologies I am using...i am using the generic pop3 objects that come from the framework. I was hoping someone could be able to tell me if the code was faulty or just not getting any response either from gmail...
landerson999,

Using atom feeds is a nice solution and "easy" to implement. I like that solution but you can try other solutions (I never used this ones)

http://www.example-code.com/vbdotnet/pop3_gmail.asp 
http://www.theserverside.net/discussions/thread.tss?thread_id=48505 

 And sorry about  the off topic comments but we also what to learn more :)  
 

CodeCruiser,

 I have tested now the code to send the emails and works very good!
Thanks I will take a look at them, but what sucks is I am going to reverse engeneer their code, while I am sure that they are probably doing the same thing using http requests...anyways, i appreciate the help, if anyone can get my code working as I said, to see if it works without being behind a router like I am ...it would be great to know if it works...
I have updated my router to have ports open for 110 and 995 and reroute the traffic to my computer making the reuests. So I am assuming that the router should not be the reason it is dropping the connection after the first command being sent (which is the USER command) the return string from the USER command is "" and then when I go to use the PASS command , I get the connection was dropped by the HOST computer error.
Does anyone have a working example using the pop object from the .net framework?
I don't have one working example!
I can try to do that later but I cannot promisse you nothing.
Hey CodeCruiser, I was hoping to get the source code seeing as I am developing this, 3rd party will not give you their source code...I think even they had to figure this one out to make their 3rd party tool....so I know it can be done, no?
Sorry guys, I seem a little baffled that no one is able to make this work, as there are some third party utilities that do this stuff in .NET, as I have stated, I am looking for working code using the POP
protocal and sending commands to the POP server....anyone have a working concept either with gmail or hotmail...I imagine, that with all the experts on this site, it should be possible???
This one is open source which means that source code is also available
http://sourceforge.net/projects/hpop/
I am looking at it and need a few weeks if not month to sift through the mess, but it does not work for me.
It does seem to have the call for POP as per my suggestion, however does anyone else have problems getting it to connect....
Gmail uses POP over SSL layer, try using SSL with your POP code
there are libraries that can help you with that, try using Mail Bee
I have, using port 995, and for the ssl object, which one are you refering to. I was hoping for a working example, no one seems able to make this work! I am looking for someone who has a working example to log into their gmail account using pop and retrieves their messages. Alot of experts, yet no one knows how to do this???
OK...lets look at it this way, maybe I am asking the wrong place, but I thought a .net question was to be asked here. If i were to want to create a new OS that allows me to do everything windows does, but also allows me to run on a cluster....I would hope someone would not tell me to buy windows seeing as they do this...i would hope someone that has already built an OS could give valuable information on how to build my own, please refrain from suggesting 3rd party software as I am looking to build such an application, and not buy one.
raising the points
ASKER CERTIFIED SOLUTION
Avatar of sufianmehmood
sufianmehmood
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You have tried it with a ssl gmail account as of todays' date, or back in 2006 when it worked???
anyone? raising points again
sufianmehmood when you said it worked for gmail, did you use it lately, and if so, where you behind a firewall or router at home, or just s straight connection to the internet???

I am curious, as I am seeing certain people are specifying a hidden command to connect which is STLS which is not listed anywhere, but may be needed???

Any thoughts
i tested it with my gmail account today and it works.
Being precise test time was 09-Oct-2008 5:30 GMT
Are you behind any firewall or router, and the configuration on your gmail account was different then default???
yes i am behind a firewall as well as a router, and the configurations for my gmail account were default, so it should work for you. Confirm that you have access to the google SMTP/POP ports (587/995) on your network, may be your's network's proxy is blocking the data on the ports in use.
i have no idea about the hidden command......as there's no need of it
Note that google SMTP works over TLS
If I am reading this correctly , your routers are configured to allow traffic on ports 587 and 995?
ofcourse! you should have access for these ports from the network admin, check for your access if you do not have it ask for it from your network admin.
There in lay my problem, thanks that was it, the admin had blocked those ports and I had no way of knowing if they were blocked or not until i asked him about it.