Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching auto generated pdf to email in asp.net app

Tags:

c#

asp.net

I have a very specific requirement. In my web app, I have to generate a pdf invoice from the database values, and an email body. I can easily send this using SMTP which works perfect.

But, problem is we can't rely on system to always be perfect, and this is an invoice. So, we need to open the default mail client instead of using SMTP. Right now, I have following code

//Code to create the script for email
string emailJS = "";
emailJS += "window.open('mailto:[email protected]?body=Test Mail" + "&attachment=" + emailAttachment + "');";
//Register the script for post back
ClientScript.RegisterStartupScript(this.GetType(), "mailTo", emailJS, true);

This opens the email perfectly, but no attachment is working. Path is supposed to be /Web/Temp/123.pdf.

If I use the same path as normal url like below, it opens the file in new window properly.

ClientScript.RegisterStartupScript(this.GetType(), "newWindow", "window.open('/Web/Temp/123.pdf');", true);

So, clearly file exists, but it exists on the server. Outlook on the other hand open on client machine. So, I can't use the full determined path like C:\Web\Temp\123.pdf. If I try that, it will try to find the file on client machine, where the folder itself might not exist.

I am trying to figure out what can I do here. If there is another method I should try.

P.S. No, I can't send the email directly. That will cause a hell lot more problem in future for me.

Edit: I also found one weird problem. If I add a double quote to the file path in attachment, a \ is added automatically. @"&attachment=""" + Server.MapPath(emailAttachment) + @"""');" gives me output as &attachment=\"C:\Web\Temp\123.pdf\".

I am trying to escape that double quote and somehow it adds that slash. I know this is a completely different problem, but thought I should mention here, instead of creating a new question.

Edit: I tried a fixed path on localhost. So, I am basically testing the app on the same machine where file is getting stored. still, no attachment at all.

string emailJS = "";

emailJS += @"window.open('mailto:[email protected]?body=Test Mail" + emailAttachment + @"&attachment=";
emailJS += @"""D:\Dev\CSMS\CSMSWeb\Temp\635966781817446275.Pdf""');";
//emailJS += Server.MapPath(emailAttachment) + @"');";
//Register the script for post back
ClientScript.RegisterStartupScript(this.GetType(), "mailTo", emailJS, true);

Updated the path to make sure it is proper. Now, it just throws error saying command line argument not valid.

Edit:

Is there any other method I can try? I have the file path on the server side. Maybe I can download the file automatically to some default folder on client machine and open from there? Is that possible?

Edit: I tried one more option.

emailJS += @"mailto:[email protected]?body=Test Mail" + @"&attachment=";
emailJS += @"\\localhost\CSMSWeb\Temp\635966781817446275.Pdf";
//emailJS += Server.MapPath(emailAttachment) + @"');";
Process.Start(emailJS);

The Process.Start line works but it does nothing at all. There is no process that starts, no error either.

Edit: yay. I finally got the user to approve using a separate form to display the subject and body, instead of opening the default mail client. although, I would still prefer to solve this problem as is.

like image 460
jitendragarg Avatar asked Oct 30 '22 05:10

jitendragarg


1 Answers

So, the problem here is the fact that mailto only supports direct file path for attachment. That is, path has to be local to use machine, or intranet path within the network.

In other words, path like http://yourapp/Web/Temp/123.pdf won't work, and /Web/Temp/123.pdf being essentially the same won't work either. These are not paths, but links to files that has to be downloaded and stored locally before they can be used as attachments - mailto protocol has no support for that.

However, since your application is intranet, what you could do is make sure intended users have access to some network shared folder on your server, and then provide them with network path to the file, that is \\theserver\files\123.pdf

like image 138
Andrei Avatar answered Nov 09 '22 22:11

Andrei