hyperlink email address in textbox and send it via Outlook

后端 未结 2 589
遇见更好的自我
遇见更好的自我 2021-01-24 16:56

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 add

2条回答
  •  不知归路
    2021-01-24 17:49

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

    
    

    Then we have a Hyperlink object whose NavigateUri property is data bound to the Textbox.Text field of the EmailTextBox object:

    
        
    
    

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

提交回复
热议问题