2
Answers

Mail is working but i want send two excel file to eachperson

narasiman rao

narasiman rao

8y
309
1
 My code is working, But i want the First excel send to one person and another excel to send to another person.
 
My code as follows
 
 
int count = 0;
string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
SqlConnection sqlConnection = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
DataSet ds = new DataSet();
cmd.CommandText = "select * from Empdetails";
cmd.CommandText += " where shifttype = @par";
cmd.Parameters.Add("@par",SqlDbType.Int).Value = j;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
   string filePath = @"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls";
    System.IO.StreamWriter sw_In = new System.IO.StreamWriter(filePath);
while (reader.Read())
{
if (count == 0)
{
for (int i = 0; i < reader.FieldCount; i++)
{
sw_In.AutoFlush = true;
sw_In.Write(reader.GetName(i) + "\t");
}
sw_In.Write("\n");
count = 1;
}
for (int i = 0; i < reader.FieldCount; i++)
{
sw_In.AutoFlush = true;
sw_In.Write(reader[i].ToString() + "\t");
}
sw_In.Write("\n");
}
sw_In.Dispose();
MailMessage mis = new MailMessage();
mis.From = new MailAddress("[email protected]","Report");
mis.IsBodyHtml = true;
mis.To.Add("[email protected]");
mis.Subject = "Data Dump Report";
mis.Attachments.Add(new Attachment(filePath, "application/vnd.ms-excel"));
SmtpClient smtpserver = new SmtpClient("smtp.gmail.com"); 
 
smtpserver.Credentials = new System.Net.NetworkCredential("[email protected]", "Password");
smtpserver.Host = "smtp.gmail.com";
smtpserver.Port = 587;
smtpserver.EnableSsl = true;
smtpserver.Send(mis);
}
reader.Close();
sqlConnection.Close();
 
 
When i run the above in desktop C folder 1Excel and 2Excel to be download in that folder.
 
I am sending the both the excel file to mail.
 
That excel file is getting a mail no problem.
 
i am getting a both  1Excel and 2Excel to [email protected] to this mail.
 
 
But i want  1Excel file to  be sent to  [email protected].
 
And 2Excel file to be sent to  [email protected].
 
 
In my above code both 1Excel and 2Excel  file getting mail to  [email protected].
 
For that what changes need to be done in my code.
 
Please help me.
 
 
 
 
 
 
 

Answers (2)