Tuesday, 15 December 2015

Sending Web mail in C#

using System;
using System.IO;
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.Data.OleDb;
using System.Collections;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using BusinessEntities;
using DataAccessLayer;
using System.Net;
using System.Net.Mail;

namespace MultiShop
{
    public partial class FrmDemo : Form
    {
        public FrmDemo()
        {
            InitializeComponent();
        }
        OleDbConnection con;

        private string dbCon()
        {
            StreamReader sr = File.OpenText("dbcon.txt");
            string conread = sr.ReadLine();
            return conread;
        }

        private string var1 = "";
        private string var2 = "";
        private string var3 = "";
        private string var4 = "";//"25";
        private string var5 = "";//"false";
        private string var6 = "";

        private void LoadEmail()
        {
            string str = "Select ServerName,EmailID,EmailPassword,PortNo,SSLInfo from T_EmailConfig";
         
            con = new OleDbConnection(dbCon());
            OleDbCommand cmd = new OleDbCommand(str, con);
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

                 var1 = dr.GetValue(0).ToString();
                 var2 = dr.GetValue(1).ToString();
                 var3 = dr.GetValue(2).ToString();
                 var4= dr.GetValue(3).ToString();
                 var5= dr.GetValue(4).ToString();
                 var6  = dr.GetValue(1).ToString();
            }
        }


        private void FrmDemo_Load(object sender, EventArgs e)
        {
            this.progressBar1.Visible = false;
            textBox1.Focus();
            LoadEmail();
           
        }

        private void sendmail()
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Please enter To Email id ", "Enter ID", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox1.Focus();
                return;
            }
            if (textBox3.Text == "")
            {
                MessageBox.Show("Please enter Cc id ", "Enter ID", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox3.Focus();
                return;
            }
            if (textBox4.Text == "")
            {
                MessageBox.Show("Please enter Bcc id ", "Enter ID", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox4.Focus();
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("Please enter Subject ", "Enter ID", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox2.Focus();
                return;
            }


            try
            {
                MailMessage message = new MailMessage();
                SmtpClient client = new SmtpClient(this.var1);
                message.From = new MailAddress(this.var6);
                string[] strArray = this.textBox1.Text.Split(new char[] { ',' });
                for (byte i = 0; i < strArray.Length; i = (byte)(i + 1))
                {
                    message.To.Add(strArray[i]);
                }
                string[] StrCCArray = textBox3.Text.Split(new char[] { ',' });
                 string[] StrBCCArray = textBox4.Text.Split(new char[] { ',' });
                for (byte c = 0; c < StrCCArray.Length; c = (byte)(c + 1))
                {
                    message.CC.Add(StrCCArray[c]);
                    message.Bcc.Add(StrBCCArray[c]);
                }
                client.Port = Convert.ToInt32(this.var4);
                client.Credentials = new NetworkCredential(this.var2, this.var3);
                client.EnableSsl = Convert.ToBoolean(this.var5);              
                message.Subject = textBox2.Text;
                message.Body = richTextBox1.Text;
                Byte ii = 0;
                if (listBox1 .Items .Count !=0)
                {
                    for (ii = 0; ii < listBox1.Items.Count; ii++)
                        message.Attachments.Add(new Attachment(listBox1.Items[ii].ToString()));
                }
                message.IsBodyHtml = true;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
             
                this.progressBar1.Visible = true;
                int num2 = 0;
                num2 = 5;
                this.progressBar1.Maximum = 5;
                this.progressBar1.Value = 0;
                for (int j = 0; j < num2; j++)
                {
                    this.progressBar1.Value++;
                }
                client.Send(message);
                this.progressBar1.Value = 0;
                this.progressBar1.Visible = false;
                MessageBox.Show("Mail has been sent...","Success",MessageBoxButtons .OK ,MessageBoxIcon .Information);
             
            }
            catch (Exception)
            {
                MessageBox.Show("Problem in Mail Id / Mail Server. Check your Email Server Master Setting","!Failed?",MessageBoxButtons .OK ,MessageBoxIcon .Error);
            }
            finally
            {
                Cursor.Current = Cursors.Arrow;
            }
        }


        private void btnSendMail_Click(object sender, EventArgs e)
        {
            LoadEmail();
            sendmail();

        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            MultiShop.FrmAttachment frm = new MultiShop.FrmAttachment();
            frm.ShowDialog(this);
            if (frm.txtFile.Text.Trim() != "")
                listBox1.Items.Add(frm.txtFile.Text);
            frm.Dispose();
        }

        private void BtnRemove_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            richTextBox1.Text = "";
            listBox1.Items.Clear();
        }

        private void FrmDemo_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel =true;
            this.Hide();

        }
    }
}