2012-11-22 32 views
-3

我已經開始使用Windows窗體創建一個小應用程序,用戶可以從他們的Gmail帳戶發送電子郵件,我可以在用戶在登錄表單中輸入正確的登錄憑證時發送郵件(FORM 1),但如果他進入在登錄表格(表格1)錯誤的證書,進入我的郵箱(表格2),並顯示錯誤,所以我想檢查Gmail登錄Credentials..Help我的代碼...在My C#窗體窗體應用程序中檢查Gmail登錄憑證?

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"); 
      } 

     } 

    } 
} 
+0

首先能否請您告訴我們您目前的代碼。一般來說,如果登錄失敗,您應該收到Gmail的回覆並將其顯示給用戶。 –

+0

我已經添加了我的代碼,請通過它.. – praveen

回答

5

谷歌提供了一個.net api應該可以解決你的問題。

https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2

--------- -------編輯

第1步:註冊使用谷歌API的。它是免費的,上面的鏈接描述了這樣做的過程。

第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); 
    } 
} 

}

+0

請你詳細說明我如何在我的表單代碼中使用此代碼。我是新的形式如此.. – praveen

+0

@praveen - 在網站上有例子。使用Microsoft OAuth SDK將Google的SDK實施到桌面應用程序中。 –

+0

@praveen - 粘貼上面粘貼的鏈接中的一些示例代碼。但Ramhound是正確的...這一切都在網站上。 – drewan50

相關問題