Visual Basic app - run as service and send email thru Exchange (pehaps using MS outlook) Server

Dear Experts,

The goal here is to have a visual basic application run as a service in the background that can also email thru our exchange server (we have been using MS Outlook).     This is what I have tried.    Please help.

Operating system: Microsoft Server 2003
Microsoft Outlook Version: 2010
References: Microsoft Outlook 14.0 Object Library
Visual Basic version: 6.0

Problem:  if I run the below visual application on from the desktop our Windows Server 2003, the application completes within a few minutes.  However in running it as a service, nothing happens.  I can see that it's running in the task manager and outlook.exe is present as well.  However it just sits there doing nothing.  I can also see that outlook does remain loading fine on the server.  The application I am using to make it a service is:  NTWrapperLite.   Also other Visual Basic applications run just fine using NTWrapperLite. Please someone help.  

Here is the code:

General Declarations:
Public countit1 As Long
Public OUTLOOKapp As String
Public countit As Long

Private Sub Form_Load()

Dim i As Integer
Dim ObjMail As Object
Dim Ol As Outlook.Application
Dim x As Integer

countit = 0
countit1 = 0
x = 0

Dim shell As Object
Set shell = CreateObject("WScript.Shell")

OUTLOOKapp = """" & "C:\Program Files\Microsoft Office\Office14\outlook.exe" & """"
shell.Run OUTLOOKapp, vbMinimized

On Error GoTo OpenError

Set Ol = GetObject(, "Outlook.Application")

Set ObjMail = Ol.CreateItem(0)

On Error GoTo CreateError

With ObjMail
 .Subject = "Istream1"
 .To = "<email address #1">
 .Body = "job started1"
 x = x + 1
End With

Set ObjMail = Nothing
Set ObjMail = Ol.CreateItem(0)

With ObjMail
 .Subject = "Istream2"
 .To = "<email address #2>"
 .Body = "job started2"
 x = x + 1
End With

Set ObjMail = Nothing
Set Ol = Nothing

Call Closeit

Exit Sub

Resume RetryHere1
Exit Sub

Resume RetryHere2
End Sub

Public Sub Closeit()

Dim arrFolders() As String
Dim strFolderPath As String
strCOMPUTER = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strCOMPUTER & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Outlook.exe'")

strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")

Set objOutlook = GetObject(, "Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
On Error Resume Next
Set Ol = objNamespace.Folders.Item(arrFolders(0))

On Error GoTo OpenCError
   Set Ol = GetObject(, "Outlook.Application")

Exit Sub

Resume RetryCHere1

End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you need it to send email using your Exchange server, there is no need to launch Outlook.  .NET has objects defined for sending emails; namely the SmtpClient Class in System.Net.Mail.

Other than that, a service really isn't hard to create.

Microsoft has a walkthrough that can get you started:

The next important question is what triggers the sending of the email?  Joacim has a service walkthrough that sends emails from a service based on File Changes:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Apples01Author Commented:
Dear Saige,

Thank you for the response.

I am also finding examples of this SmtpClient Class in Visual Basic:

However, I am having difficulty though as to how to use it.  Also when I create the class as described above there are enough lines that are in red.  Perhaps I need specific references or components checked?  Also what triggers the email (without any attachments) is when a certain event occurs within the visual basic application itself.  Please help.
Apples01Author Commented:
Dear Expert,

Are you saying to use SmtpClient Class I would need to use VB .NET?  If yes, in using VB.NET could I import my existing Visual Basic forms and modules to this new VB .NET project?  How would use this SmtpClient Class so that it does what Outlook does (logs into to our exchange server --->, and send this email?     In looking at the outlook settings the only setting I see of any difference is:  Logon network Security: Negotiate Authentication.  Not sure where to include that and how to change the SMTP server so it points to exchange?

This is what I have found so far.  Please help.

Dim email_to as string = trim(txtMessageto.text)
Dim email_from as string = trim(txtEmail.text)
Dim email_Body as string = trim(txtEmailBody.text)
Dim smtpServer as New.Net.Mail.SMTPclient = “”
Smtp.timeout = 60
Smtp.deliverymethod =

Dim username as string = “my username”
Dim password as string = “my password”

Smtpserver.credentials = new net.mail.mailmessage(email_from, email_to)

Message.subject = email_subject
Message.body = email_body
Message.bodyEncoding = system.text.encoding.UTF8
Message.headers.add(“Reply to”, email_from)
Message.headers.add(“X-Organization”, “home”)

Apples01Author Commented:
Thank you for the suggestion.  It worked!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.