附加自动生成的PDF在asp.net应用到电子邮件 [英] Attaching auto generated pdf to email in asp.net app

查看:258
本文介绍了附加自动生成的PDF在asp.net应用到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常具体的要求。在我的web应用程序,我不得不产生从数据库中值的PDF格式的发票,以及电子邮件正文。我可以轻松地发送这种使用SMTP这完美的作品。

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.

不过,问题在于,我们不能依赖系统始终是完美的,这是发票。因此,我们需要打开而不是使用SMTP默认邮件客户端。现在,我有以下code

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:testmail@gmail.com?body=Test Mail" + "&attachment=" + emailAttachment + "');";
//Register the script for post back
ClientScript.RegisterStartupScript(this.GetType(), "mailTo", emailJS, true);

这完全打开电子邮件,但没有附件是否正常。路径应该是 /Web/Temp/123.pdf

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);

因此​​,很明显文件存在,但它存在于服务器上。展望另一方面客户端机器上打开。所以,我不能使用像 C时完全确定的路径:网络\\ TEMP \\ 123.pdf \\。如果我尝试,它会尝试找到客户机,在该文件夹本身可能不存在的文件。

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。不,我不能直接发送电子邮件。这将导致在今后的一个地狱很多我的问题。

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

编辑:
我还发现了一个奇怪的问题。如果我在附件中添加双引号的文件路径,一个\\自动添加。 @&放大器;附件=+使用Server.Mappath(emailAttachment)+ @,);给我的输出为&安培;附件= \\C:\\网络\\ 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.

编辑:
我试图在本地主机上固定的路径。所以,我基本上测试,其中file是获得存储在同一台机器上的应用程序。尽管如此,没有附着在所有。

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: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);

更新的路径,以确保它是正确的。现在,它只是抛出错误说命令行参数无效

编辑:

是否有任何其他方法我可以尝试?我已经在服务器端的文件路径。也许我可以自动将文件下载到客户端机器上一些默认的文件夹,并从那里开?这可能吗?

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?

编辑:我想多一个选择。

I tried one more option.

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

的Process.Start 行工作,但它确实什么都没有。没有过程,开始时,没有任何错误。

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

编辑:
好极了。我终于得到了用户使用一个单独的形式来显示主题和正文,而不是打开默认邮件客户端批准。虽然,我仍然preFER为的就是解决这个问题。

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.

推荐答案

所以,这里的问题是,的mailto 仅支持直接连接文件路径的事实。即,路径必须是本地网络内使用的机器,或企业内部网的路径。

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.

在换句话说,像 HTTP路径://yourapp/Web/Temp/123.pdf 将无法正常工作,而 /网络/温度/ 123.pdf 基本上是相同的将不能工作。这些都不是路径,但链接到的文件已被下载并存储在本地它们可以被用来作为附件前 - 至mailto协议具有用于不支持

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.

不过,由于你的应用是企业内部网,你可以做的就是确保预期的用户才能访问服务器上的一些网络共享文件夹,然后为他们提供网络路径的文件,即 \\\\ theserver \\文件\\ 123.pdf

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

这篇关于附加自动生成的PDF在asp.net应用到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