473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5780
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bill | last post by:
I need to convert a variable, nNum, into a two-character string. nNum is always less than 100. If nNum is 0, the string needs to be "00", if it's 1, it needs to be "01", if it's 34, it needs to...
2
by: davidgordon | last post by:
Hi, I have some pages with this VBScript code, which obviously does not work in Firefox. How can I convert this to Javascript in order for my web page to work in Firefox ? It basically fills a...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
4
by: Alicia | last post by:
Hi all, I have a problem with an Enum and Reflection. I am using an Xml and Reflection to create some controls, and to set their properties. All goes well until I encounter one property which is...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
2
by: Ronchese | last post by:
Hi all. Is possible I coerce a value to a determined type informed by a string? See the sample: dim obj as Object obj = SomeConversionType(2, "System.Windows.Forms.DockStyle")
2
by: LostnCode | last post by:
Hi, Can anyone help, I need to convert his code from vbscript to sharp C# for use with ASP.Net2.0? This is my first time using a forum. I don't know anything about either coding language so...
2
by: teecee99 | last post by:
This may be a dumb question but here goes. I have a retail web-site and back end order processing system built with vbscript that is currently hosted on GoDaddy on a shared hosting platform (i.e....
1
by: pyar | last post by:
Hi guys, I am trying since so long to resolve my query as how to Convert VBScript to DLL(.NET DLL).I browsed thru net but no use.So please could anyone of you guys help me out by giving a sample...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.