链接电子邮件地址并通过outlook发送电子邮件 [英] link email address and send email via outlook

查看:248
本文介绍了链接电子邮件地址并通过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. Appreciate for samples. Thanks.

我想要的是一个标签或文本块,其文本是左边的电子邮件(不需要绑定到文本框中的文本),右边的文本框是您可以输入电子邮件地址。在文本框中键入有效的电子邮件地址后,您可以单击电子邮件地址,它将自动打开Outlook。在To of 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 of outlook, the email address is what you typed in.

<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> 


推荐答案

我使用此方法发送电子邮件。 ..请注意,这不是专门用于Outlook ...它将使用用户计算机上设置的默认电子邮件程序:

I use this method to send my e-mails... note that this is not specifically for Outlook... it will use whichever software is the default e-mail program that is set on the user's computer:

public bool SendEmail(List<string> toAddresses, List<string> ccAddresses, string fromAddress, string emailSubject, string emailBody, bool isBodyHtml)
{
    MailMessage email = new MailMessage();
    email.From = new MailAddress(fromAddress);
    foreach (string address in toAddresses) email.To.Add(new MailAddress(address));
    foreach (string address in ccAddresses) email.CC.Add(new MailAddress(address));
    email.BodyEncoding = Encoding.UTF8;
    email.IsBodyHtml = false;
    email.Subject = emailSubject;
    email.Body = emailBody;
    email.Priority = MailPriority.Low;
    SmtpClient smtpClient = new SmtpClient(Settings.Default.DefaultEmailServerPath);
    smtpClient.Credentials = new NetworkCredential(Settings.Default.EmailNetworkCredentialUserName, Settings.Default.EmailNetworkCredentialPassword, Settings.Default.EmailNetworkCredentialDomain);
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.Host = Settings.Default.DefaultEmailServerPath;
    smtpClient.UseDefaultCredentials = true;
    try { smtpClient.Send(email); }
    catch { return false; }
    return true;
}

请注意我为此重载了方法,所以这个方法有所有选项在它...如果你愿意,你可以自由删除几行。还有一种发送电子邮件的快捷方式:

Note that I have overloaded methods for this, so this one has all the options in it... you can freely remove several lines if you prefer. There is also a shortcut way of sending an e-mail:

System.Diagnostics.Process.Start("mailto:youremail@yourcompany.com");

基本上,我会添加 HyperLink 控件,或者按钮,其中包含 Command ,然后从您的处理程序中调用此代码。您可以从 HyperLink 控件的更多信息。 MSDN上的.ui.xaml.documents.hyperlink.aspxrel =nofollow noreferrer>超链接类页面,在这篇文章

Basically, I would add either a HyperLink control, or a Button that has a Command into your UI and then call this code from your handler. You can find out more about the HyperLink control from the Hyperlink class page at MSDN and there is a good example found in this post.

更新>>>

你真的应该提供代码示例......我不知道你有什么设置 TextBox ,无论是否绑定,参数名称等等。因此,我只能假设您必须与自己的代码相关。

You really should provide code examples... I have no idea how you have set up your TextBox, whether you are binding or not, the names of the parameters and so forth. As such, I can only make assumptions that you will have to relate to your own code.

首先,添加超链接相同的位置控制 TextBox

First, add a Hyperlink control in the same place as your TextBox:

<TextBox Grid.Row="0" Grid.Column="1" Name="EmailTextBox" Text="{Binding Email}" 
    Visibility="{Binding IsValidEmail, Converter={StaticResource 
    InverseBoolToVisibilityConverter}}" />
<TextBlock Grid.Row="0" Grid.Column="1">
    <Hyperlink RequestNavigate="Hyperlink_RequestNavigate">
        <TextBlock Text="{Binding Text, ElementName=EmailTextBox}" Visibility="{
    Binding IsValidEmail, Converter={StaticResource BoolToVisibilityConverter}}" />
    </Hyperlink>
</TextBlock>

您看到这里的基本想法是让两个控件共享一个UI位置并轮流根据 TextBox 的值显示。因此,您需要添加一个 bool 属性(在我的示例中为 IsValidEmail ),当您设置为true时文本值是有效的电子邮件地址。然后 BoolToVisibilityConverter 会将该真值转换为 Visibility.Visible ,用于超链接 control和 InverseBoolToVisibilityConverter 会将该false值转换为 Visibility.Collapsed 超链接控件的Visibility.Hidden 。我希望并相信你可以自己完成剩下的工作,因为我今天的时间有限。

You see the basic idea here is to have the two controls share one UI location and 'take turns' to be visible depending on the value of the TextBox. Therefore, you'll need to add a bool property (IsValidEmail in my example) that you set to true when the text value is a valid e-mail address. Then the BoolToVisibilityConverter will convert that true value to Visibility.Visible for the Hyperlink control and the InverseBoolToVisibilityConverter will convert that false value to Visibility.Collapsed or Visibility.Hidden for the Hyperlink control. I hope and trust that you can work the rest out yourself as my time is limited today.

这篇关于链接电子邮件地址并通过outlook发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