Inside Out Outside In

Flex Quick Tip: Launching email client on event

The flash.net package provides a nice solution for launching the default mail client on a mailto: link in Flex,  The sendToURL() function will launch the mail client without opening a new browser window. 

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:comp
="components.*"
    layout
="absolute" borderColor="#000000">
        
<mx:Script>
     
<![CDATA[
    
      
     import flash.net.*;
    
     public function launchMailer(e:Event):void{        
         var mailLink:URLRequest 
= new URLRequest("mailto:" + e.currentTarget.text);
         navigateToURL( mailLink, "_self" );  
     }
            
     ]]
>
</mx:Script>
<mx:Canvas>
  
<mx:Panel>
          
<mx:Text text="John Doe" />
        <
mx:Text text="jdoe@somecompany.com" click="launchMailer(event)" />

  </
mx:Panel>    
</mx:Canvas>

</mx:Application>

Colorized by: CarlosAg.CodeColorizer

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Eric Cobb's Gravatar Great tip! Thanks!

I've got it working perfectly in IE, but I can't get it to work with FireFox. Weird. It may be problems with my FF. When I first tried it, FF kept trying to open up to Gmail instead of Outlook, even though Outlook is my default. I fixed that, FF now opens Outlook for other mailto links, but when I click on the links in my Flex app I get nothing.

Have you run across this before, or is it just a problem with my implementation of FireFox?
# Posted By Eric Cobb | 1/31/08 10:57 AM
Christopher Wigginton's Gravatar Sorry, forgot to take my corporate/IE only
blinders off.

Change the function to:

navigateToURL( mailLink, "_self" );

I just tested that function and it works in both browsers without launching
a secondary blank window.
# Posted By Christopher Wigginton | 1/31/08 3:23 PM
Eric Cobb's Gravatar Thanks! That works perfectly!
# Posted By Eric Cobb | 1/31/08 4:51 PM
Phil's Gravatar FYI, I had to remove the "//" just after the "mailto:". Otherwise the email address looked like "//jdoe@somecompany.com".

I also used the navigateToURL() method as the sendToURL() didn't work.

Otherwise this worked fine. I have an AIR b3 app, running on Mac OS X 10.5.2 and sending email through Apple Mail.
# Posted By Phil | 2/14/08 1:28 PM
Christopher Wigginton's Gravatar @Eric, good catch. I've corrected the code.
That was a hold over from playing around with
some URL's and I hadn't noticed it since
Lotus Notes trimmed it.
# Posted By Christopher Wigginton | 2/14/08 2:24 PM
Jana's Gravatar Is there a way to add an attachment to an email? How about multiple attachments? So far I have the following code.

   private function mailTest():void {
var urlRequest:URLRequest =
         new URLRequest("mailto:someone@company.com?subject=Widget testing&body=this is the body content");
navigateToURL(urlRequest, "_self");
trace ("mailtest");
   }
# Posted By Jana | 2/21/08 7:18 AM
Niels's Gravatar It would be a good idea to update the code in the original post. I for one started out by using the code in the example, without reading the comments. In the end I was saved by the navigateToURL(req,"_self") though.
Good precise read!
# Posted By Niels | 4/9/08 4:31 AM
Christopher Wigginton's Gravatar Thanks Niels,

Example updated.
# Posted By Christopher Wigginton | 4/10/08 1:47 PM