<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <iframe src="http://www.blogger.com/navbar.g?targetBlogID=8211560&amp;blogName=Tech+Tips,+Tricks+%26+Trivia&amp;publishMode=PUBLISH_MODE_BLOGSPOT&amp;navbarType=BLUE&amp;layoutType=CLASSIC&amp;searchRoot=http://mvark.blogspot.com/search&amp;blogLocale=en_IN&amp;v=1&amp;homepageUrl=http://mvark.blogspot.com/&amp;vt=-5147029996388199615" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" height="30px" width="100%" id="navbar-iframe" allowtransparency="true" title="Blogger Navigation and Search"></iframe> <div></div>

Tech Tips, Tricks & Trivia

by 'Anil' Radhakrishna
A seasoned developer's little discoveries and annotated bookmarks.

Search from over a hundred HOW TO articles, Tips and Tricks


HOW TO send an email with a Word or Excel file attachment built on the fly

This question was asked on a Forum and the poster faced a situation where some hundred invoices had to be sent as an attachment to different receipients each pre-filled with user specific data retreived from the database. Creating these invoices manually, saving the files and picking them up programmatically to be emailed was the obvious time-consuming solution. It also carries the additional overhead of having to delete the files after emailing those attachments.

I remembered reading that with System.Net.Mail, attachments could be created from a stream. Now there is also a cool re-usable method "DataTable2ExcelString" which can be used to generate an Excel sheet (or Word document) programmatically. I packed both of these ideas into a quick and dirty code snippet as a proof of concept.

The email with the dynamically generated attachment can be copied to the sender's address also if proof of delivery or archiving is required.


Related links:
HOW TO implement "Download as Word/Excel" functionality through a web page
HOW TO add a header or footer to a dynamically generated Word document
HOW TO generate a Word document dynamically with user submitted text formatted with Free Text Box
HOW TO send an email programmatically using C# with GMail

Labels: , ,

« Home | Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »

»

Post a Comment