locked
Send an e-mail from LightSwitch with default email program

    Question

  • in vb.net and c# I easily send mail in client default email program by using the "System.Diagnostics.Process".

    in the lightswitch I can't do it.

    is there any way to make  a simple ""mailto:" ???

    Friday, May 13, 2011 12:08 PM

Answers

  • I was using similar code to that in my application Yann.

    I kept running into a threading issue.

    But if you wrap that code (C#) as follows:

    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
                    {
                        HtmlPage.Window.Navigate(uri, "_blank");
                    })

     

    You can get it to operate correctly.

     

    The only annoying thing that I've noticed is that it opens a new instance of IE when the Outlook message comes up, so after sending the email, you'd have to close the IE browser that it opened up. I haven't been able to call Outlook without having the IE window pop up too, which is a bit annoying.

    • Marked as answer by alonisoft Saturday, May 28, 2011 7:08 AM
    Thursday, May 26, 2011 5:13 PM
  • Hi,

    Here's a couple of posts that address just that subject.

    http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/58700658-0bf8-4578-a1aa-472b9a84af1c

    http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/114b9975-10f9-459f-be55-045a1a07164e

    Here's the code I ended up with from with help from Bob's post:

    VB:

     

        Public Sub SendEmail( _
              ByVal toAddress As String _
            , ByVal subject As String _
            , ByVal body As String _
            )
     
            Dim url As String = String.Format("mailto:{0}?subject={1}&body={2}", toAddress, subject, body)
            Dim uri As Uri = New Uri(url)
     
            Select Case AutomationFactory.IsAvailable
                Case True
                    Dim shell = AutomationFactory.CreateObject("Shell.Application")
     
                    'shell.ShellExecute(url) if Option Strict is Off
                    Versioned.CallByName(shell, "ShellExecute", Method, url)
     
                Case Else
                    HtmlPage.Window.Navigate(uri, "_blank")
            End Select
        End Sub
    

     

    C#:

     

    public void SendEmail(string toAddress, string subject, string body)

    {

     

    string url = string.Format("mailto:{0}?subject={1}&body={2}", toAddress, subject, body);

    Uri uri = new Uri(url);

     

    switch (AutomationFactory.IsAvailable)

    {

    case true:

    var shell = AutomationFactory.CreateObject("Shell.Application");

    shell.ShellExecute(url)

    break;

    default:

    HtmlPage.Window.Navigate(uri, "_blank");

    break;

    }

    }

    Hope that helps,

     

    Yann

    Friday, May 13, 2011 12:19 PM

