C#通过web浏览器自动登录到gmail/hotmail

本文关键字:gmail hotmail 登录 通过 web 浏览器 | 更新日期: 2023-09-27 18:25:54

我正在尝试做一些类似LassPass的事情,但作为一个桌面应用程序。我使用VS 2013、C#和Windows窗体。

我有我的用户电子邮件和gmail或outlook的密码,我想点击桌面程序中的一个按钮打开我的本地默认浏览器,即带有适当链接的Chrome/Firefox。我想在服务中自动登录(gmail/outlook)。我想问我是否必须使用适当的API或仅使用某种链接(https://mail.google.com/login="userlogin",password="userpassword")会很好。

我看到一个gmail API,有如何发送邮件,但我找不到如何登录服务。

是在Gmail或Outlook API功能可以帮助我?

C#通过web浏览器自动登录到gmail/hotmail

您正在寻找硒

一旦你设置好了,你就可以做一些类似的事情

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
class Test
{
    static void Main(string[] args)
    {
        IWebDriver driver = new FirefoxDriver();
        driver.Navigate().GoToUrl("https://www.gmail.com/");
        IWebElement query = driver.FindElement(By.Id("Email"));
        query.SendKeys("my.email@gmail.com");
        query = driver.FindElement(By.Id("next"));
        query.Click();
        // now you just have to do the same for the password page
        driver.Quit();
    }
}

你可能会发现这些链接很有用:

  • http://james-prescott.com/2014/02/01/tutorial-setting-up-selenium-webdriver-visual-studio/
  • http://docs.seleniumhq.org/docs/03_webdriver.jsp#the-5分钟设置启动指南

对于Gmail,您可以使用以下代码进行身份验证

protected void Button1_Click(object sender, EventArgs e)
{
Imap client = new Imap();
// connect to server
client.Connect("imap.gmail.com", 993, SslMode.Implicit);
// authenticate
client.Login("username", "password");
}