Tuesday, April 23, 2013

Send Mail In New Thread Using Background Worker in C#

using System.Net.Mail;

public static void DoMails( string Msg)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler (worker_DoWork);
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = false;
            worker.RunWorkerAsync(Msg);//passing argument
        }

        private static void worker_DoWork( object sender, DoWorkEventArgs e)
        {
            DoMail(e.Argument.ToString());
        }

private static void DoMail( string Msg)
        {
            string MailFrom = "Abc@gmail.com"; //from Mail id
            string MailTo = "Xyz@yahoo.com";//to mail id
            string SmtpHostServer = "smtp.gmail.com"; // your server
            string MailUserID = MailFrom;
            string MailPassword = From mail id password;
            string MailSubject = "Hello";//your subject
            string MailMsg = Msg; //your msg
            string MailPort = Properties.Settings .Default.Port;
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient(SmtpHostServer);

                mail.From = new MailAddress (MailFrom);
                mail.To.Add(MailTo);
                mail.Subject = MailSubject;
                mail.Body = MailMsg;
                //if you have attachment file then decomment below code
                //if (File.Exists(TxtAttechment.Text))
                //{
                //    Attachment FileAttachments = new Attachment(TxtAttechment.Text);
                //    mail.Attachments.Add(FileAttachments);
                //}

                SmtpServer.Port = "956";//your server port
                SmtpServer.Credentials = new System.Net.NetworkCredential (MailUserID, MailPassword);
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
            }
            catch
            {
                //MessageBox.Show("Mail Not Send : " + ex.Message.ToString());               
            }
        }

9 comments:

  1. Ankur Bhargav'S C Bolg >>>>> Download Now

    >>>>> Download Full

    Ankur Bhargav'S C Bolg >>>>> Download LINK

    >>>>> Download Now

    Ankur Bhargav'S C Bolg >>>>> Download Full

    >>>>> Download LINK HA

    ReplyDelete