2 Feb 2012

A Simple Windows Forms Application to Send an E-Mail


The Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets for managed implementation of Internet protocols that applications can use to send or receive data over the Internet .The  SMTP(Simple Mail Transfer Protocol) is using for sending email from C# code. 
The Below application is used to Describe How to send a mail from Gmail account using C# code.




The Source Code For This Application is :


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.Mail;

namespace SendEmail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress(txtFrm.Text);
                mail.To.Add(txtTo.Text);
                mail.Subject = txtSub.Text;
                mail.Body = txtMsg.Text;
                SmtpServer.Port = 25;
                SmtpServer.Credentials = new System.Net.NetworkCredential(txtFrm.Text.ToString(),txtPsw.Text.ToString());
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                MessageBox.Show("Mail Sent Successfull");
               
            }
            catch(Exception emsg)
            {
                MessageBox.Show("Mail Sending Failed..Due to" + emsg.Message.ToString());
            }
            txtFrm.Text = txtPsw.Text = txtSub.Text = txtMsg.Text = txtTo.Text = "";
            button1.Enabled = true;

        }
       
    }
}


For downloading the Entire Solution File: CLICK HERE