tags:

views:

458

answers:

4

I'm trying to read through a gmail account to get gps data that is being sent there ( in the text of a email) from an moble phone (my phone)

using (Pop3Client cl = new Pop3Client())
            {
                cl.UserName = "crash893";
                cl.Password = "password";
                cl.ServerName = "pop.gmail.com";
                cl.AuthenticateMode = Pop3AuthenticateMode.Pop;
                cl.Ssl = true;
                cl.Authenticate();
                ///Get first mail of my mailbox
                Pop3Message mg = cl.GetMessage(1);  <<<<<<<<<< ERROR
                String MyText = mg.BodyText;
                ///If the message have one attachment
                Pop3Content ct = mg.Contents[0];
                ///you can save it to local disk
                ct.DecodeData("c:\\test.txt");
            }

but I get a exception on the "get first mail of mailbox message

"Higuchi.Net.Pop3.Pop3ConnectException: Pop3 connection is closed
at Higuchi.Net.Pop3.Pop3Client.SendCommand(String inCommand)
at Higuchi.Net.Pop3.Pop3Client.Execute(String inCommand, Boolean inIsMultiLine)
at Higuchi.Net.Pop3.Pop3Client.Execute(Pop3Command inCommand)
at Higuchi.Net.Pop3.Pop3Client.GetMessage(Int64 inMailIndex)"}

Ideally what i would like to do is open this read all the new unread emails in this account for a certain subject line then read the data in the body and mark them as read

does anyone know why its erroring out

does anyone have any experince with c#mail that hey could point me in the right direction for reading and makring emails as read etc

+1  A: 

It is not possible to mark emails as read using the POP protocol.

Try using IMAP.

SLaks
are you sure? how does outlook do it via pop3?
Crash893
do you have a easy to use free imap client?
Crash893
Outlook does not do it via POP3. When you mark a message as read in Outlook, it updates a local database. The only interaction between Outlook and the POP server is to download the message in the first place.
SLaks
To do this using IMAP, you'll also need to reconfigure Outlook on your phone to connect to Gmail using IMAP.
SLaks
You can find a .Net IMAP client here: http://www.lumisoft.ee/lsWWW/Download/Downloads/Net/. Or, try searching Stack Overflow or Google
SLaks
A: 

I do not have experience with C#Mail, and this answer may not help, but I've experienced weirdness in the past while trying to write email send/receive related code.

Turned out the antivirus software we were running at work had a whitelist of allowed .EXE's that could make in/outbound POP3 or SMTP connections. Any chance this is your problem?

Yoopergeek
+2  A: 

cl.Port = 995;

Crash893
+1  A: 
        using (Pop3Client cl = new Pop3Client())
        {
            cl.UserName = "ewgsdssw";
            cl.Password = "sdgwsegw";
            cl.ServerName = "pop.gmail.com";
            cl.AuthenticateMode = Pop3AuthenticateMode.Pop;
            cl.Port = 995;
            cl.Ssl = true;
            cl.Authenticate();
            ///Get first mail of my mailbox
            ///
            int total = Convert.ToInt16(cl.GetTotalMessageCount());

            while (total >= 1)
            {
                Pop3Message mg = cl.GetMessage(total);
                if (mg.Subject == "I am Here")
                {

                    // http://maps.google.com/maps?q=38.89552,-77.43265
                    //(+/- 76 metres.)

                    string location = mg.BodyText;
                    location = location.Replace("http://maps.google.com/maps?q=","~");
                    location = location.Replace("metres.)\r\n\r\n","~");

                    location = location.Split('~')[1];

                    location = location.Replace("(+/- ", ",");
                    location = location.Replace("\r\n", "");


                    string[] data = location.Split(',');
                    string lat = data[0];
                    string lon = data[1];
                    string res = data[2];
                    DateTime time = mg.Date;

                    textBox1.AppendText(string.Format("Lat: {0} LON: {1} Res: {2} TIME: {3}\r\n",lat,lon,res,time.ToString()));

                }

                total--;
            }

        }
Crash893
You should probably make total a long (not int), and get rid of Convert.ToInt16.
SLaks