给定一个URI(确认为Uri.UriSchemeMailto
)是否有一个对象可以转换为提供mailto属性的对象?
如果.TargetURI
{mailto:your@email.com?subject =我已经完成了& body =已完成& cc = the@email.com& bcc =她的@ email。 com} 是否有一个对象将此作为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)
结果是:
your@email.com his@email.com her@email.com I'm all done Finished
或者这是人们通常只是手动解析或构建.TargetURI
下的属性?
答案 0 :(得分:2)
我不知道有哪个类可以做到这一点,但您可以使用HttpUtility.ParseQueryString()
方法轻松提取它:
var mailto = "mailto:your@email.com?subject=I'm all done&body=Finished&cc=his@email.com&bcc=her@email.com";
var values = HttpUtility.ParseQueryString(mailto.Split('?')[1]);
var subject = values["subject"];
var body = values["body"];
var cc = values["cc"];
答案 1 :(得分:0)
我最后为此写了自己的课程。它不完整,并没有使用模式的所有可能部分进行测试,但它似乎对大多数邮件运行良好。随意修改此答案。
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:john @ jim.com,jim @ john.com (两个收件人)将导致.TargetURI
失败。在这种情况下,您可以改为传递.TargetURI.OriginalString
。