You must Sign In to post a response.
  • Category: ASP.NET

    Mail is working but i want send two excel file to different person

    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;
    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");
    count = 1;
    for (int i = 0; i < reader.FieldCount; i++)
    sw_In.AutoFlush = true;
    sw_In.Write(reader[i].ToString() + "\t");
    MailMessage mis = new MailMessage();
    mis.From = new MailAddress("","Report");
    mis.IsBodyHtml = true;
    mis.Subject = "Data Dump Report";
    mis.Attachments.Add(new Attachment(filePath, "application/"));
    SmtpClient smtpserver = new SmtpClient("");

    smtpserver.Credentials = new System.Net.NetworkCredential("", "Password");
    smtpserver.Host = "";
    smtpserver.Port = 587;
    smtpserver.EnableSsl = true;

    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 to this mail.

    But i want 1Excel file to be sent to

    And 2Excel file to be sent to

    In my above code both 1Excel and 2Excel file getting mail to

    For that what changes need to be done in my code.

    Please help me.
  • #767646
    You can check value of j and send excel to desired user as follows:

    if (j == 1)

  • Sign In to post your comments