.NET 中的 MailTo URI 对象?

发布于 2024-11-02 07:43:54 字数 1379 浏览 6 评论 0原文

给定一个 URI(已确认为 Uri.UriSchemeMailto),是否有一个可以转换为提供 mailto 属性的对象?

就像 .TargetURI{mailto:[email  ;protected]?subject=我全部完成&body=完成&[电子邮件受保护]&[电子邮件受保护]}< /strong> 是否有一个对象会将其作为 URI 并返回 MailTo 属性?就像

伪代码

Dim mailDetails as New MailDetailsObject(MyURI.TargetURI)

Console.WriteLine(mailDetails.To)
Console.WriteLine(mailDetails.CC)
Console.WriteLine(mailDetails.BCC)
Console.WriteLine(mailDetails.Subject)
Console.WriteLine(mailDetails.Body)

一样,结果是:

[email protected]
[email protected]
[email protected]
I'm all done
Finished

或者这是人们通常只是手动解析或构造 .TargetURI 下的属性?

Given a URI (that is confirmed to be a Uri.UriSchemeMailto) is there an object this can be cast to that would give provide mailto properties?

Like if the .TargetURI was {mailto:[email protected]?subject=I'm all done&body=Finished&[email protected]&[email protected]} is there an object that will take this as a URI and spit back MailTo properties? Like

Pseudo Code

Dim mailDetails as New MailDetailsObject(MyURI.TargetURI)

Console.WriteLine(mailDetails.To)
Console.WriteLine(mailDetails.CC)
Console.WriteLine(mailDetails.BCC)
Console.WriteLine(mailDetails.Subject)
Console.WriteLine(mailDetails.Body)

That results in:

[email protected]
[email protected]
[email protected]
I'm all done
Finished

Or is this something people normally just manually parse or construct out of the properties under .TargetURI?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

も星光 2024-11-09 07:43:54

我不知道有一个类可以做到这一点,但您可以使用 HttpUtility.ParseQueryString() 方法:

var mailto = "mailto:[email protected]?subject=I'm all done&body=Finished&[email protected]&[email protected]";
var values = HttpUtility.ParseQueryString(mailto.Split('?')[1]);
var subject = values["subject"];
var body = values["body"];
var cc = values["cc"];

I don't know of a class that does that, but you could easily extract it with the HttpUtility.ParseQueryString() method:

var mailto = "mailto:[email protected]?subject=I'm all done&body=Finished&[email protected]&[email protected]";
var values = HttpUtility.ParseQueryString(mailto.Split('?')[1]);
var subject = values["subject"];
var body = values["body"];
var cc = values["cc"];
可遇━不可求 2024-11-09 07:43:54

我最终为此编写了自己的课程。它并不完整,也没有使用模式的所有可能部分对其进行测试,但它似乎对于大多数 mailtos 来说工作得足​​够好。请随意编辑此答案并进行改进。

Class MailToItem
    Public Property [To] As String
    Public Property Cc As String
    Public Property Subject As String
    Public Property Body As String
    Public Property Bcc As String
    Private _mailToParameters As String()
    Sub New(uri As String)
        Dim mailtoString As String() = uri.Split("?")
        Me.To = GetEmailToRecipients(mailtoString(0).Split(":")(1))
        _mailToParameters = mailtoString(1).Split("&")
        SetOtherEmailRecipients()
        SetSubject()
        SetBody()
    End Sub
    Private Sub SetOtherEmailRecipients()
        Me.Cc = GetEmailRecipients("cc")
        Me.Bcc = GetEmailRecipients("bcc")
    End Sub
    Private Function GetEmailRecipients(type As String) As String
        Dim recipients As String = Field(type)
        If recipients IsNot Nothing Then
            Dim recipientsAll() As String
            recipientsAll = recipients.Split("=")(1).Split(",")
            Dim s As New System.Text.StringBuilder
            If recipientsAll.Count > 1 Then
                For r = 0 To recipientsAll.Count - 1
                    If r < recipientsAll.GetUpperBound(0) Then
                        s.Append(recipientsAll(r) + ";")
                    Else
                        s.Append(recipientsAll(r))
                    End If
                Next
            Else
                s.Append(recipientsAll(0))
            End If
            Return s.ToString
        Else
            Return ""
        End If
    End Function
    Private Function Field(type As String) As String
        Return _mailToParameters.Where(Function(f) f.StartsWith(type, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault
    End Function
    Private Function GetEmailToRecipients(toString As String) As String
        Dim recipientsAll() As String = toString.Split(",")
        Dim s As New System.Text.StringBuilder
            If recipientsAll.Count > 1 Then
                For r = 0 To recipientsAll.Count - 1
                    If r < recipientsAll.GetUpperBound(0) Then
                        s.Append(recipientsAll(r) + ";")
                    Else
                        s.Append(recipientsAll(r))
                    End If
                Next
            Else
                s.Append(recipientsAll(0))
            End If
            Return s.ToString
    End Function
    Private Sub SetSubject()
        Dim subject As String = Field("subject")
        If subject IsNot Nothing Then
            Me.Subject = NormalizeText(subject.Split("=")(1))
        End If
    End Sub
    Private Sub SetBody()
        Dim body As String = Field("body")
        If body IsNot Nothing Then
            Me.Body = NormalizeText(body.Split("=")(1))
        End If
    End Sub
    Private Function NormalizeText(text As String) As String
        text.Replace("%20", " ")
        text.Replace("%0A%0A", Environment.NewLine + Environment.NewLine)
        text.Replace("%0A", Environment.NewLine)
        text.Replace("%0D", Environment.NewLine)
        Return text
    End Function
End Class

请注意,将 .TargetURI 传递到构造函数中可能会导致失败,因为它似乎没有覆盖完整的架构。例如mailto:[电子邮件受保护],< a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afc5c6c2efc5c0c7c181ccc0c2">[电子邮件受保护](两个收件人)将导致 .TargetURI 失败。在这种情况下,您可以改为传入 .TargetURI.OriginalString

I ended up writing my own class for this. It's not complete and haven't tested it with all possible parts of the schema, but it appears to work well enough for most mailtos. Feel free to edit this answer with improvements.

Class MailToItem
    Public Property [To] As String
    Public Property Cc As String
    Public Property Subject As String
    Public Property Body As String
    Public Property Bcc As String
    Private _mailToParameters As String()
    Sub New(uri As String)
        Dim mailtoString As String() = uri.Split("?")
        Me.To = GetEmailToRecipients(mailtoString(0).Split(":")(1))
        _mailToParameters = mailtoString(1).Split("&")
        SetOtherEmailRecipients()
        SetSubject()
        SetBody()
    End Sub
    Private Sub SetOtherEmailRecipients()
        Me.Cc = GetEmailRecipients("cc")
        Me.Bcc = GetEmailRecipients("bcc")
    End Sub
    Private Function GetEmailRecipients(type As String) As String
        Dim recipients As String = Field(type)
        If recipients IsNot Nothing Then
            Dim recipientsAll() As String
            recipientsAll = recipients.Split("=")(1).Split(",")
            Dim s As New System.Text.StringBuilder
            If recipientsAll.Count > 1 Then
                For r = 0 To recipientsAll.Count - 1
                    If r < recipientsAll.GetUpperBound(0) Then
                        s.Append(recipientsAll(r) + ";")
                    Else
                        s.Append(recipientsAll(r))
                    End If
                Next
            Else
                s.Append(recipientsAll(0))
            End If
            Return s.ToString
        Else
            Return ""
        End If
    End Function
    Private Function Field(type As String) As String
        Return _mailToParameters.Where(Function(f) f.StartsWith(type, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault
    End Function
    Private Function GetEmailToRecipients(toString As String) As String
        Dim recipientsAll() As String = toString.Split(",")
        Dim s As New System.Text.StringBuilder
            If recipientsAll.Count > 1 Then
                For r = 0 To recipientsAll.Count - 1
                    If r < recipientsAll.GetUpperBound(0) Then
                        s.Append(recipientsAll(r) + ";")
                    Else
                        s.Append(recipientsAll(r))
                    End If
                Next
            Else
                s.Append(recipientsAll(0))
            End If
            Return s.ToString
    End Function
    Private Sub SetSubject()
        Dim subject As String = Field("subject")
        If subject IsNot Nothing Then
            Me.Subject = NormalizeText(subject.Split("=")(1))
        End If
    End Sub
    Private Sub SetBody()
        Dim body As String = Field("body")
        If body IsNot Nothing Then
            Me.Body = NormalizeText(body.Split("=")(1))
        End If
    End Sub
    Private Function NormalizeText(text As String) As String
        text.Replace("%20", " ")
        text.Replace("%0A%0A", Environment.NewLine + Environment.NewLine)
        text.Replace("%0A", Environment.NewLine)
        text.Replace("%0D", Environment.NewLine)
        Return text
    End Function
End Class

Note that passing a .TargetURI into the constructor may cause failures as it doesn't appear to cover the full schema. For example mailto:[email protected],[email protected] (two recipients) will cause .TargetURI to fail. In that case, you can pass in .TargetURI.OriginalString instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文