2012-11-22 1 views
-3

사용자가 자신의 Gmail 계정에서 이메일을 보낼 수있는 작은 응용 프로그램을 만들기 위해 Windows 양식을 사용하기 시작했습니다. 사용자가 로그인 양식 (FORM 1)에 올바른 로그인 자격 증명을 입력하면 메일을 보낼 수 있지만My C# windows의 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

구글이 제공 ..., 내 메일 (양식 2)를 입력하고 난 코드에서 Gmail 로그인 Credentials..Help 저를 검사 할 수 있도록 오류가 표시 로그인 양식 (양식 1)에서 잘못된 자격 증명을 입력 귀하의 문제를 해결해야합니다. 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

관련 문제