-3

ユーザーが自分のGmailアカウントからメールを送信できる小さなアプリを作成するためにWindowsフォームを使い始めました。ユーザーがログインフォーム(フォーム1)に正しいログイン資格情報を入力するとメールを送信できますが、間違って入力するとログインフォーム(フォーム1)の資格情報、それは私のメール(フォーム2)に入り、エラーを表示するので、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");
            }

        }

    }
}
4

1 に答える 1

5

Googleは、問題を解決するための.netAPIを提供しています。

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

- - - - -編集 - - - -

ステップ1:GoogleAPIを使用するためにサインアップします。それは無料で、そうするためのプロセスは上のリンクで説明されています。

ステップ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);
    }
}

}

于 2012-11-22T04:28:33.723 に答える