Code keeps running to the point where it throws me an exception

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 880 Views - Last Post: 03 April 2017 - 07:49 PM Rate Topic: -----

#1 gooby31   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 07-December 16

Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 05:37 AM

So I have this application which will help me with my emailing for some of my customers and everything is working just fine until it gets to the last invoke. It presses the button and logs me in but then it keeps running the code and loops back to the button click again which throws me an error since the button isnt there anymore due to the fact that we already logged in so the HtmlDocument isnt the same anymore. I've tried to put a return; at the end but that didnt stop my code from running and im fairly lost at this point. I've tried debugging it with breakpoints and it seems to be, as I mentioned loop through the code again.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NoTheme
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("https://accounts.google.com/ServiceLogin#identifier");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            HtmlDocument startDoc = webBrowser1.Document;
            HtmlElement usernameElement = startDoc.GetElementById("Email");
            HtmlElement loginBtnElement = startDoc.GetElementById("signIn");

            usernameElement.SetAttribute("value", "[email protected]");
            loginBtnElement.InvokeMember("click");
            webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;
        }

        private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            HtmlDocument startDoc = webBrowser1.Document;
            HtmlElement usernameElement = startDoc.GetElementById("Passwd");
            HtmlElement loginBtnElement = startDoc.GetElementById("signIn");

            usernameElement.SetAttribute("value", "thePassword"); //this is where it throws the error
            loginBtnElement.InvokeMember("click");
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Code keeps running to the point where it throws me an exception

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16480
  • View blog
  • Posts: 65,328
  • Joined: 12-June 08

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 06:43 AM

See - you are going the wrong way of doing it. It's not about navigating a page when you have the gmail API to use.
https://developers.g....com/gmail/api/
Was This Post Helpful? 0
  • +
  • -

#3 gooby31   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 07-December 16

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 07:14 AM

So would you recommend me to use the api to check if the account is able to log in?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16480
  • View blog
  • Posts: 65,328
  • Joined: 12-June 08

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 07:18 AM

I would recommend you use the API for all your gmail needs. Mostly on the 'sending email' variety of life.
Was This Post Helpful? 0
  • +
  • -

#5 gooby31   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 07-December 16

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 07:20 AM

Oh alright!
Would you happen to know where I can find docs about logging in to an account with this API? I tried googleing but no luck.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16480
  • View blog
  • Posts: 65,328
  • Joined: 12-June 08

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 07:21 AM

I would start with the link posted. Google is pretty solid about providing a ton of information on getting api code up and running.
Was This Post Helpful? 0
  • +
  • -

#7 gooby31   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 07-December 16

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 07:24 AM

I read through this but im not sure if it will work. Do you think I will be able to login to my gmails and throw an exception if there is an error?
http://www.daimto.co...ail-with-c-net/
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16480
  • View blog
  • Posts: 65,328
  • Joined: 12-June 08

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 07:39 AM

I don't understand your question.

The API allows you to retrieve emails and send emails. That is their route they prefer developers to go through.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,430
  • Joined: 05-May 12

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 08:21 AM

From my point of view, the OP wants to have a UI to play with while manipulating the user's email. The GMail API's are all written so that UI is not required (nor really desired).
Was This Post Helpful? 0
  • +
  • -

#10 gooby31   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 07-December 16

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 08:44 AM

Thats pretty much it, I want to be able to log in to emails through a UI.. Lets say two textboxes and a button, when I press the button it tries to login to the email and password I added to the two textboxes.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,430
  • Joined: 05-May 12

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 09:00 AM

You do realize that good email clients (ex. Outlook, Mailbird, MetroMail, etc.) actually use the mail APIs and or POP/IMAP/SMTP protocols to get the mail data and present their own UI. They don't just provide a web browser that hosts the web based email view like you are trying to do.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16480
  • View blog
  • Posts: 65,328
  • Joined: 12-June 08

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 09:06 AM

Maybe it needs to go with saying, but they, The Googles, provided developer API to use so you are not trying to kludge some automated thing trying to work with a browser. All your mail clients would be just GUI representation of the APIs as it is.

aaaaaaaaaand skydiver beat me to it.
Was This Post Helpful? 0
  • +
  • -

#13 gooby31   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 07-December 16

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 09:29 AM

Yeah I do understand that using the API will take the whole "Trying to automate the whole login process" away but I really cant figure out which function in the API to use for logging in to a account, not logging into the API but to an actual gmail adress.
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16480
  • View blog
  • Posts: 65,328
  • Joined: 12-June 08

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 09:38 AM

Okay.. well there's always the SMTP namespace.
https://msdn.microso...(v=vs.110).aspx
https://developers.g.../imap/imap-smtp


Example of their API's get:
https://developers.g...rs/messages/get

The whole giant section labeled '.NET'.
https://developers.g...ickstart/dotnet
Was This Post Helpful? 0
  • +
  • -

#15 gooby31   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 07-December 16

Re: Code keeps running to the point where it throws me an exception

Posted 03 April 2017 - 09:45 AM

I found this one video that will probably be able to explain it on a lower level.
Also, About the less secure apps, is there a way to bypass it or do I need to enable it on ALL of my accounts?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2