C# 通过网络浏览器自动登录 gmail/hotmail

C# automatic login to gmail/hotmail via web browser

我正在尝试做类似 LassPass 的东西,但作为桌面应用程序。我使用 VS 2013、C# 和 Windows 表单。

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

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

Gmail 或 Outlook 中有哪些 API 功能可以帮助我?

对于 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");

}

您正在寻找Selenium

设置完成后,您可以执行类似

的操作
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();
    }
}

您可能会发现这些链接有帮助: