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;
    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("rajesh@gmail.com","Report");
    mis.IsBodyHtml = true;
    mis.To.Add("rajesh@gmail.com");
    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("rajesh@gmail.com", "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 rajesh@gmail.com to this mail.


    But i want 1Excel file to be sent to rajesh@gmail.com.

    And 2Excel file to be sent to karthik@gmail.com.


    In my above code both 1Excel and 2Excel file getting mail to rajesh@gmail.com.

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

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

    if (j == 1)
    {
    mis.To.Add("rajesh@gmail.com");
    }
    else
    {
    mis.To.Add("karthik@gmail.com");
    }


Sign In to post your comments