Page 1 of 1

Send Email Using Gmail

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 01 May 2009 - 11:22 PM

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
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
Boolean _Boolean = false;



And my all code is here
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
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();
		}



Attached File(s)


This post has been edited by noorahmad: 02 May 2009 - 01:07 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Send Email Using Gmail

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 02 May 2009 - 12:31 AM

The CheckTextBox() is buggy. It displays the "Fields Incomplete" MessageBox even when everything is filled out properly.

I commented out the CheckTexBox() function call and it worked fine.
Was This Post Helpful? 0
  • +
  • -

#3 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 02 May 2009 - 01:11 AM

Quote

The CheckTextBox() is buggy. It displays the "Fields Incomplete" MessageBox even when everything is filled out properly.


thanks born2c0de.

and now it is free from bug.
Was This Post Helpful? 0
  • +
  • -

#4 viswa.teja@live.in  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-September 09

Posted 03 December 2009 - 01:23 AM

Small help here how to resive the mails from gmail using C#. Please kindly help its urgent please. I have to submit project.
Was This Post Helpful? 0
  • +
  • -

#5 Charny  Icon User is online

  • New D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 42
  • Joined: 10-January 10

Posted 07 February 2010 - 03:37 PM

How can I add attachments to the message?
Was This Post Helpful? 0
  • +
  • -

#6 Guest_matix*


Reputation:

Posted 24 March 2010 - 06:38 AM

Hi,

After using your code, my gmail account was temporarily disabled?
Was This Post Helpful? 0

#7 Obee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-April 10

Posted 30 May 2010 - 12:47 PM

TY wery much!

This post has been edited by Obee: 30 May 2010 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_Dinuni*


Reputation:

Posted 15 June 2010 - 07:56 AM

View Postnoorahmad, on 01 May 2009 - 10:22 PM, said:

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
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
Boolean _Boolean = false;



And my all code is here
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
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();
		}






Hi!
It was helped me a lot and has worked at the first time without errors. So thank you very much. Keep going the good work.
Was This Post Helpful? 0

#9 Guest_Pierre*


Reputation:

Posted 16 September 2010 - 06:06 AM

This code works great, thank you very much for sharing it!
Was This Post Helpful? 0

#10 midouban  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-July 09

Posted 25 November 2010 - 09:33 AM

I,ve tried the code, it sent the Body text and Subject name well, but not for the attachment. I only attached 1 picture JPEG file, and it wasnt sent.
So, How?

Please........ :angel:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1