C# 在我的C windows窗体应用程序中检查Gmail登录credentails?
我已经开始使用windows窗体创建一个小应用程序,其中用户可以从他们的gmail帐户发送电子邮件,当用户在我的登录表单1中输入正确的登录凭据时,我可以发送邮件,但如果他在登录表单1中输入了错误的凭据,它进入我的邮件表单2并显示错误,因此我想检查Gmail登录凭据..请帮助我获取代码C# 在我的C windows窗体应用程序中检查Gmail登录credentails?,c#,winforms,C#,Winforms,我已经开始使用windows窗体创建一个小应用程序,其中用户可以从他们的gmail帐户发送电子邮件,当用户在我的登录表单1中输入正确的登录凭据时,我可以发送邮件,但如果他在登录表单1中输入了错误的凭据,它进入我的邮件表单2并显示错误,因此我想检查Gmail登录凭据..请帮助我获取代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Syste
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
namespace first
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 a = new Form1();
this.Hide();
a.ShowDialog();
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new NetworkCredential(Form3.tb.Text, Form3.tb1.Text);
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(To.Text));
msg.From = new MailAddress(From.Text);
msg.Subject = Sub.Text;
msg.Body = Body.Text;
client.EnableSsl = true; //for security in gmail,https kind of
client.Send(msg);
try
{
MessageBox.Show("Mail sent successfully", "Praveen Mail");
}
catch (Exception ex)
{
MessageBox.Show("Mail Sending Failed Due to" + ex.Message, "Praveen Mail");
}
}
}
}
谷歌提供了一个可以解决你问题的.NETAPI -----编辑---- 步骤1:注册使用谷歌api。它是免费的,上面的链接描述了这样做的过程
using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Samples.Helper;
using Google.Apis.Tasks.v1;
using Google.Apis.Tasks.v1.Data;
using Google.Apis.Util;
namespace Google.Apis.Samples.TasksOAuth2
{
/// <summary>
/// This sample demonstrates the simplest use case for an OAuth2 service.
/// The schema provided here can be applied to every request requiring authentication.
/// </summary>
public class Program
{
public static void Main(string[] args)
{
// Display the header and initialize the sample.
CommandLine.EnableExceptionHandling();
CommandLine.DisplayGoogleSampleHeader("Tasks API");
// Register the authenticator.
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = "<client id>";
provider.ClientSecret = "<client secret>";
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
// Create the service.
var service = new TasksService(auth);
TaskLists results = service.Tasklists.List().Fetch();
Console.WriteLine("Lists:");
foreach (TaskList list in results.Items)
{
Console.WriteLine("- " + list.Title);
}
Console.ReadKey();
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
Console.Write(" Authorization Code: ");
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
}
}
步骤2:实现下面的代码。我从上面的链接借用了它
using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Samples.Helper;
using Google.Apis.Tasks.v1;
using Google.Apis.Tasks.v1.Data;
using Google.Apis.Util;
namespace Google.Apis.Samples.TasksOAuth2
{
/// <summary>
/// This sample demonstrates the simplest use case for an OAuth2 service.
/// The schema provided here can be applied to every request requiring authentication.
/// </summary>
public class Program
{
public static void Main(string[] args)
{
// Display the header and initialize the sample.
CommandLine.EnableExceptionHandling();
CommandLine.DisplayGoogleSampleHeader("Tasks API");
// Register the authenticator.
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = "<client id>";
provider.ClientSecret = "<client secret>";
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
// Create the service.
var service = new TasksService(auth);
TaskLists results = service.Tasklists.List().Fetch();
Console.WriteLine("Lists:");
foreach (TaskList list in results.Items)
{
Console.WriteLine("- " + list.Title);
}
Console.ReadKey();
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
Console.Write(" Authorization Code: ");
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
}
}
}首先,请您向我们展示您当前拥有的代码。一般来说,如果登录失败,你应该得到Gmail的回复,并将其显示给用户。我已经添加了我的代码,请检查一下。你能详细说明我如何在我的表单代码中使用它吗。我不熟悉表格,所以..@praveen-网站上有一些例子。使用Microsoft OAuth SDK将Google的SDK实现到桌面应用程序中。@praveen-粘贴了我上面粘贴的链接中的一些示例代码。但是拉姆霍恩是对的。。。都在网站上。