C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,433 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,497 people online right now. Registration is fast and FREE... Join Now!




Send Email Using Gmail

 
Reply to this topicStart new topic

> Send Email Using Gmail

noorahmad
Group Icon



post 1 May, 2009 - 10:22 PM
Post #1


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


Attached File(s)
Attached File  Send_Email_Using_Gmail_Account.zip ( 54.36k ) Number of downloads: 240
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

born2c0de
Group Icon



post 1 May, 2009 - 11:31 PM
Post #2
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.
Go to the top of the page
+Quote Post

noorahmad
Group Icon



post 2 May, 2009 - 12:11 AM
Post #3
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.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
3 User(s) are reading this topic (3 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 12:49AM

Live C# Help!

Be Social

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

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month