none
I need a help. please help me

    Question

  • I want to open any mail account depending on the mail ID and password were entered by the user into textboxes and when click on a button the internet explorer will open on the user account. after this page had opened the user can access his account such as sending messages and open inbox messages to read the messages. in another word i want to make user login to his mail through windows application.

      my code:

    private void button1_Click(object sender, EventArgs e)

    {

    string id = textBox1.Text;

    string pass = textBox2.Text;

    login(id,pass)

    }

    private void login(x,y)

    {

    }

    please tell me what is the appropriate code that should be put in the function login(x,y) to work as i want.

     

                                                         

    Wednesday, March 05, 2008 9:07 AM

Answers

  • Here's a sample that fills in the google search form and clicks the button. Same idea, but you need to find the appropriate url and element names/ids for your mail client.

     

    Code Snippet

    using System;
    using System.Windows.Forms;

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            using(Form form = new Form())
            using (WebBrowser browser = new WebBrowser())
            {
                browser.Dock = DockStyle.Fill;
                form.Controls.Add(browser);
                form.Load += delegate
                {
                    browser.Navigate("http://www.google.com");
                };
                browser.DocumentCompleted += delegate
                {
                    foreach (HtmlElement el in browser.Document.All)
                    { // set the value of the "q" field (think: username etc)
                        if (el.Name == "q")
                        {
                            el.SetAttribute("value", "foo");
                            break;
                        }
                    }
                    foreach (HtmlElement el in browser.Document.All)
                    { // click the submit button
                        if (el.Name == "btnG")
                        {
                            el.InvokeMember("click");
                            break;
                        }
                    }
                };
                Application.Run(form);
            }
           
        }
    }

     

     

     

     

     

    Wednesday, March 05, 2008 10:59 PM
  • Create a new win-forms project:

    File->New->Project

    Visual C#->Windows->Windows Application

     

    Find Program.cs in the Solution Explorer:

    View->Solution Explorer

    [solution name]->[project name]->Program.cs

     

    Double click Program.cs, and replace *everything* with the code I previously pasted. Run.

     

    If you want to stop the constant re-posting, change as follows (swap approx lines 20-40):

     

                bool done = false;
                browser.DocumentCompleted += delegate
                {
                    if (done) return;
                    foreach (HtmlElement el in browser.Document.All)
                    { // set the value of the "q" field (think: username etc)
                        if (el.Name == "q")
                        {
                            el.SetAttribute("value", "foo");
                            break;
                        }
                    }
                    foreach (HtmlElement el in browser.Document.All)
                    { // click the submit button
                        if (el.Name == "btnG")
                        {
                            el.InvokeMember("click");
                            done = true;
                            break;
                        }
                    }
                };

     

    Marc

    Friday, March 07, 2008 12:36 PM

