Attach an automatically generated PDF to email in asp.net

I have a very specific requirement. In my web application, I need to create an invoice from the database values ​​and email body. I can easily send this using SMTP, which works great.

But the problem is that we cannot rely on the system to always be perfect, and this is an invoice. Therefore, we must open the default mail client instead of using SMTP. Right now, I have the following code

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

This opens the letter perfectly, but the application does not work. The path should be /Web/Temp/123.pdf .

If I use the same path as the regular url, as shown below, it opens the file correctly in a new window.

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

So, the file explicitly exists, but it exists on the server. Outlook, on the other hand, is open on the client machine. Thus, I cannot use the full specific path, for example C:\Web\Temp\123.pdf . If I try this, it will try to find the file on the client machine, where the folder itself may not exist.

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

PS No, I can not send an email directly. This will cause me more problems in the future for me.

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

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

Edit: I tried a fixed path on localhost. So, I mainly test the application on the same machine where the file is stored. still no attachment at all.

 string emailJS = ""; emailJS += @"window.open('mailto: jitendragarg@gmail.com ?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 path to make sure it is correct. Now it just throws an error saying command line argument not valid .

Edit:

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

Edit: I tried another option.

 emailJS += @"mailto: testmail@gmail.com ?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. The process does not start without errors.

Edit: cheers. Finally, I asked the user to approve the use of a separate form to display the object and body, instead of opening the default email client. although, I would still decide to solve this problem as it is.

+5
source share
1 answer

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

In other words, a path like http://yourapp/Web/Temp/123.pdf will not work, and /Web/Temp/123.pdf will be essentially the same, will also not work. These are not paths, but links to files that need to be downloaded and saved locally before they can be used as attachments. The mailto protocol does not support this.

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

+2
source

Source: https://habr.com/ru/post/1247420/


All Articles