Reflection Question - Convert VBScript to C#

C

CJ

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("Outlook.Application")
Set theMailItem = theApp.CreateItem(0)

theMailItem.Recipients.Add (e-mail address removed)
theMailItem.Subject = "Your Subject Here"
theMailItem.Body = "Your message here."
theMailItem.Send

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.GetTypeFromProgID("Outlook.Application");
Object outlookApp = Activator.CreateInstance(outlookType);


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

Thanks
 
S

stax

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
C

Craig S

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 said:
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]
- (e-mail address removed)

CJ said:
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("Outlook.Application")
Set theMailItem = theApp.CreateItem(0)

theMailItem.Recipients.Add (e-mail address removed)
theMailItem.Subject = "Your Subject Here"
theMailItem.Body = "Your message here."
theMailItem.Send

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.GetTypeFromProgID("Outlook.Application");
Object outlookApp = Activator.CreateInstance(outlookType);


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

Thanks
 
C

CJ

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.Interop stuff, but I wasn't sure what dlls I needed to
distribute (my project has 3 outlook-related dlls referenced -
Interop.Microsoft.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 said:
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 said:
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]
- (e-mail address removed)

CJ said:
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("Outlook.Application")
Set theMailItem = theApp.CreateItem(0)

theMailItem.Recipients.Add (e-mail address removed)
theMailItem.Subject = "Your Subject Here"
theMailItem.Body = "Your message here."
theMailItem.Send

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.GetTypeFromProgID("Outlook.Application");
Object outlookApp = Activator.CreateInstance(outlookType);


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

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top