如何访问 HttpUtility.ParseQueryString 返回的 NameValueCollection 中的集合?
使用 C#、.net 4.0
我使用 HttpUtility.ParseQueryString 来解析 mailto URI,如下所示:
MailMessage message = new MailMessage();
NameValueCollection mailTo = System.Web.HttpUtility.ParseQueryString(mailToUri.OriginalString.Split('?')[1]);
message.Subject = mailTo["subject"];
URI 可以有多个“to”参数,例如:
mailto://[email protected]&[email protected]&[email protected]&subject=test&body=this%20should%20be%20the%20body"
单步执行代码时,我可以看到 ParseQueryString 创建了 mailTo["to "] NameValueCollection 包含具有多个值的单个“to”键,它似乎是类型的对象System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry
我确信我错过了一些简单的东西。我无法弄清楚如何迭代这个集合并获取每个单独的值。我正在寻找这样的东西,这显然行不通:
foreach (String address in mailTo["to"])
{
message.To.Add(new MailAddress(address));
}
此外,如果你知道从 mailto URI 到 SMTP 消息的更好方法,我会全力以赴。
Using C#, .net 4.0
I'm using HttpUtility.ParseQueryString to parse a mailto URI, as such:
MailMessage message = new MailMessage();
NameValueCollection mailTo = System.Web.HttpUtility.ParseQueryString(mailToUri.OriginalString.Split('?')[1]);
message.Subject = mailTo["subject"];
The URI can have multiple "to" parameters, such as:
mailto://[email protected]&[email protected]&[email protected]&subject=test&body=this%20should%20be%20the%20body"
When stepping through the code I can see ParseQueryString creates the mailTo["to"] NameValueCollection that contains a single "to" key with multiple values, which appears to be an object of type System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry
I'm sure I'm missing something simple. I haven't been able to figure out how to iterate over this collection and get each individual value. I'd be looking for something like this, which obviously doesn't work:
foreach (String address in mailTo["to"])
{
message.To.Add(new MailAddress(address));
}
Additionally, if you know a better way to go from mailto URI to SMTP message I'm all eyes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要回答您的问题而不提供替代解决方案,请尝试以下操作:
To answer your question without offering an alternative solution, try this:
NameValueCollection 的值部分是一个字符串。因此,对于“to”键,它将是一个以逗号分隔的电子邮件地址字符串。
结果如下
the value part of the NameValueCollection is a string. So for the "to" key it would be a comma delimited string of email addresses.
results in the following
mailTo["to"] 只是一个字符串,其中包含逗号分隔的地址,您可以像这样拆分和解析:
The mailTo["to"] is simply a string that contains the comma-separated addresses which you can split and parse like this: