I have started using windows form for creating a small App in which user’s are able to send email from their gmail accounts, i’m able to send mails when user enters correct login credentials in my login form(FORM 1) but if he enters wrong credentials in login form(Form 1),it enters my Mail(form 2) and shows error so i want to check Gmail Login Credentials..Help me with the Code…
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");
}
}
}
}
Google provides a .net api that should solve your problem.
https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2
———EDIT——-
Step 1: Sign up to use google api’s. It’s free and the process to do so is described in the link above.
Step 2: Implement the code below. I borrowed it from the link above.
}