Our winforms application has long allowed a “print” option which basically uses RDLC.
The customer has requested that we add a feature allowing users to send the “printed” output via email.
Now, we know that an EMF file is created (in the TEMP folder) as a sort of hidden byproduct of our current printing process.
Seems to us we can simply grab this EMF file and attach it to a new email and the job is done.
- Is this the best option?
- Can we rely on an EMF file be opened by any Windows machine?
- How we identify the EMF file? … just seems to be named
%TEMP%\DiaryGrid_1.emfcurrently. OK so DiaryGrid is the name of our RDLC file but the _1 gets added somewhere along the way.
I did it before. I did it exporting programatically the report to a pdf to a specific location, then we email the pdf file and delete it. I will try to find the code for you (Not in home now)
EDITED:
Sorry for the later. Now i’m in home and I will give you some code blocks that I think will give you some help to acomplish your task. I will include some comments to the code so you can understand some things that are specific in my project. This code are tested and are working well in my clients, but i’m sure that it can be improved. Please, let me know if you can improve this code 😉
First of all, we will export the report to pdf.
Now, we need a class that control the Mail system. Every mail system has their own caracteristics, so maybe you will need modify this class. The behaviour of the class is simple. You only need to fill the properties, and call the Send method. In my case, windows don’t let me delete the pdf file once I send it (Windows says the file is in use), so I program the file to be deleted in the next reboot. Take a look to the delete method. Please, note that the send method use a cutom class named MailConfig. This is a small class that has some config strings like Host, User Name, and Password. The mail will be send using this params.
Now, you can create a form to ask for the destinataries, add some validation, etc, return to you an instance of the Mail class… or you can simply “hard code” the values and fill the class.
Here is the code that I use in a button to call this form, in my example it is named SendMailView.
In this example senMailView.CurrentItem is the instance of the mail class. We simply need to call to the Send methis and the work is done.
This is the largest answer I ever wrote in SO… I hope it help you 😀 If you have any problem using it, call me. By the way, i’m not very proud of my english, so forgive me if the text has any mistake.