1

I'm using this button to create the Excel based on a DataGridView:

 private void exportbutton_Click(object sender, EventArgs e){

        if (mesakemGrid.Rows.Count > 0)
        {
            Excel._Application XcelApp = new Excel.Application();
            XcelApp.Application.Workbooks.Add(Type.Missing);

            for (int i = 1; i < mesakemGrid.Columns.Count + 1; i++)
            {
                XcelApp.Cells[1, i] = mesakemGrid.Columns[i - 1].HeaderText;
                Excel.Borders border = XcelApp.Cells[1, i].Borders;
                border.LineStyle = Excel.XlLineStyle.xlContinuous;
                border.Weight = 2d;
            }

            for (int i = 0; i < mesakemGrid.Rows.Count; i++)
            {
                for (int j = 0; j < mesakemGrid.Columns.Count; j++)
                {
                    Excel.Borders border = XcelApp.Cells[i + 2, j + 1].Borders;

                    border.LineStyle = Excel.XlLineStyle.xlContinuous;
                    border.Weight = 2d;
                    XcelApp.Cells[i + 2, j + 1] = mesakemGrid.Rows[i].Cells[j].Value.ToString();
                }
            }
            XcelApp.Columns.AutoFit();
            XcelApp.Visible = true;
        }

    }

I want to send this Excel file to an Email address. Is it possible? I saw in some guide this method, But it requires an Attachment, and i don't know if it's possible to convert Excel._Application to Attachment.

       public void send(System.Net.Mail.Attachment attachment)
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("SMTP_INFO");
        mail.From = new MailAddress("YOUR_EMAIL@XXX.com");
        mail.To.Add("TO_ADDRESS@XXX.COM");
        mail.Subject = "THIS IS THE SUBJECT";
        mail.Body = "THIS IS THE BODY";
        attachment = new System.Net.Mail.Attachment(@"C:\EXCEL_FILE.XLS");
        mail.Attachments.Add(attachment);
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("USERNAME", "PASSWORD");
        SmtpServer.EnableSsl = true;
        SmtpServer.Send(mail);
    }
Hila Gal
  • 149
  • 2
  • 9
  • 1
    Why dont you get the location that you are writing the file to and then add that as an attachment, or attach that object as an attachment – Inept Adept Jul 23 '15 at 12:48
  • Use the Excel Workbook Save method to save the file as *C:\EXCEL_FILE.XLS* then the file will be attached with your email code. – Jeremy Thompson Jul 23 '15 at 12:57
  • @JeremyThompson How can i attach the excel file without saving it? I want to send this email once a day automatically so i have no need to save it before sending. – Hila Gal Jul 23 '15 at 13:07

2 Answers2

1

How can i attach the excel file without saving it?

You can't. Only way I can think of is making the Body of the email contain the data in your DataGridView.

I want to send this email once a day automatically so i have no need to save it before sending.

All dev's that I know would simply save the XLS to a C:\temp location, email it as a file attachment and finally delete the XLS file after the email is sent.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • You should use [`Path.GetTempPath`](https://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx) instead of hardcoding "C:\temp". – Lithis Jul 23 '15 at 13:30
  • OP, obviously follow @Lithis advice, I didn't mean to hard code the c:\temp path. Cheers – Jeremy Thompson Jul 23 '15 at 13:47
  • @JeremyThompson I started learning (All alone..) C# five day's ago. Can you please explain what do i need to write in order to save/delete the Excel._Application i have? – Hila Gal Jul 23 '15 at 14:33
  • Well you seem to be picking it up fast, expect 6 months of this before you really get the hang of things. See this answer to save the Excel file: http://stackoverflow.com/a/16755811 . To delete the XLS file is one line: System.IO.File.Delete(filePath); - good luck! – Jeremy Thompson Jul 23 '15 at 14:39
  • Tip: try and do google searches in a format like: "Technology Class issue", eg "C# Workbook Save" or "C# File delete" – Jeremy Thompson Jul 23 '15 at 14:45
0

Using System.Net.Mail u can easly attach any file you want. So when you are having your excel file created just attach it to email.

Koteczeg
  • 161
  • 2
  • 16
  • How can i attach the excel file without saving it? I want to send this email once a day automatically so i have no need to save it before sending. – Hila Gal Jul 23 '15 at 13:06
  • Is Excel._Application a existing proccess? And it is not saved file? – Koteczeg Jul 23 '15 at 13:12
  • If so, maybe try this: http://stackoverflow.com/questions/23655210/attach-to-existing-excel-instance But so far as I know, the normal thing is to attach file that exists :P – Koteczeg Jul 23 '15 at 13:14
  • Yes, it will be eventually (When i'll find the way :) ) – Hila Gal Jul 23 '15 at 13:16
  • Hmm, don't know why you got a downvote... I'll fix that. – Jeremy Thompson Jul 23 '15 at 13:48
  • 1
    Because this is not an answer, but a comment. And it misses the question, since the OP asked for a solution to attach the excel document to the email without saving it. So its a very low quality answer which does not answer the question. And, in the code shown in the question, usage of `System.Net.Mail` is already made. So this is also redundant. Please don't benefit such poor answers by upvoting them. Upvotes are for very good answers, which are well researched, pointy and - most important - **helpful** – Patrik Jul 24 '15 at 08:07