.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

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

Posted By: Rao     Posted Date: August 28, 2016    Points:5   Category :C#
I tried my code as follows,

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("firstmailid","Report");
mis.IsBodyHtml = true;
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("firstmailid", "Password");
smtpserver.Host = "smtp.gmail.com";
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 [mail address 1 removed] to this mail.

But i want 1Excel file to be sent to Firstmailid

And 2Excel file to be sent to Secondmailid

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

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

Please help me

Author: david stephan             
Posted Date: January 06, 2017     Points: 20   

To be exact, you are writing CSV (Comma Separated Values) text files, except the delimiter isn't a common, it's a tab. Just because you gave the files an ".xls" extension does not make it an Excel file.

You need to separate this code into distinct methods. The concept is called "separation of concerns". Right now, you have a monolithic method that does everything. Break this out into methods that do one thing and one ting only. A method that exports certain data to a CSV file. A method that assembles an email message. A method that sends the email message. ... and whatever else you need.

Once you have this broken out, it's pretty easy to use the code over and over again to send emails to as many people as you want with any content you want.

Also you can contact to: http://www.outsourcing4work.de/en/software/dotnet-developer/ for best resolutions.

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend