简体   繁体   中英

link email address and send email via 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.

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> 

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");

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 .

UPDATE >>>

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.

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM