1

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.

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> 
Community
  • 1
  • 1
DanielXu
  • 97
  • 4
  • 14

2 Answers2

4

OK, let's have another go... first we have a TextBox that the user enters an e-mail address into:

<TextBox x:Name="EmailTextBox" />

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>

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

UPDATE >>>

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.

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,");
Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I changed your code ** if (Regex.IsMatch(hyperlink.NavigateUri, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"))** to ** if (Regex.IsMatch(hyperlink.NavigateUri.ToString(), @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$")) **, the errors gone. But when I type an email address and press the hyperlink, it opens the Outlook but throw an error, say **IOException was unhandled Cannot locate resource 'view/organisationv/danielxu89@gmail.com'**. I did research about this, but most is about **IOException was unhandled Cannot locate resource 'App.xaml' **, not about this problem. – DanielXu Aug 22 '13 at 15:45
  • DUDE!! Come on now... let's look at that first error... **cannot convert from 'System.Uri' to 'string'**... now where do we have a `System.Uri`? Maybe `hyperlink.NavigateUri` seeing as it ends with `Uri`? Now, how do we convert an object to a `string`? Hmmm... maybe `ToString()`? You can't have 'researched' for too long. As for the second error, I have no idea what that's for... I finally had a chance to chuck this code into Visual studio and I don't get that error. My example code works just fine for me... take another look because I updated it. – Sheridan Aug 22 '13 at 15:50
  • 'view/organisationv/danielxu89@gmail.com' is *not* an e-mail address... try just typing 'danielxu89@gmail.com' into the `TextBox`. – Sheridan Aug 22 '13 at 15:53
  • I am typing only danielxu89@gmail.com in the textbox, the view/organisationv is my viewpage where the email textbox is – DanielXu Aug 22 '13 at 16:00
  • That doesn't happen in my code... you must have changed something. Either way, and with all due respect to you, I'm done with trying to help you with this problem now... I've provided you with an answer that works (I tested it) and I can't believe how much time I've spent on this. I could have answered another 20+ questions in this time. All I can say is that I tried to help you and good luck with the last little bit. – Sheridan Aug 22 '13 at 16:08
  • Is there a reason you use the `sender as Hyperlink` rather than using the event args `e.Uri.ToString()`? – Ben May 03 '14 at 12:02
  • Also, I found I needed to include `e.Handled = true;` at the end of the `Hyperlink_RequestNavigate` event. Otherwise my app would still try to navigate to the uri after it had already opened the mail client. – Ben May 03 '14 at 12:05
0

you can try code XAML

<TextBlock Name="tbReferAFriend"  MouseDown="tbReferAFriend_MouseDown">Refer a friend</TextBlock>

code behind

 private void tbReferAFriend_MouseDown(object sender, MouseButtonEventArgs e)
            {
                try
                {

                   LaunchEmailClientByShellExecute();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }

    [DllImport("shell32.dll")]
            public static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation,
                string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

            private void launchEmailClientByShellExecute()
            {
                ShellExecute(IntPtr.Zero, "open", "mailto:username?subject=Read%20This&body=message%20contents", "", "", 4/* sw_shownoactivate */);
            }

from : https://social.msdn.microsoft.com/Forums/vstudio/en-US/dcbaaced-97b3-4276-bf95-960e77cb6c03/how-to-launch-default-mail-client-in-wpf-applications?forum=wpf