469,282 Members | 1,842 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Reflection Question - Convert VBScript to 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 you.n...@whatever.com
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

Jan 9 '06 #1
4 5504
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
Jan 9 '06 #2
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]
- mv*@spam.guard.caspershouse.com

"CJ" <cj*********@mail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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 you.n...@whatever.com
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

Jan 9 '06 #3
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eD*************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"CJ" <cj*********@mail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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 you.n...@whatever.com
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


Jan 10 '06 #4
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 S wrote:
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eD*************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"CJ" <cj*********@mail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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 you.n...@whatever.com
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



Jan 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by davidgordon | last post: by
4 posts views Thread by Alicia | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.