none
How to use 'mailTo' in Hyperlink for multiple email recipients?

    Question

  • Hi,

    I want to send an email to multiple users using Hyperlink in WPF.

    This can be achieved using 'CC' or 'BCC' field, but I want to add them in the 'TO' Field only.
    Number of recipients are around (5-10).

    How can use this in 'NavigateUri' using "mailTo"?

    e.g.

    <Hyperlink NavigateUri="mailto:test1@test.com,test2@test.com?subject=test">Contact us
    </Hyperlink>


    Thanks
    Thursday, September 17, 2009 10:18 AM

Answers

  • Wowsers, you found a product bug there Himanshu.  To set the record straight here:

    1) MailTo: Hyperlinks do work starting in .NET 3.5 SP1, which all 3.X versions will get updated to if the machine is set for automatic updates.
    2) A common problem with using hyperlinks is not having them be parented by a Navigator, i.e. a Frame, NavigationWindow, or web browser.

    Unfortunately multiple to: recipients isnt working, and I've filed a bug on our side of things to track that.

    All is not lost though, while your links may only have one to: recipient, they aren't limited at all in the CC: and BCC: recipients, i.e. this works:

                    <TextBlock>
                      <Hyperlink NavigateUri="mailto:changoval@microsoft.com?subject=why dont mailto links work&amp;cc=mattgal@microsoft.com;andreen@microsoft.com" >Send mail to those guys! </Hyperlink>
                    </TextBlock>
    ... for as many semi-colon separated email addresses you care to add to the CC line.

    Hope this helps.  This will be addressed in some future release, but please use a workaround for now.

    -Matt
    SDET : Deployment/Hosting
    • Proposed as answer by Olaf Rabbachin Tuesday, September 22, 2009 5:11 PM
    • Marked as answer by Bruce.Zhou Wednesday, September 23, 2009 2:24 AM
    Monday, September 21, 2009 6:29 PM
    Moderator

