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

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

RetryHere2:
Set ObjMail = Ol.CreateItem(0)

On Error GoTo CreateError

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

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

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


Set ObjMail = Nothing
Set Ol = Nothing

Call Closeit

Exit Sub

OpenError:
Resume RetryHere1
Exit Sub

CreateError:
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
RetryCHere1:
   Set Ol = GetObject(, "Outlook.Application")
   Ol.Quit


Exit Sub

OpenCError:
Resume RetryCHere1

End Sub

Open in new window

Apples01Asked:
Who is Participating?
 
it_saigeDeveloperCommented:
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:

https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.90).aspx

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:

http://blogs.msmvps.com/joacim/2009/09/13/creating-a-windows-service-using-vb-net/

-saige-
0
 
Apples01Author Commented:
Dear Saige,

Thank you for the response.

I am also finding examples of this SmtpClient Class in Visual Basic:
stackoverflow.com/questions/15913261/send-emails-with-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.
0
 
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 ---> exchange.companyx.com, 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.server.host = “smtp.my.isp.net”
Smtp.timeout = 60
Smtp.deliverymethod = net.mail.smtpdelivery.network

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”)

smtpServer.SendAsync(message)
0
 
Apples01Author Commented:
Thank you for the suggestion.  It worked!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.