Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
            {
                System.IO.StreamWriter sw_In = new System.IO.StreamWriter(@"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls");

 while (reader.Read())
                {
                    if (count == 0)
                    {
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
    MailMessage mis = new MailMessage();
                            SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
                            smtpserver.Credentials = new System.Net.NetworkCredential("emailaddress@example.com","<password>");
    smtpserver.Host = "smtp.gmail.com";
                            smtpserver.Port = 587;
                            smtpserver.EnableSsl = true;
                            mis.From = new MailAddress("rajesh@gmail.com", "Report");
                            mis.IsBodyHtml = true;
                            mis.To.Add("rajesh@gmail.com");
                            mis.CC.Add(new MailAddress("rajesh@gmail.com"));
                            mis.Subject = "Data Dump Report";
                            smtpserver.Send(mis);
                            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");
                }
            }
            sqlConnection.Close();
            reader.Close();


in desktop under the name of Data Dump folder excel file will be download.

i am sending that excel file to mail.

for that sending mail, i written the code above.


Mail function code as follows

  MailMessage mis = new MailMessage();
                            SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
                            smtpserver.Credentials = new System.Net.NetworkCredential("rajesh@gmail.com","12345");
    smtpserver.Host = "smtp.gmail.com";
                            smtpserver.Port = 587;
                            smtpserver.EnableSsl = true;
                            mis.From = new MailAddress("rajesh@gmail.com", "Report");
                            mis.IsBodyHtml = true;
                            mis.To.Add("rajesh@gmail.com");
                            mis.CC.Add(new MailAddress("rajesh@gmail.com"));
                            mis.Subject = "Data Dump Report";
                            smtpserver.Send(mis);

</ore>
but when i run the above code, in mail i getting only subject as Data Dump Report.

The excel file is i am not getting in mail.

please help me what is the mistake in my above code

What I have tried:

i tried several times to send  excel file to mail using c#

When i run the above code, in mail i getting only subject as Data Dump Report.

The excel file is i am not getting in mail.

please help me what is the mistake in my above code
Posted
Updated 27-Aug-16 22:43pm
v3
Comments
Afzaal Ahmad Zeeshan 28-Aug-16 4:34am    
Do not share the email or possible password combination online. Use sample text like I did, testers will input their own credentials to test the code. This would only expose you to spammers, or hackers.

1 solution

In the code that you show,
C#
MailMessage mis = new MailMessage();
                            SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
                            smtpserver.Credentials = new System.Net.NetworkCredential("rajesh@gmail.com","12345");
    smtpserver.Host = "smtp.gmail.com";
                            smtpserver.Port = 587;
                            smtpserver.EnableSsl = true;
                            mis.From = new MailAddress("rajesh@gmail.com", "Report");
                            mis.IsBodyHtml = true;
                            mis.To.Add("rajesh@gmail.com");
                            mis.CC.Add(new MailAddress("rajesh@gmail.com"));
                            mis.Subject = "Data Dump Report";
                            smtpserver.Send(mis);

Where exactly are you attaching the Excel file? Secondly,
Quote:
when i run the above code, in mail i getting only subject as Data Dump Report.
Where exactly, are you adding anything other than the subject? The body is missing, attachments are missing and that is why you don't get anything other than that.

Start by adding the following lines,
C#
mis.Body = "<p>This email contains attachment for an Excel spreadsheet.</p>";
mis.Attachments.Add(new Attachment("C:\Path\To\ExcelFile.xls"));

// Send this.

Finally, the email would be sent using the body content and the attachments attached to the email.

For more, please read:
Attachment Constructor (String) (System.Net.Mail)[^]
MailMessage.Attachments Property (System.Net.Mail)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900