文本框中的超链接电子邮件地址并通过Outlook发送 [英] hyperlink email address in textbox and send it via Outlook
问题描述
我正在开发一个wpf应用程序,我有一个客户信息部分,我可以记录我的客户信息。在本节中,我使用文本框记录客户的电子邮件地址。但现在我想制作电子邮件地址超链接并通过Outlook电子邮件链接电子邮件地址,例如,如果我点击电子邮件地址,它会自动打开Outlook电子邮件,以便我可以通过outlook发送电子邮件。谢谢。
I am working on a wpf app, and I have a Customer Information section where I can record my customer information. In this section, I use a textbox recording customer's email address. But now I want to make the email address hyperlink and link the email address via Outlook email, say, if I click the email address, it opens the outlook email automatically so that I can send email via outlook. Thanks.
我想要的是标签或文本块,其左侧是文本电子邮件(不需要绑定到文本中的文本)文本框),右侧的文本框,您可以在其中键入电子邮件地址。在文本框中键入有效的电子邮件地址后,您可以单击电子邮件地址,它将自动打开Outlook。在Outlook的 To 字段中,电子邮件地址就是您输入的内容。(上一个问题的评论很长,所以我将它作为一个新问题,旧的问题链接是链接电子邮件地址并通过Outlook发送电子邮件
What I want is a Label or Textblock whose text is Email on the left (do not need to bind to the text in the textbox), a Textbox on the right where you can type an email address. After you type a valid email address in the textbox, you can click the email address, and it will open outlook automatically. In the To field of outlook, the email address is what you typed in.(The comments are so long in last question, so I make it a new question, the old question link is link email address and send email via outlook
<TextBlock Text="Email" Grid.Row="11" x:Name="lblEmail" VerticalAlignment="Top"/>
<TextBox Grid.Column="1" Grid.Row="11" x:Name="txtEmail" VerticalAlignment="Top"
TextDecorations="UnderLine" Foreground="Blue" Text="{Binding
Email, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,
ValidatesOnExceptions=True, NotifyOnValidationError=True}">
</TextBox>
推荐答案
好的,让我们再来一次......首先我们有一个 TextBox
用户输入的电子邮件地址为:
OK, let's have another go... first we have a TextBox
that the user enters an e-mail address into:
<TextBox x:Name="EmailTextBox" />
然后我们有超链接
对象,其中 NavigateUri
属性是绑定到 EmailTextBox $ c $
Textbox.Text
字段的数据c>对象:
Then we have a Hyperlink
object whose NavigateUri
property is data bound to the Textbox.Text
field of the EmailTextBox
object:
<Hyperlink RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="{Binding Text,
ElementName=EmailTextBox, UpdateSourceTrigger=PropertyChanged}">
<TextBlock Text="Click here to e-mail" />
</Hyperlink>
然后我们有 RequestNavigateEvent
处理程序验证电子邮件地址(正则表达式取自此帖):
Then we have the RequestNavigateEvent
handler that validates the e-mail address (Regular expression was taken from this post):
public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Hyperlink hyperlink = sender as Hyperlink;
if (hyperlink == null) return;
if (Regex.IsMatch(hyperlink.NavigateUri.ToString(), @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"))
{
string address = string.Concat("mailto:", hyperlink.NavigateUri.ToString());
try { System.Diagnostics.Process.Start(address); }
catch { MessageBox.Show("That e-mail address is invalid.", "E-mail error"); }
}
}
现在,我仍然无法测试任何一个,所以你可能必须自己修复一些小错误,但这是你必须要做的大致。随意添加评论,但这次不要让评论部分大于问题部分。 ;)
Now, I still haven't been able to test any of this, so you might have to fix a couple of little errors yourself, but this is the roughly what you have to do. Feel free to add comments, but lets not make the comment section bigger than the question section this time. ;)
更新>>>
好的,问题是 hyperlink.NavigateUri
实际上是 Uri
对象,而不是字符串
所以我们需要在它上面调用 ToString()
。
Ok, so the problem was that the hyperlink.NavigateUri
is in fact a Uri
object and not a string
so we need to call ToString()
on it.
万一你需要它,你可以替换你的 Hyperlink_RequestNavigate
使用此行设置电子邮件主题的处理程序:
Just in case you need it, you can replace the line in your Hyperlink_RequestNavigate
handler with this line to set the subject of the e-mail:
string address = string.Concat("mailto:", hyperlink.NavigateUri.ToString(),
"?subject=This is the subject");
这可以进一步扩展到添加身体的一部分(或全部):
This can be further extended to add part (or all) of the body too:
string address = string.Concat("mailto:", hyperlink.NavigateUri.ToString(),
"?subject=This is the subject&body=Dear Sir/Madam,");
这篇关于文本框中的超链接电子邮件地址并通过Outlook发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!