In this tutorial I will be showing you how you would go about Sending Email Using Gmail in C#. Reading and knowing this Project is very Simple, before we get into any code, you need to understand that this tutorial is not to advanced tutorial, using System.Net namespace. If you are not familiar with this item, I suggest you study them before attempting this tutorial.
Sending Email with System.Net is not so advanced but it is in intermediate level. This tutorial is not to long it is too short. And so this tutorial is not taking you time.
I will start by showing the Getting IP Function, Passing tow parameters.
So Let’s Began:
First of All Import The namespace
CODE
using System.Net.Mail;
The System.Net.Mail namespace provides a simple programming interface for many of the rotocols used on networks today. The WebRequest and WebResponse classes form the basis of what are called pluggable protocols, an implementation of network services that enables you to develop applications that use Internet resources without worrying about the specific details of the individual protocols.
And Then Create Variables
CODE
Boolean _Boolean = false;
And my all code is here
CODE
rivate void btnSend_Click(object sender, EventArgs e)
{
_Boolean = false;
CheckTextBox();
if (_Boolean)
{
txtusername.Focus();
return;
}
try
{
System.Net.NetworkCredential _Credential = new System.Net.NetworkCredential(txtusername.Text ,txtpassword.Text );
System.Net.Mail.MailMessage _MailMessage = new MailMessage();
_MailMessage.To.Add(txtto.Text);
_MailMessage.Subject = txtsubject.Text;
_MailMessage.From = new System.Net.Mail.MailAddress(txtusername.Text);
_MailMessage.Body = txtbody.Text;
System.Net.Mail.SmtpClient _SmtpClient = new System.Net.Mail.SmtpClient("smtp.gmail.com");
_SmtpClient.UseDefaultCredentials = false;
_SmtpClient.EnableSsl = true;
_SmtpClient.Credentials = _Credential;
_SmtpClient.Port = 587;
_SmtpClient.Send(_MailMessage);
MessageBox.Show("Your Message Has Been Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Gmail Sender");
}
}
And here are the other codes
CODE
private void btndlg_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Select File For Attachments";
if (dlg.ShowDialog() == DialogResult.OK)
{
txtattahment.Text = dlg.FileName;
}
}
private void bntclear_Click(object sender, EventArgs e)
{
txtpassword.Text = "";
txtusername.Text = "";
txtfullname.Text = "";
txtto.Text = "dark.hacker01@yahoo.com";
txtcc.Text = "";
txtsubject.Text = "Thanks";
txtattahment.Text = "";
txtbody.Text = "Thanks For Coding";
txtpassword.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
String _strMessage;
_strMessage = "Coded By Noor Ahmad\r\n";
_strMessage += "Email: dark.hacker01@yahoo.com\r\n";
_strMessage += "And Special Thanks To My Dear Teacher M.Arif.";
MessageBox.Show(_strMessage, "Gmail Sender", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void CheckTextBox()
{
String strMessage = "Following Fields Is Require:\r\n";
if (txtusername.Text == "")
{
_Boolean = true;
strMessage += "User Name" + "\r\n";
errorProvider1.Clear();
errorProvider1.SetError(txtusername, "Field Required");
return;
}
if (txtpassword.Text == "")
{
_Boolean = true;
strMessage += "Password" + "\r\n";
errorProvider1.Clear();
errorProvider1.SetError(txtpassword , "Field Required");
return;
}
if (txtfullname.Text == "")
{
_Boolean = true;
strMessage += "Full Name" + "\r\n";
errorProvider1.Clear();
errorProvider1.SetError(txtfullname , "Field Required");
return;
}
if (txtto.Text == "")
{
_Boolean = true;
strMessage += "Receiver Email" + "\r\n";
errorProvider1.Clear();
errorProvider1.SetError(txtto , "Field Required");
return;
}
if (txtsubject.Text == "")
{
txtsubject.Text = "Send Email Using Gmail In VB.Net";
}
if (txtbody.Text == "")
{
txtbody.Text = "Thanks For Coding Noor Ahmad\r\ndark.hacker01@yahoo.com";
}
if (_Boolean )
{
MessageBox.Show(strMessage, "Send Email Using Gmail In VB.Net",MessageBoxButtons.OK ,MessageBoxIcon.Error );
}
}
private void Form1_Load(object sender, EventArgs e)
{
txtpassword.Focus();
}
This post has been edited by noorahmad: 2 May, 2009 - 12:07 AM