C# 发送带有 html 链接的电子邮件到文件 - 无法识别的转义序列

标签 c# html

我有一个正在发送电子邮件的 C# 应用程序。我希望在邮件中发送一个链接,供用户单击并打开文件。

但是,在我有指向我的工作簿的链接的地方,我遇到了问题。我有 5 个错误都是相同的“无法识别的转义序列”,其中每个“/”都是。我该如何解决这个问题?

string htmlHeader = "<table style='font-size: 12pt;'>" +
            "<tr><a href='file:///G:\Shared\Team\New\Corporate%20Actions\Corp%20Events.xlsx'>Corp Events Workbook></tr><tr/><tr/>" +
            "<tr><th align='left'>Status</th><th>&nbsp;</th>" +
            "<th align='left'>Sedol</th><th>&nbsp;</th>" + 
            "<th align='left'>Name</th><th>&nbsp;</th>" + 
            "<th align='left'>Date Effective</th><th>&nbsp;</th>" + 
            "<th align='left'>Event Code</th><th>&nbsp;</th>" + 
            "<th align='left'>Terms</th><th>&nbsp;</th></tr>";

最佳答案

C# 使用字母 \ 后跟另一个字母来转义字符,例如:换行符转义:\n。由于 C# 中没有 \S 转义字符(参见此处的列表:http://msdn.microsoft.com/en-us/library/h21280bw.aspx),编译器无法解析它。为了解决这个问题,我们使用 \\,转义序列后跟反斜杠,这样编译器就会知道您要打印 \

例子:

string htmlHeader = "<table style='font-size: 12pt;'>" +
            "<tr><a href='file:///G:\\Shared\\Team\\New\\Corporate%20Actions\\Corp%20Events.xlsx'>Corp Events Workbook></tr><tr/><tr/>" +
            "<tr><th align='left'>Status</th><th>&nbsp;</th>" +
            "<th align='left'>Sedol</th><th>&nbsp;</th>" + 
            "<th align='left'>Name</th><th>&nbsp;</th>" + 
            "<th align='left'>Date Effective</th><th>&nbsp;</th>" + 
            "<th align='left'>Event Code</th><th>&nbsp;</th>" + 
            "<th align='left'>Terms</th><th>&nbsp;</th></tr>";

注意第二行,在路径部分,有一个双左反斜杠而不是一个。

关于C# 发送带有 html 链接的电子邮件到文件 - 无法识别的转义序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22169832/

相关文章:

c# - MySql 和 Entity Framework 代码优先

c# - ConfigureAwait(true) 没有返回等待它的上下文

html - Aptana 3 的状态和文档?

javascript - 如果所有元素(表格)的宽度与某些(200)像素相同,如何隐藏它们?

javascript - jquery 的 if else 语句使用选择器来查看某个 div 是否已附加到它

c# - 如何处理 x86 与 x64 包

c# - 使用 Xamarin 表单录制和播放音频

c# - GSM 调制解调器未响应 30 秒(短信应用程序)

javascript - 给定原始图像分辨率,缩放后如何使用javascript计算新分辨率?

css - HTML5 视频 : full screen without borders?