How to send email through Asp .net email id get from excel ?
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....
Dorababu MekaPosted Jan 16, 2013, 7:12 AM
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();
}