首页 文章

使用url作为电子邮件附件的路径会出错:不支持给定路径的格式

提问于
浏览
-1

我的网络主机上存有PFD . 我需要将此PDF作为附件发送 . 我将attchment =设置为PDF的URL并在VS 2017中运行代码给出了错误:不支持给定路径的格式 . 我尝试使用GetInvalidFileNameChars())修剪URL;和GetInvalidPathChars());但修剪后的网址与原始网址相同 . 我生成错误的代码是:

string MyPDF = "http://happylandings2018.com/free_ebook_ultimate_weight_loss_bible.pdf";
Attachment attachment = new Attachment(MyPDF);

当我将MyPDF设置为等于系统上PDF的路径时 . c:/ ..它工作正常 . 我猜这里必须有一个简单的解决方案,但是找不到任何有用的东西 . 谢谢你的回复!

1 回答

  • 0

    Attachment 构造函数的参数可以是本地文件的路径,但不是URL . 这就是为什么它在您将其设置为系统上的文件时有效 .

    如果文件在您的服务器上,您可以使用 Server.Map() 函数将文件的URL转换为物理路径,然后传递给 Attachement() . 但是,如果您想使用其他网站的文件,则必须先将其下载到您的服务器 . 像这样的东西:

    string MyPDF = "http://happylandings2018.com/free_ebook_ultimate_weight_loss_bible.pdf";
    var stream = new WebClient().OpenRead(MyPDF);  
    Attachment attachement = new Attachment(stream);
    

相关问题