School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,156 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,002 people online right now. Registration is fast and FREE... Join Now!



Send Email Using Gmail

Page 1 of 1

Send Email Using Gmail

#1 noorahmad  Icon User is offline

  • Author
  • Icon
  • View blog
  • Group: Author w/DIC++
  • Posts: 2,198
  • Joined: 12-March 09


Dream Kudos: 1600

Posted 01 May 2009 - 10: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 - 12:07 AM

Was This Post Helpful? 0
  • +
  • -


#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 4,566
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Posted 01 May 2009 - 11:31 PM

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

  • Author
  • Icon
  • View blog
  • Group: Author w/DIC++
  • Posts: 2,198
  • Joined: 12-March 09


Dream Kudos: 1600

Posted 02 May 2009 - 12: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
  • Pip
  • Group: New Members
  • Posts: 8
  • Joined: 24-September 09


Dream Kudos: 0

Posted 03 December 2009 - 12: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 offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 13
  • Joined: 10-January 10


Dream Kudos: 0

Posted 07 February 2010 - 02:37 PM

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

Page 1 of 1


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month