10

I am dynamically generating a number of different types of files based upon a GridView in ASP.NET - an Excel spreadsheet and a HTML file. I am doing so using this code (this is just for the Excel spreadsheet):

  Response.Clear();
  Response.AddHeader("content-disposition", "attachment;filename=InvoiceSummary" + Request.QueryString["id"] + ".xls");
  Response.Charset = "";

  Response.ContentType = "application/vnd.xls";
  System.IO.StringWriter stringWrite = new System.IO.StringWriter();
  System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
  contents.RenderControl(htmlWrite);
  //GridView1.RenderControl(htmlWrite);
  Response.Write(stringWrite.ToString());
  Response.End();

I would like to give users the options of emailing the generated file as an attachment to either an email address they specify or one linked with their account on the database. But I don't want the user to have to save the file, then attach it in a form - I'd like to automatically attach the generated file. Is this possible, and how easy is it?

Of course, I'll be using the System.Net.Mail class to send mail...if it's possible anyway!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chris
  • 7,415
  • 21
  • 98
  • 190
  • The MailMessage class has an Attachments property that will do this. Just create a new Attachment (can be based on a MemoryStream) and then attach (e.g. MailMessage.Attachments.Add()). – Zachary Nov 29 '10 at 20:46

4 Answers4

9

You might be able to create System.Net.Mail.Attachment from string then send out the mail as normal.

var m = new System.Net.Mail.MailMessage(from, to, subject, body);
var a = System.Net.Mail.Attachment.CreateAttachmentFromString(stringWrite.ToString(), "application/vnd.xls");
m.Attachments.Add(a);
tonyjy
  • 1,359
  • 3
  • 12
  • 24
  • I would like to create the generated stringbuilder (which is done) to an attachment to outlook and wait for the user to press Send. I keep getting an error. – Si8 Feb 22 '16 at 14:45
1
    protected void btnSend_OnClick(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        byte[] data = new byte[1024];
        MemoryStream stream = new MemoryStream(data);
        Attachment attach = new Attachment(stream, "Attachment file name");
        mail.Attachments.Add(attach);
        new SmtpClient().Send(mail);
    }
Sid C
  • 67
  • 2
0

You can save the file contents into a byte array and then do this:

Creating In-Memory Mail Attachments

A G
  • 21,087
  • 11
  • 87
  • 112
  • Negating Aliostad's -1. As I read the question, Chris wants to send the mail to the user from his server, and this is the correct way to do it. System.Net.Mail works the same from a desktop or a web application. – Jess Nov 29 '10 at 20:24
  • @Aliostad: So whats the problem? File generation is server side, so I dont think this should be a problem. – A G Nov 29 '10 at 20:24
  • I still believe this is supposed to be done at the client. But I tried to revert my -1 anyway bit but it doesnt allow me. – Aliostad Nov 29 '10 at 20:27
0

Here is an working example of what I mentioned earlier, there is a little extra logic in the code to parse the GridView to a Table.

        //Table to store our GridView Data
        Table table = new Table();

        //Parse our GridView into Table, stored in a StringBuilder
        StringBuilder sb = new StringBuilder();
        using (StringWriter sw = new StringWriter(sb))
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  header
                if (GridView1.HeaderRow != null)
                {
                    table.Rows.Add(GridView1.HeaderRow);
                }

                //  details
                foreach (GridViewRow row in GridView1.Rows)
                {
                    table.Rows.Add(row);
                }

                //  footer
                if (GridView1.FooterRow != null)
                {
                    table.Rows.Add(GridView1.FooterRow);
                }

                //  render table
                table.RenderControl(htw);
            }
        }


        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (StreamWriter writer = new StreamWriter(memoryStream))
            {
                //Convert StringBuilder to MemoryStream
                writer.Write(sb.ToString());
                writer.Flush();

                //Create Message
                MailMessage message = new MailMessage();
                message.To.Add(new MailAddress("you@address.com", "You"));
                message.From = new MailAddress("me@address.com", "Me");
                message.Subject = "The Subject";

                //Create Attachment
                Attachment attachment = new Attachment(memoryStream, "InvoiceSummary.xls", "application/vnd.xls");

                //Attach Attachment to Email
                message.Attachments.Add(attachment);

                //Open SMTP connection to server and send
                SmtpClient smtp = new SmtpClient();
                smtp.Port = 25;
                smtp.Send(message);
            }
        }
Zachary
  • 6,522
  • 22
  • 34