All replies

  • Try ; instead of ,
    Thursday, September 17, 2009 10:25 AM
  • Thanks for quick reply but ; is not working as well getting following error:

    Error    Value 'mailto:test1@test.com;test2@test.com' cannot be assigned to property 'NavigateUri'. Invalid URI: The hostname could not be parsed.
    Thursday, September 17, 2009 10:28 AM
  • Hi Himanshu,

    This won't work with the Hyperlink control, not even if you only have a single mail adress within it. See i.e. this thread:
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/31679c87-ca07-40e0-a299-fd77096b6d47

       - Matt Galbraith says: Not true as of .NET 3.5 Sp1.  This bug was fixed in that release.  Any remaining confusion stems from using a Hyperlink not parented by a Navigator (NavigationWindow, Frame, or browser window)


    I tend to instead use a little helper method for sending mail, in that method, UrlEncoding such addresses does the trick for me:

       ''' <summary>
       ''' Simple method to open a new mail window (default mail client must be setup and working)
       ''' </summary>
       ''' <param name="To">Recipient(s) of mail (if more than one, delimit addresses with ";")</param>
       ''' <param name="Subject">Subject of mail</param>
       ''' <param name="Message">Body of mail</param>
       Public Shared Sub StartDefaultMail( _
           ByVal [To] As String, _
           Optional ByVal Subject As String = "", _
           Optional ByVal Message As String = "" _
       )
          Try
             Dim psi As New ProcessStartInfo()
             psi.UseShellExecute = True
             psi.FileName = _
                  "mailto:" & HttpUtility.UrlEncode([To]) & _
                  "?subject=" & HttpUtility.UrlEncode(Subject) & _
                  "&body=" & HttpUtility.UrlEncode(Message)
             Process.Start(psi)
          Catch ex As Exception
             Throw _
                 New ApplicationException( _
                     "Default mail client could not be started.", _
                     ex _
                 )
          End Try
    

    If required, you can DataBind your Hyperlink to a property exposed by your Window's code-behind or your ViewModel, or use i.e. a button instead.

    Cheers,
    Olaf
    Sunday, September 20, 2009 5:33 PM
  • Wowsers, you found a product bug there Himanshu.  To set the record straight here:

    1) MailTo: Hyperlinks do work starting in .NET 3.5 SP1, which all 3.X versions will get updated to if the machine is set for automatic updates.
    2) A common problem with using hyperlinks is not having them be parented by a Navigator, i.e. a Frame, NavigationWindow, or web browser.

    Unfortunately multiple to: recipients isnt working, and I've filed a bug on our side of things to track that.

    All is not lost though, while your links may only have one to: recipient, they aren't limited at all in the CC: and BCC: recipients, i.e. this works:

                    <TextBlock>
                      <Hyperlink NavigateUri="mailto:changoval@microsoft.com?subject=why dont mailto links work&amp;cc=mattgal@microsoft.com;andreen@microsoft.com" >Send mail to those guys! </Hyperlink>
                    </TextBlock>
    ... for as many semi-colon separated email addresses you care to add to the CC line.

    Hope this helps.  This will be addressed in some future release, but please use a workaround for now.

    -Matt
    SDET : Deployment/Hosting
    • Proposed as answer by Olaf Rabbachin Tuesday, September 22, 2009 5:11 PM
    • Marked as answer by Bruce.Zhou Wednesday, September 23, 2009 2:24 AM
    Monday, September 21, 2009 6:29 PM
    Moderator
  • Hi Matt,

    I am running 3.5 SP1 and your sample code is not working for me. If I try the XAML you posted (i.e. pasted into a regular window), nothing happens at all (which is also true for a http-URI). What you seem to be stating in 2) is rather what is quoted in the thread I posted a link for (MSDN-quote):

    "Hyperlink can navigate to the value of the NavigateUri property only if either the direct or indirect parent of a Hyperlink is a navigation host, including NavigationWindow , Frame , or any browser that can host XBAPs (which includes Internet Explorer 7, Microsoft Internet Explorer 6, and Firefox 2.0+)."

    That said, I'll still have to utilize the click-event in order to actually "start" the link via code.
    So, am I missing something in your recommendation that would be required in order for your sample code to work?

    Cheers,
    Olaf
    Tuesday, September 22, 2009 7:25 AM
  • Sorry for duplicating #2 there but it's clearly a common error ...  yes if you paste the code into a regular window, it won't work.  So (if for some reason you wanted) to fix this you need to do one of the following:

    1) Add a WPF Frame element to this window, and have the hyperlink be a child of this frame. 
    2) Change the type of your Window, both in the .xaml and .xaml.cs file to NavigationWindow.

    Otherwise NavigationService will be undefined for your hyperlink, and as you observe, nothing happens.  This is a completely separate issue to the Uri parsing bug you hit.  Given the Uri parsing bug and that you are running a full trust app (you clearly are if you're creating Windows) and you want to handle your navigation in code anyways, you don't need to use the Hyperlink control at all... you could use any UIElement and respond to it being clicked by doing what Olaf_R does in his sample.   

    In that case, if you want to use a plain Hyperlink in a plain Window for this approach, you can even do that... simply handle the PreviewMouseDown event on your Hyperlinks, then do whatever you like in the codebehind for them.  This approach isn't particularly different than using a button to do the same work.



    Let me know if this doesnt make sense, and I'll try again :)

    Hope this helps,
    Matt
    SDET : Deployment/Hosting
    Tuesday, September 22, 2009 5:07 PM
    Moderator
  • Let me know if this doesnt make sense, and I'll try again :)
    Hi Matt,

    thanks for elaborating - I got it. :)

    I just wonder why the documentation isn't all to clear about this in the first place, especially since developers (well, I for one) would expect the Hyperlink to work as i.e. a LinkLabel in WinForms did.

    Cheers,
    Olaf
    Tuesday, September 22, 2009 5:11 PM
  • Duly noted... I agree the docs aren't the best for making this problem discoverable.  I've forwarded this thread to our team's technical writer as an area for improvement.  Thanks for your feedback.

    -Matt
    SDET : Deployment/Hosting
    Tuesday, September 22, 2009 6:01 PM
    Moderator