Forum guidelines
AuthorQuestion
How to send email through Asp .net email id get from excel ?
Posted on: 16 Jan 2013
website2.rar what I want to do? i want to send email to all the person who's mail id enter in my excel sheet i want to send mail to all the address at a same time & no body can see their mail id each other in their mail. I want to use BCC concept here....

AuthorReply
Re: How to send email through Asp .net email id get from excel ?
Posted on: 16 Jan 2013  
form4.rar Try this, attached code was done in Windows application change to web code is almost all same except open file dialog, use file upload if u used that here

declaration

[DllImport("user32.dll")]
        private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr ProcessId);
        private static string connectionString;
        private string connectionString1;
        private string FileName;
        public string[] workSheetNames1 = new string[] { };
       
        private DataSet excelDataset;


        private OleDbConnection oledb_con;
        private OleDbDataAdapter oledb_Da;

string[] splitByDots = FileName.Split(new char[1] { '.' }); // FileName is your excel file path


            Microsoft.Office.Interop.Excel.Application ExcelObj = null;
            ExcelObj = new Microsoft.Office.Interop.Excel.Application();


            if (splitByDots[splitByDots.Length - 1] == "xls")
                OpenExcelFile(false);


            if (splitByDots[splitByDots.Length - 1] == "xlsx")
                OpenExcelFile(true);


public void OpenExcelFile(bool isOpenXMLFormat)
        {
            string EmailID = string.Empty;
            workSheetNames1 = new String[1];
            OleDbConnection con;


            if (isOpenXMLFormat)
            {
                connectionString1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                    FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
            }
            else
            {
                connectionString1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                    FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";


                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                    FileName + ";Extended Properties=Excel 8.0;";
            }


            con = new OleDbConnection(connectionString);
            con.Open();


            System.Data.DataTable dataSet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            foreach (DataRow row in dataSet.Rows)
            {
                workSheetNames1[0] = row["TABLE_NAME"].ToString().Trim('$');
                if (workSheetNames1[0].ToString().Contains("$"))
                    workSheetNames1[0] = workSheetNames1[0].Replace("$", "");
                workSheetNames1[0] = workSheetNames1[0].Replace("'", "");
                break;
            }
           
            //int i = 0;
           
            foreach (string sheetName in workSheetNames1)
            {
                oledb_Da = new OleDbDataAdapter();
                excelDataset = new DataSet();
                oledb_con = new OleDbConnection(connectionString);
                if (oledb_con.State == ConnectionState.Closed)
                    oledb_con.Open();
                oledb_Da = new OleDbDataAdapter("select * from [" + sheetName + "$]", oledb_con);
                oledb_Da.Fill(excelDataset);
            }
            if (excelDataset.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < excelDataset.Tables[0].Rows.Count; i++)
                {
                    EmailID = excelDataset.Tables[0].Rows[i][2].ToString();
// your email code for BCC
                    MailMessage mail = new MailMessage();
                    mail.bcc.Add(EmailID); // this is a sample code code the actual one for BCC here      
                }
            }


            if (con != null)
            {
                con.Close();
                con.Dispose();
            }


            if (dataSet != null)
                dataSet.Dispose();
        }


Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.