0

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> 
DanielXu
  • 97
  • 4
  • 14
  • Please see answer provided by Sheridan from the link http://stackoverflow.com/questions/18378707/hyperlink-email-address-in-textbox-and-send-it-via-outlook. – DanielXu Aug 23 '13 at 10:29

1 Answers1

1

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.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Hi, Sharidan. Thanks for your quick reploy. I have already read the post and got similar sample about sending email using wpf. But it is not what I want. Now I want make the email address hypelink and after I type the email address and press it, the outlook email page will display automatically so that email can be sent. – DanielXu Aug 21 '13 at 10:29
  • @DanielXu, the last link that I provided you with will take you to a post on StackOverflow where a `Hyperlink` is created and used... just what you want. – Sheridan Aug 21 '13 at 10:36
  • What I want is after typing an email, the email address is hyperlink. Then, I press the email address and it opens outlook. The post enables me to write a text hypelink and the email address is fixed, say "Click Me", after u press "Click Me", outlook is open and the receipt is what you set up first (fixed). – DanielXu Aug 21 '13 at 11:17
  • If I understand you correctly from your latest comment, then you want to add a hyperlink containing the text that a user has just entered into a `TextBox`... is that correct? – Sheridan Aug 21 '13 at 11:22
  • Yes, that is what i want. Sorry for not clarify. I did so much research, but I did not find. Do you have such sample? thanks – DanielXu Aug 21 '13 at 11:30
  • I tried your code, but it has error say **The property 'Text' was not found in type 'Hyperlink'**. I post my code now. Please see the update question. thanks. – DanielXu Aug 21 '13 at 14:22
  • Sorry, my mistake... the text comes from the content... the `TextBlock` whose `Text` property is bound to the value of the `TextBox`. I've updated my answer. – Sheridan Aug 21 '13 at 14:38
  • I use the above code, error message says **A value of type 'Hyperlink' cannot be added to a collection or dictionary of type 'UIElementCollection'** – DanielXu Aug 21 '13 at 14:59
  • Sorry once more... I can't test this code at the moment. The problem was that you must add `Hyperlink` objects inside something like a `TextBlock` control. I have updated my answer code again. – Sheridan Aug 21 '13 at 15:09
  • @ Sheridan. I also did research, we can only use Hypelink inside Textblock. But when I use your update code, there is no build errors, but the app crashes and throw error 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' – DanielXu Aug 21 '13 at 15:47
  • Dude, you have to add two `BoolToVisibility` converters into your `Resources` section... call one `BoolToVisibilityConverter` and the other `InverseBoolToVisibilityConverter`... come on... you have to do *some* of this yourself. – Sheridan Aug 21 '13 at 16:25
  • Sorry, your code still does not work. I did try a lot before asking the question. At first I use this code, it can open outlook, but the email address in **To** field is not what I typed. It is what I defined in Hyperlink_RequestNavigate behind: – DanielXu Aug 22 '13 at 08:08
  • private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) {System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "mailto:someone@somewhere.com?subject=hello&body=love my body";proc.Start(); e.Handled = true; } – DanielXu Aug 22 '13 at 08:13
  • Basically, 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. I may not clarify in my question. I do understand your code and tried to fix errors while replying to you, not just wait for your answer. Thanks again for your help. – DanielXu Aug 22 '13 at 08:24
  • Try updating the `TextBox` binding with this: `Text="{Binding Email, UpdateSourceTrigger=PropertyChanged}"`. – Sheridan Aug 22 '13 at 08:30
  • I have tried that before, it does not work, either. My current problem is how to write the code in private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) to get the email address in the **To** field of the outlook. – DanielXu Aug 22 '13 at 08:59
  • I found a sample yesterday http://www.codeproject.com/Articles/9151/LinkTextBox-a-quick-solution-for-entering-emails-a , but it is for windowsform. The email address function is what i want in the sample. But I tried but i still have no idea how to implement that in wpf – DanielXu Aug 22 '13 at 09:49
  • Buddy, this has gone on too long... start a fresh question with a link to this one. – Sheridan Aug 22 '13 at 10:54
  • @Shredian. Thanks so much for your help, haha... I will create a new question linking to this one. – DanielXu Aug 22 '13 at 10:57
  • Where does Settings.Default.DefaultEmailServerPath come from? I can't find that anywhere. Thanks – David Thielen Mar 25 '18 at 23:11
  • `Settings.Default` is the default name for the application/user settings files. See [Using Application Settings and User Settings](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings) on MSDN for more information on settings. `DefaultEmailServerPath` is just the name of a custom setting, in which the default e-mail server path is stored, so you won't have one... until you add one. ;) – Sheridan Mar 26 '18 at 10:46