Thursday, 16 June 2011

Instant Mail Service- C#.Net


SMTP- Instant Mail Service:


SMTP- Simple Mail Transfer Protocol
SMTP is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks.
This article will guide you to Design the SMTP mail Client using  .NET Frame work.
It is a very simple and cool application to mail instantly. Download  project files and Installation Files from the links below.
1. Download Project files for Developer.
2. Download Application Installation file with Windows Installer 4.5 (Size: 21.47 MB)
3. Download Application Installation file without Windows Installer 4.5 (Size: 381.50 KB)
let me guide you about step by step Development of SMTP Instant Mail Client .

Part I:

Design the Form as below.

In Tab1:

1.Add TabControl to the Form
2.Add TextBox (for To address and Subject)
3.Add Rich Text Box (Message Body)
4.Add Buttons( for Send and Clear)



















In Tab 2:
1.Add ComboBox (for Choose mail service Provider)
2.Add TextBox (for get Email address and Password)

















              You may ask why I’m dealing with basic options after  the designing Part. All these options gain their importance only while building and debugging the Application(give more importance for Naming the Controls,classes and Namespaces) .

SMTP Mail Service  basically consists, Two operations :
1. File and Directory Manipulations.
2. SMTP mail client Configuration.


1. File manipulation:
          Here, I implemented file operation instead of Database.It consists of two phases

Write to the File:

 Directory.CreateDirectory(@"C:\mailserv"); //Set path for folder
 FileStream fs = new FileStream(@"C:\mailserv\temp.txt",            
                                            FileMode.CreateNew, FileAccess.ReadWrite);
 fs.Close();
 StreamWriter sd = new StreamWriter(@"C:\mailserv\temp.txt");
 string ad = sendfrmtxt.Text + "\t" + Passwordtxt.Text + "\t" +

                                                 comboBox1.SelectedItem.ToString();
 sd.WriteLine(ad);// write to file
 sd.Flush(); //clear the Buffer
 sd.Close(); //close the writer


Read File:

StreamReader sr = new StreamReader(@"C:\mailserv\temp.txt");// set path for file reader
string tmp = sr.ReadLine();
string[] a = tmp.Split('\t'); // seperat the username and password
sendfrmtxt.Text = a[0];
Passwordtxt.Text = a[1];
passwordtxt2.Text = a[1];

2. SMTP client  Configuration :

Using System.Net.Mail; - class is used used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.

MailMessage mMailMessage = new MailMessage();
mMailMessage.From = new MailAddress("From mail address");
mMailMessage.To.Add(new MailAddress("To mail address"));
mMailMessage.Subject ="My subject";
mMailMessage.Body = "Actual Content of the mail";
mMailMessage.IsBodyHtml = true;
mMailMessage.Priority = MailPriority.High;
//Upto this Everything is Comman for all Mail Services

SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.Port = 587; // Gmail works on this port
mSmtpClient.Host = "smtp.gmail.com";
mSmtpClient.EnableSsl = true; //Gmail works on Server Secured Layr

Note that Port number and Host name may differ for each Mail service
Eg:
1.For yahoo port=456 and host=smtp.mail.yahoo.com
2.For windowslive port=587  and host=smtp.live.com


Part II:

Complete Coding:

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.Configuration;
using System.Net.Mail;//for Mail client
using System.IO;

namespace Mailing
{
    public partial class Mail : Form
    {
   
        SmtpClient mSmtpClient = new SmtpClient();
        public Mail()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.loadall();//Calling Finction
        }

        private void ButtonSend_Click(object sender, EventArgs e)
        {

            try
            {
                MailMessage mMailMessage = new MailMessage();
                mMailMessage.From = new MailAddress(sendfrmtxt.Text);
                mMailMessage.To.Add(new MailAddress(sendto.Text));
                mMailMessage.Subject = sub.Text;
                mMailMessage.Body = mesgtxt.Text;
                mMailMessage.IsBodyHtml = true;
                mMailMessage.Priority = MailPriority.High;
             
                mSmtpClient.Credentials = new
                      System.Net.NetworkCredential(sendfrmtxt.Text, Passwordtxt.Text);
            
                mSmtpClient.Send(mMailMessage);
                MessageBox.Show("Mail send Successfully", "Instant Mail Box");
            }
            catch (Exception e1)
            {
               MessageBox.Show(e1.Message+"CheckSettings,username, password","Error");
            }

        }

        private void Button3_Click(object sender, EventArgs e)
        {

            this.saveall();   //calling function
        }
      
        private void Mail_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Do you want to close the Application", "Instant Mail
                     Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                 MessageBoxDefaultButton.Button2) == DialogResult.No)
                e.Cancel = true;
            else
                e.Cancel = false;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            mesgtxt.Clear();
        }


        private void TabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TabControl1.SelectedIndex == 0)
            {
                this.saveall();
            }
            if (TabControl1.SelectedIndex == 1)
            {
                notify.Visible = false;
            }
        }


//called functions

        public void loadall()
        {
            try
            {
                StreamReader sr = new StreamReader(@"C:\mailserv\temp.txt");
                string tmp = sr.ReadLine();
                string[] a = tmp.Split('\t');

                sendfrmtxt.Text = a[0];
                Passwordtxt.Text = a[1];
                passwordtxt2.Text = a[1];
                comboBox1.SelectedItem = a[2];
                if (a[2] == "Gmail")
                {
                    mSmtpClient.Port = 587; // Gmail works on this port
                    mSmtpClient.Host = "smtp.gmail.com";
                    mSmtpClient.EnableSsl = true; //Gmail works on Server Secured Layr
                }
                else if (a[2] == "Live")
                {
                    mSmtpClient.Port = 587;
                    mSmtpClient.Host = "smtp.live.com";
                    mSmtpClient.EnableSsl = true; //windowslive works on Server Secure
                }
                else if (a[2] == "Yahoo")
                {
                    mSmtpClient.Port = 465;
                    mSmtpClient.Host = "smtp.mail.yahoo.com";
                    mSmtpClient.EnableSsl = true;
                }
                sr.Close();
            }
            catch(Exception)
            {
                MessageBox.Show("Goto to the Settings and Set your username and
                           Password    Contact:www.facebook.com/nmkr90", "New User");
            }
         }



        public void saveall()
        {
            if (Passwordtxt.Text == passwordtxt2.Text)
            {
                try
                {
                    Directory.Delete(@"C:\mailserv", true);
                    comboBox1.SelectedItem = "Gmail";
                }
                catch (Exception)
                {
                    ;
                }
                 Directory.CreateDirectory(@"C:\mailserv");

                try
                {
               FileStream fs = new FileStream(@"C:\mailserv\temp.txt",            
                                            FileMode.CreateNew, FileAccess.ReadWrite);
                    fs.Close();
                }
                catch (Exception)
                {              ;
                }
                StreamWriter sd = new StreamWriter(@"C:\mailserv\temp.txt");
                string ad;
                try
                {
                    ad = sendfrmtxt.Text + "\t" + Passwordtxt.Text + "\t" +
                                                 comboBox1.SelectedItem.ToString();
                }
                catch (Exception)
                {
                   ad = sendfrmtxt.Text + "\t" + Passwordtxt.Text + "\t" + "Gmail";
                }
                sd.WriteLine(ad);
                sd.Flush();
                sd.Close();
                this.loadall();
                notify.Visible = true;
            }
            else
            {
                MessageBox.Show("Re-Enter Password Correctly");
            }
        }

      
    }
}

No comments:

Post a Comment