All replies

  • I don't think this will be trivial... first off (and most importantly): what web-mail client is the user using?

     

    You can automate shdocvw (also known as "Internet Explorer") by hosting a WebBrowser in your app and modifying properties in the DHTML tree - i.e. find the username box using its HTML id, and set the .text / .value - and ditto for the password - then find the submit button by ID and use DHTML to simulate a click. But note that some mail clients will actively try and stop you from doing this by using random[ish] element ids.

     

    For gmail, for example, the ids: "Email" and "Passwd" for the inputs, and a submit button with the name "signIn" but no id. Other clients will have *completely* different layout; and you can fully expect these forms to change occasionally.

     

    You might also consider more shared logins, such as passport or infocard as alternatives...

    Wednesday, March 05, 2008 9:22 AM
  • thank you very much for your helping

    I am begineer in this subject and i cant make what you said exactly, please complete your favour with me and send to me sample for this code.

    Really it is a good idea. I hope that you help me. I think that I ask the right person. again thank you.

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    Wednesday, March 05, 2008 7:52 PM
  • Here's a sample that fills in the google search form and clicks the button. Same idea, but you need to find the appropriate url and element names/ids for your mail client.

     

    Code Snippet

    using System;
    using System.Windows.Forms;

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            using(Form form = new Form())
            using (WebBrowser browser = new WebBrowser())
            {
                browser.Dock = DockStyle.Fill;
                form.Controls.Add(browser);
                form.Load += delegate
                {
                    browser.Navigate("http://www.google.com");
                };
                browser.DocumentCompleted += delegate
                {
                    foreach (HtmlElement el in browser.Document.All)
                    { // set the value of the "q" field (think: username etc)
                        if (el.Name == "q")
                        {
                            el.SetAttribute("value", "foo");
                            break;
                        }
                    }
                    foreach (HtmlElement el in browser.Document.All)
                    { // click the submit button
                        if (el.Name == "btnG")
                        {
                            el.InvokeMember("click");
                            break;
                        }
                    }
                };
                Application.Run(form);
            }
           
        }
    }

     

     

     

     

     

    Wednesday, March 05, 2008 10:59 PM
  • really thank you but can you please attach to me the full program because I try the code but it dosen't run.

    best regards

     

     

     

    Thursday, March 06, 2008 5:03 PM
  • I have re-verified it, and it is fine; it compiles and runs fine. A minor glitch is that it constantly re-posts, but that is easily fixed by setting a bool flag sowhehere (3 lines of code, tops [I've just done it]).

     

    If it doesn't work for you, then please quote an actual error. The code is complete, so I'm not sure what else you need...

    Thursday, March 06, 2008 7:35 PM
  • thank you marc but if you have the c# program

    for this code please send it to me to the following mail:

    rawaaljabaly@hotmail.com

    I hope that you attach the program and send it to the previous email.

    Friday, March 07, 2008 12:16 PM
  • Quite frankly: no.

    The demo code is exactly as already posted above. I don't see any benefit in starting a separate e-mail discussion about it.

    If you are having a problem with the code I have posted, then please tell me the problem.

    Friday, March 07, 2008 12:19 PM
  • dear marc I work on c#.net 2005 and I cant find the method main so how can I put your code in my program.

    Friday, March 07, 2008 12:25 PM
  • Create a new win-forms project:

    File->New->Project

    Visual C#->Windows->Windows Application

     

    Find Program.cs in the Solution Explorer:

    View->Solution Explorer

    [solution name]->[project name]->Program.cs

     

    Double click Program.cs, and replace *everything* with the code I previously pasted. Run.

     

    If you want to stop the constant re-posting, change as follows (swap approx lines 20-40):

     

                bool done = false;
                browser.DocumentCompleted += delegate
                {
                    if (done) return;
                    foreach (HtmlElement el in browser.Document.All)
                    { // set the value of the "q" field (think: username etc)
                        if (el.Name == "q")
                        {
                            el.SetAttribute("value", "foo");
                            break;
                        }
                    }
                    foreach (HtmlElement el in browser.Document.All)
                    { // click the submit button
                        if (el.Name == "btnG")
                        {
                            el.InvokeMember("click");
                            done = true;
                            break;
                        }
                    }
                };

     

    Marc

    Friday, March 07, 2008 12:36 PM
  • thank you very much marc. the code is running that was my mistake.I am so sorry. I will try to write one like it for opening mail account. if i have any problem I will ask you.

    best regards

    Friday, March 07, 2008 12:37 PM
  • dear marc the form that contain the google page in the running stage is reload every second. how can solve this problem?

     

    Friday, March 07, 2008 12:44 PM
  • hi marc,

    can you explain the code for me? because I am beginner and I dont understand it well. please document for me each line by comment statements.

    Friday, March 07, 2008 12:47 PM
  • thank you very much marc. I have a problem

    after the I replace the mail account with your code the yahoo mail page open but the button not clicked just the textboxes was filled. how can I make the button clicked.

    I hope to listen from you soooooon.

     

    Friday, March 07, 2008 2:09 PM
  • on yahoo mail the button name is ".save"; if you put a break-point on the invoke, is it finding the button?

    Friday, March 07, 2008 2:25 PM
  • I've just tried, and it works fine.

     

    Watch out, though - if you get it wrong a couple of times it quite-correctly detects that you are scripting, and presents a captcha ;-p

     

    http://en.wikipedia.org/wiki/Captcha

     

    Friday, March 07, 2008 2:29 PM
  • yas I see that, I put the following code but not work

    foreach (HtmlElement el in browser.Document.All)

    { // click the submit button

    if (el.Name == ".save")

    {

    el.InvokeMember("click");

    done = true;

    break;

    }

    }

    Friday, March 07, 2008 2:31 PM
  • Can you degine "not work"?

     

    Note that it doesn't submit anything unless you have put in the username and password first...

     

    I get a login failed box; are you sure you aren't also seeing a captcha? it wouldn't surprise me...

     

    Friday, March 07, 2008 2:36 PM
  •  

     please I have another question how can I put this code on a button, in other words how can I from form have textboxes to entered username and  password then when I click on a button (sign in) the email will open if the entered information is correct. how can I do that?

    Friday, March 07, 2008 3:50 PM
  • hi marc

    please anaswer me the following question:

    how can i view the adress bar in the browser when the program run?

    how can maximize the form that contain the the web page?

    how can I hide the title bar of the form?

     

    marc really you are a great person because you solve my problem, please answer me the previous questions.

     

    Saturday, March 08, 2008 9:51 AM