如何访问 HttpUtility.ParseQueryString 返回的 NameValueCollection 中的集合?

发布于 2024-12-02 12:11:41 字数 1309 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最单纯的乌龟 2024-12-09 12:11:41

要回答您的问题而不提供替代解决方案,请尝试以下操作:

     foreach (var value in mailTo.GetValues("to"))
     {
        Debug.WriteLine(value);
     }

To answer your question without offering an alternative solution, try this:

     foreach (var value in mailTo.GetValues("to"))
     {
        Debug.WriteLine(value);
     }
怪我入戏太深 2024-12-09 12:11:41

NameValueCollection 的值部分是一个字符串。因此,对于“to”键,它将是一个以逗号分隔的电子邮件地址字符串。

string s = "[email protected]&[email protected]&[email protected]&subject=test";
var items = System.Web.HttpUtility.ParseQueryString(s);

foreach(string email in items["to"].Split(','))
{
    Console.Out.WriteLine(email);
}

结果如下

[email protected]
[email protected]
[email protected]

the value part of the NameValueCollection is a string. So for the "to" key it would be a comma delimited string of email addresses.

string s = "[email protected]&[email protected]&[email protected]&subject=test";
var items = System.Web.HttpUtility.ParseQueryString(s);

foreach(string email in items["to"].Split(','))
{
    Console.Out.WriteLine(email);
}

results in the following

[email protected]
[email protected]
[email protected]
我是男神闪亮亮 2024-12-09 12:11:41

mailTo["to"] 只是一个字符串,其中包含逗号分隔的地址,您可以像这样拆分和解析:

    foreach (var address in mailTo["to"].Split(','))
    {
        message.To.Add(new MailAddress(address));
    }

The mailTo["to"] is simply a string that contains the comma-separated addresses which you can split and parse like this:

    foreach (var address in mailTo["to"].Split(','))
    {
        message.To.Add(new MailAddress(address));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文