Sending mail with attachments programmatically in ASP.NET

六月ゝ 毕业季﹏ 提交于 2019-12-02 21:09:35

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);
    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);
    }

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

Creating In-Memory Mail Attachments

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);
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!