0

I am using Exec msdb.dbo.sp_send_dbmail in class file using string builder to append the from and To address.NowI need to attache the file which will be in Local solution folder (Within the solution I have Created the folder)

          strSql.Append("Exec msdb.dbo.sp_send_dbmail @profile_name='SLAToolProfile',");strSql.Append("@recipients='");
        strSql.Append(eMailToaddresses);

        strSql.Append("',@file_attachments='");
        string Path = "~/Material/study material.doc";
        strSql.Append(Path);

when I using the above format I got file attached Error .How do I mention the path which should be in local folder within the solution?

4

2 回答 2

0

用这个:

string filename = "study material.doc";
string storePath = Server.MapPath("~/Material");
string finalPath = Path.Combine(storePath,filename);

Server.MapPath()将您的虚拟路径转换为绝对应用程序路径;

于 2012-04-30T08:36:16.053 回答
0

您需要使用 VirtualPathUtility.ToAbsolute 方法:

strSql.Append("Exec msdb.dbo.sp_send_dbmail @profile_name='SLAToolProfile',");strSql.Append("@recipients='");
        strSql.Append(eMailToaddresses);

        strSql.Append("',@file_attachments='");
        string Path = VirtualPathUtility.ToAbsolute("~/Material/study material.doc");
        strSql.Append(Path);

那应该工作

http://msdn.microsoft.com/en-us/library/ms150160(v=vs.90).aspx

更新

好吧,做了更多的挖掘,事实证明,当您使用 sp_send_dbmail 时,附件位置是相对于数据库服务器的位置,而不是运行代码的机器。因此,您遇到的第一个问题是使数据库服务器可以访问此文件位置,即使用文件共享和 UNC 位置。所以你不能使用~. Db 服务器无法理解您的路线在哪里......!

于 2012-04-30T08:25:14.097 回答