(Print this page)

Sending e-mails with attachments (a different method)
Published date: Friday, July 30, 2010
On: Moer and Éric Moreau's web site

One of the most asked questions on forums is about sending e-mails from an application.

The easiest method from a .Net application is to use the System.Net.Mail namespace as I covered in September 2007.

Another method is to use the mailto protocol using the Process component which I also covered in December 2003.

Outlook automation is also possible but your users will have to reply to a message saying that something is trying to send a message on their behalf (which most users don’t like).

But those methods are not good for in all situations. For example, the first method (System.Net.Mail) is not linked to your Outlook and then the message is not saved into your sent items. Because the RFC document does not support the attach parameter, not every email applications (Outlook once again) can be used to send attachments when using the second method.

Lately, I found a solution that is working great so far. The last problem I solved with it was to add a button to the Crystal Report viewer to easily attach a PDF version of the currently viewed report to a new e-mail.

The origin of this article

I haven’t created the code you will see here. It is freely available from The Code Project web site in an article titled “Programmatically adding attachments to emails in C# and VB.Net” written by David M Brooks. Thanks to David for sharing.

The downloadable demo application

I don’t have a downloadable version this month. Instead, I strongly encourage you to download the source code that goes with the linked article. This demo is available in both VB and C#.

So why is this solution special?

The solution you will find here is perfect whenever you need to create a new email using your default email application (which can use your address book, your editor, your spell checker, ...) and even attach files and all other features like the sent items.

Sure you could be able to reproduce many of those features but not for free!

Full tests required

I want to warn you that you will need to thoroughly test this solution with every combination of Windows and Outlook versions. You will also need to ensure that this solution is still working whenever a new version is released.

The reason is that the trick here is using some undocumented and unpublished features of Outlook that may not work in the future (or may not work right now if you have very strict permissions set).

Conclusion

This method would have been useful to me many times in the last 20 years. Too bad I just found it 2 months ago!


(Print this page)