All replies

  • Hi,

    Here's a couple of posts that address just that subject.

    http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/58700658-0bf8-4578-a1aa-472b9a84af1c

    http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/114b9975-10f9-459f-be55-045a1a07164e

    Here's the code I ended up with from with help from Bob's post:

    VB:

     

        Public Sub SendEmail( _
              ByVal toAddress As String _
            , ByVal subject As String _
            , ByVal body As String _
            )
     
            Dim url As String = String.Format("mailto:{0}?subject={1}&body={2}", toAddress, subject, body)
            Dim uri As Uri = New Uri(url)
     
            Select Case AutomationFactory.IsAvailable
                Case True
                    Dim shell = AutomationFactory.CreateObject("Shell.Application")
     
                    'shell.ShellExecute(url) if Option Strict is Off
                    Versioned.CallByName(shell, "ShellExecute", Method, url)
     
                Case Else
                    HtmlPage.Window.Navigate(uri, "_blank")
            End Select
        End Sub
    

     

    C#:

     

    public void SendEmail(string toAddress, string subject, string body)

    {

     

    string url = string.Format("mailto:{0}?subject={1}&body={2}", toAddress, subject, body);

    Uri uri = new Uri(url);

     

    switch (AutomationFactory.IsAvailable)

    {

    case true:

    var shell = AutomationFactory.CreateObject("Shell.Application");

    shell.ShellExecute(url)

    break;

    default:

    HtmlPage.Window.Navigate(uri, "_blank");

    break;

    }

    }

    Hope that helps,

     

    Yann

    Friday, May 13, 2011 12:19 PM
  • Hi Yann,

     

    Thanks for your help.

    I've Imports in th code

     

    Imports System.Runtime.InteropServices.Automation

    Imports Microsoft.VisualBasic.CompilerServices

     

    And still un declared for htmlpage + method.

     

    one link was unuseful and the other didn't got the point in the end.

    maybe i'm not insert the code in the right place ?

     

    Thanks Again.

    Saturday, May 14, 2011 5:56 PM
  • And still un declared for htmlpage + method.

    image

    Add a reference to System.Windows.Browser, by switching to File View in the Solution Explorer, and right-clicking on References in the Client project.


    http://www.adefwebserver.com

    http://LightSwitchHelpWebsite.com

    Saturday, May 14, 2011 7:24 PM
  • Hi,

    The reference that Michael points out should fix your "undeclared" problem, sorry I missed pointing that out.

    The second post was to give background (& credit where it belonged) of what "my" code was based on. I tidied it up a little, converted it to VB (& founds solutions for late binding problems), but originally it case from Bob's reply in that second post.

    If the missing reference doesn't help, maybe tell us where you're using the code.

    Yann

    Sunday, May 15, 2011 12:40 AM
  • hi There,

     

    still not work...

    my lightswitch is for web.

    if you can to upload any full example that checked that realy work.

    alot of developers will say abig thank's :)))

     

    thanks alot.

    p.s i add the reference and in method... - i get undeclare problem.

     

    Friday, May 20, 2011 8:44 PM
  • OT.  Is that powerpoint to Annotate the screen captures?  tia
    Friday, May 20, 2011 10:12 PM
  • OT.  Is that powerpoint to Annotate the screen captures?  tia


    It's PicPick http://www.picpick.org

     


    http://www.adefwebserver.com

    http://LightSwitchHelpWebsite.com

    Friday, May 20, 2011 10:17 PM
  • If you've added the reference, & still get the problem, if a rebuild doesn't "fix" it, you've added the reference in the wrong place, or accidentally added the "wrong" reference.

    The code I gave you should handle a web application, but nothing will work until you get the reference sorted out correctly.

    Saturday, May 21, 2011 12:14 AM
  • Hi yann,

    i choose from application type  - to desktop instead of web.

    and now, everything works just great and a big smile on my face :)

     

    life not easy you know, becuse i want to deploy my app to the web users.

    probably we can somehow any "link" - mailto:

    Thanks.

     

    alon.

     

    Wednesday, May 25, 2011 7:21 PM
  • Hi alon,

    I'm very glad you got it working!

    However, are you saying that the code I gave earlier doesn't work for a web app?

    I do desktop apps myself, so I haven't needed it to work in a web app, but I'd like to know if it doesn't work.

    Yann

     

    Wednesday, May 25, 2011 10:54 PM
  • I was using similar code to that in my application Yann.

    I kept running into a threading issue.

    But if you wrap that code (C#) as follows:

    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
                    {
                        HtmlPage.Window.Navigate(uri, "_blank");
                    })

     

    You can get it to operate correctly.

     

    The only annoying thing that I've noticed is that it opens a new instance of IE when the Outlook message comes up, so after sending the email, you'd have to close the IE browser that it opened up. I haven't been able to call Outlook without having the IE window pop up too, which is a bit annoying.

    • Marked as answer by alonisoft Saturday, May 28, 2011 7:08 AM
    Thursday, May 26, 2011 5:13 PM
  • Thanks for letting me know about that!
    Friday, May 27, 2011 11:11 PM
  • You made it !

    i use chrome and its working just great !!!

     

    Thanks.

    Saturday, May 28, 2011 7:11 AM