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

    I Tried several times but i am not getting the excel file in mail

    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("narasiman1986@gmail.com", "narasiman123");
    smtpserver.Host = "smtp.gmail.com";
    smtpserver.Port = 587;
    smtpserver.EnableSsl = true;
    mis.From = new MailAddress("narasiman1986@gmail.com", "Report");
    mis.IsBodyHtml = true;
    mis.To.Add("narasiman1986@gmail.com");
    mis.CC.Add(new MailAddress("narasiman1986@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("id","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);


    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#

    ut 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
  • #767647
    Hi,
    Try using Attachments.Add() statement as follows:

    MailMsg.Attachments.Add(new Attachment(AttachmentPath, "application/TypeOfAttachment"));

    AttachmentPath= Path of your file
    TypeOfAttachment= Just use ms-excel to send excel file as attachment.

  • #767652
    Hi

    Mention your code for Attachment File.Try this Code



    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your mail@gmail.com");
    mail.To.Add("to_mail@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/xlfile.xls");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);


    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #767653
    Hi

    you can also refer this url


    "codeproject.com/Tips/165548/C-Code-snippet-to-send-an-Email-with-attachment-fr"

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #767678
    You are missing the attachment because, you are not attaching the file in the "mis" properly. Try the following code and check it

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("your attachment file");
    mis.Attachments.Add(attachment);

    By Nathan
    Direction is important than speed


  • Sign In to post your comments