Reflection Question - Convert VBScript to C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CJ

    Reflection Question - Convert VBScript to C#

    Hi

    I'm trying to send email via a c# app, and I've come across various
    ways to do it, but the way that seems best given my constraints is this
    little vbscript:

    Dim theApp, theNameSpace, theMailItem

    set theApp = CreateObject("O utlook.Applicat ion")
    Set theMailItem = theApp.CreateIt em(0)

    theMailItem.Rec ipients.Add you.n...@whatev er.com
    theMailItem.Sub ject = "Your Subject Here"
    theMailItem.Bod y = "Your message here."
    theMailItem.Sen d

    So I'm trying to convert this to C#, but I'm stuck since I don't know
    how to get the type of theMailItem. This is as far as I've got:

    Type outlookType = Type.GetTypeFro mProgID("Outloo k.Application") ;
    Object outlookApp = Activator.Creat eInstance(outlo okType);


    Any ideas on how to convert the rest of the script?

    Thanks

  • stax

    #2
    Re: Reflection Question - Convert VBScript to C#

    Hi,
    maybe you could just try to compile it using VB .NET or another late binding language like Boo and if it works translate it to C# using Reflector.

    Regards,
    stax

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Reflection Question - Convert VBScript to C#

      CJ,

      You could do this in a similar manner in .NET, but that doesn't mean it
      is the best way. Additionally, there are issues with the way that VB Script
      runs in comparison to how code in the .NET environment runs (in particular,
      accessing COM objects in .NET).

      I would recommend using the facilities in .NET to do what you want. In
      this case, look at the classes in the System.Net.Mail namespace. It will
      allow you to send an email without Outlook being installed.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "CJ" <cj_junktrap@ma il.com> wrote in message
      news:1136815399 .074306.190700@ g44g2000cwa.goo glegroups.com.. .[color=blue]
      > Hi
      >
      > I'm trying to send email via a c# app, and I've come across various
      > ways to do it, but the way that seems best given my constraints is this
      > little vbscript:
      >
      > Dim theApp, theNameSpace, theMailItem
      >
      > set theApp = CreateObject("O utlook.Applicat ion")
      > Set theMailItem = theApp.CreateIt em(0)
      >
      > theMailItem.Rec ipients.Add you.n...@whatev er.com
      > theMailItem.Sub ject = "Your Subject Here"
      > theMailItem.Bod y = "Your message here."
      > theMailItem.Sen d
      >
      > So I'm trying to convert this to C#, but I'm stuck since I don't know
      > how to get the type of theMailItem. This is as far as I've got:
      >
      > Type outlookType = Type.GetTypeFro mProgID("Outloo k.Application") ;
      > Object outlookApp = Activator.Creat eInstance(outlo okType);
      >
      >
      > Any ideas on how to convert the rest of the script?
      >
      > Thanks
      >[/color]


      Comment

      • Craig S

        #4
        Re: Reflection Question - Convert VBScript to C#

        If you're just trying to send mail, then yes, absolutely, follow Nicholas's
        advice. MAPI is ugly, nasty, 4-headed beast that resembles your
        mother-in-law. System.Net.Mail sucks much much less. Lots of examples of
        this and it's just as simple - a handful of lines of code, google away.

        Craig

        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
        message news:eDtkHCTFGH A.916@TK2MSFTNG P10.phx.gbl...[color=blue]
        > CJ,
        >
        > You could do this in a similar manner in .NET, but that doesn't mean it
        > is the best way. Additionally, there are issues with the way that VB
        > Script runs in comparison to how code in the .NET environment runs (in
        > particular, accessing COM objects in .NET).
        >
        > I would recommend using the facilities in .NET to do what you want. In
        > this case, look at the classes in the System.Net.Mail namespace. It will
        > allow you to send an email without Outlook being installed.
        >
        > Hope this helps.
        >
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >
        > "CJ" <cj_junktrap@ma il.com> wrote in message
        > news:1136815399 .074306.190700@ g44g2000cwa.goo glegroups.com.. .[color=green]
        >> Hi
        >>
        >> I'm trying to send email via a c# app, and I've come across various
        >> ways to do it, but the way that seems best given my constraints is this
        >> little vbscript:
        >>
        >> Dim theApp, theNameSpace, theMailItem
        >>
        >> set theApp = CreateObject("O utlook.Applicat ion")
        >> Set theMailItem = theApp.CreateIt em(0)
        >>
        >> theMailItem.Rec ipients.Add you.n...@whatev er.com
        >> theMailItem.Sub ject = "Your Subject Here"
        >> theMailItem.Bod y = "Your message here."
        >> theMailItem.Sen d
        >>
        >> So I'm trying to convert this to C#, but I'm stuck since I don't know
        >> how to get the type of theMailItem. This is as far as I've got:
        >>
        >> Type outlookType = Type.GetTypeFro mProgID("Outloo k.Application") ;
        >> Object outlookApp = Activator.Creat eInstance(outlo okType);
        >>
        >>
        >> Any ideas on how to convert the rest of the script?
        >>
        >> Thanks
        >>[/color]
        >
        >[/color]


        Comment

        • CJ

          #5
          Re: Reflection Question - Convert VBScript to C#

          I know, you're right. But unfortunately in this instance I can
          guarantee that our clients will be running Outlook, and that the
          account my app will be running under will be a valid Outlook user. I
          can't guarantee that they have a smtp server configured.

          My first approach (after I ruled out smtp) was to use the
          Microsoft.Inter op stuff, but I wasn't sure what dlls I needed to
          distribute (my project has 3 outlook-related dlls referenced -
          Interop.Microso ft.Office.Core. dll, Interop.Outlook .dll, and
          stdole.dll), which I was *allowed* to distribute, what would happen if
          they have a different version of Outlook than the one I have on my dev
          PC, etc etc. Someone then recommended the vbscript route, which seemed
          to avoid a lot of these issues. Apparently just to bring up a lot of
          issues of its own!

          Craig S wrote:[color=blue]
          > If you're just trying to send mail, then yes, absolutely, follow Nicholas's
          > advice. MAPI is ugly, nasty, 4-headed beast that resembles your
          > mother-in-law. System.Net.Mail sucks much much less. Lots of examples of
          > this and it's just as simple - a handful of lines of code, google away.
          >
          > Craig
          >
          > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          > message news:eDtkHCTFGH A.916@TK2MSFTNG P10.phx.gbl...[color=green]
          > > CJ,
          > >
          > > You could do this in a similar manner in .NET, but that doesn't mean it
          > > is the best way. Additionally, there are issues with the way that VB
          > > Script runs in comparison to how code in the .NET environment runs (in
          > > particular, accessing COM objects in .NET).
          > >
          > > I would recommend using the facilities in .NET to do what you want. In
          > > this case, look at the classes in the System.Net.Mail namespace. It will
          > > allow you to send an email without Outlook being installed.
          > >
          > > Hope this helps.
          > >
          > >
          > > --
          > > - Nicholas Paldino [.NET/C# MVP]
          > > - mvp@spam.guard. caspershouse.co m
          > >
          > > "CJ" <cj_junktrap@ma il.com> wrote in message
          > > news:1136815399 .074306.190700@ g44g2000cwa.goo glegroups.com.. .[color=darkred]
          > >> Hi
          > >>
          > >> I'm trying to send email via a c# app, and I've come across various
          > >> ways to do it, but the way that seems best given my constraints is this
          > >> little vbscript:
          > >>
          > >> Dim theApp, theNameSpace, theMailItem
          > >>
          > >> set theApp = CreateObject("O utlook.Applicat ion")
          > >> Set theMailItem = theApp.CreateIt em(0)
          > >>
          > >> theMailItem.Rec ipients.Add you.n...@whatev er.com
          > >> theMailItem.Sub ject = "Your Subject Here"
          > >> theMailItem.Bod y = "Your message here."
          > >> theMailItem.Sen d
          > >>
          > >> So I'm trying to convert this to C#, but I'm stuck since I don't know
          > >> how to get the type of theMailItem. This is as far as I've got:
          > >>
          > >> Type outlookType = Type.GetTypeFro mProgID("Outloo k.Application") ;
          > >> Object outlookApp = Activator.Creat eInstance(outlo okType);
          > >>
          > >>
          > >> Any ideas on how to convert the rest of the script?
          > >>
          > >> Thanks
          > >>[/color]
          > >
          > >[/color][/color]

          Comment

          Working...