VB.NET mit hMailServer

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
deavik
New user
New user
Posts: 4
Joined: 2021-02-16 21:57

VB.NET mit hMailServer

Post by deavik » 2021-02-17 10:27

Hello,
we have been using hMailServer for more than 10 years to send measurement reports as html files to workstations.

This works very well. We are very satisfied.
Now we want to use scripting.
To my understanding: it is all written in a vbs file (different functions) and then retrieved depending on the rule needed function?Is it scripting only via vbs or also via VB.NET?

We need to automate a workflow. Customer sends name of file in message body - file is searched in specific directory and returned.
Can anyone give some examples on this or where else can I look?

Thanks a lot

Translated with [link removed] (free version)

deavik
New user
New user
Posts: 4
Joined: 2021-02-16 21:57

Re: VB.NET mit hMailServer

Post by deavik » 2021-02-17 14:43

Ok,
I'm starting to understand how it works, I wrote small vbs script to send a mail when an event occurs. (subject = "text")
If I run script not with hMS - is executed without problems, if script runs via hMS - then comes error message (logfile):

Code: Select all

"Script Error: Source: Laufzeitfehler in Microsoft VBScript - Error: 800A01C2 - Description: Falsche Anzahl an Argumenten oder ungültige Eigenschaftszuweisung: 'Protokoll' - Line: 63 Column: 0 - Code: (null)"
it is quite simple mail dispatch from vbs:

Code: Select all


Sub Protokoll()
' Send Email without Installing the SMTP Service
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "send@pc.lokal"
objEmail.To = "ku@pc.lokal"
objEmail.Subject = "Testmail" 
objEmail.Textbody = "Hier ist Data"

objEmail.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "10.2.100.003"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Your UserID on the SMTP server
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "send@pc.lokal"
'Your password on the SMTP server
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"

objEmail.Configuration.Fields.Update
objEmail.Send

End Sub
Why does error message come?
And 2 question -how can I read text with VBS body message and write to a variable?


Thanks a lot

palinka
Senior user
Senior user
Posts: 2472
Joined: 2017-09-12 17:57

Re: VB.NET mit hMailServer

Post by palinka » 2021-02-17 14:49

https://www.hmailserver.com/documentati ... ssage_send

Here is an example of using hmailserver api to send a message. This can be used within eventhandlers.vbs and called by rule or triggered at one of hmailserver's events.

palinka
Senior user
Senior user
Posts: 2472
Joined: 2017-09-12 17:57

Re: VB.NET mit hMailServer

Post by palinka » 2021-02-17 15:08

This is, SorenR's, our resident guru's, lookup match function.

Code: Select all

Function oLookup(strRegEx, strMatch, bGlobal)
   If strRegEx = "" Then strRegEx = StrReverse(strMatch)
   With CreateObject("VBScript.RegExp")
      .Pattern = strRegEx
      .Global = bGlobal
      .MultiLine = True
      .IgnoreCase = True
      Set oLookup = .Execute(strMatch)
   End With
End Function


strRegEx = "((?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+)"
Set Matches = oLookup( strRegEx, oMessage.Body, False )
For Each Match In Matches
   o_filename = Match.Value
Next
I got the RegEx for file path from here: https://stackoverflow.com/questions/641 ... xe#6416209

No guarantees or warranties. Especially since I'm doing this from my mobile. :D But it should at least point you in the right direction.

oMessage.Body is plain text body.
oMessage.HTMLBody is html.

deavik
New user
New user
Posts: 4
Joined: 2021-02-16 21:57

Re: VB.NET mit hMailServer

Post by deavik » 2021-02-17 16:06

palinka wrote:
2021-02-17 14:49
https://www.hmailserver.com/documentati ... ssage_send

Here is an example of using hmailserver api to send a message. This can be used within eventhandlers.vbs and called by rule or triggered at one of hmailserver's events.
I don't know what I'm doing wrong - I copied 1:1 to eventhandlers.vbs
my settings in - does not run
but always comes error in the logfile:

Code: Select all

"ERROR"	14296	"2021-02-17 15:04:00.819"	"Script Error: Source: Laufzeitfehler in Microsoft VBScript - Error: 800A01C2 - Description: Falsche Anzahl an Argumenten oder ungültige Eigenschaftszuweisung: 'Senden' - Line: 13 Column: 0 - Code: (null)"
line 13 does not exist

Code: Select all

Sub Senden()
dim oMessage
Set oMessage = CreateObject("hMailServer.Message")
oMessage.From = "Me <sender@domain.de>"
oMessage.FromAddress = "sender@domain.de"
oMessage.Subject = "Hi"
oMessage.AddRecipient "empfang@pc.local"
oMessage.Body = "This is the contents of the email."
oMessage.Save
End Sub


palinka
Senior user
Senior user
Posts: 2472
Joined: 2017-09-12 17:57

Re: VB.NET mit hMailServer

Post by palinka » 2021-02-17 17:13

deavik wrote:
2021-02-17 16:06
I don't know what I'm doing wrong - I copied 1:1 to eventhandlers.vbs
my settings in - does not run
but always comes error in the logfile:

Code: Select all

"ERROR"	14296	"2021-02-17 15:04:00.819"	"Script Error: Source: Laufzeitfehler in Microsoft VBScript - Error: 800A01C2 - Description: Falsche Anzahl an Argumenten oder ungültige Eigenschaftszuweisung: 'Senden' - Line: 13 Column: 0 - Code: (null)"
line 13 does not exist

Code: Select all

Sub Senden()
dim oMessage
Set oMessage = CreateObject("hMailServer.Message")
oMessage.From = "Me <sender@domain.de>"
oMessage.FromAddress = "sender@domain.de"
oMessage.Subject = "Hi"
oMessage.AddRecipient "empfang@pc.local"
oMessage.Body = "This is the contents of the email."
oMessage.Save
End Sub

AddRecipient requires 2 arguments: name and address, so change that line to:

Code: Select all

oMessage.AddRecipient "empfang@pc.local", "empfang@pc.local"

palinka
Senior user
Senior user
Posts: 2472
Joined: 2017-09-12 17:57

Re: VB.NET mit hMailServer

Post by palinka » 2021-02-18 18:13

Hmmm... This regex is more difficult than appears. Here's the best I could come up with:

Code: Select all

([a-zA-Z]\:\\|\\\\)([\w\s\.]+\\)*(\w+(\.\w+)?)
https://regex101.com/r/u0AFJ3/1

The problem is that the string to find is in the email body, so its surrounded by other stuff. In this version, no spaces are allowed in the file name, but spaces are allowed in the path.

It will pick up C:\Program Files\filename.ext but not C:\Program Files\file name.ext

Also, there could be false positives with this so it should be tested thoroughly before putting it into use.

deavik
New user
New user
Posts: 4
Joined: 2021-02-16 21:57

Re: VB.NET mit hMailServer

Post by deavik » 2021-02-24 15:41

I will consider all suggestions.
I have to do other projects unfortunately, that because of I come later to it and will report.

Thank you!

User avatar
SorenR
Senior user
Senior user
Posts: 4186
Joined: 2006-08-21 15:38
Location: Denmark

Re: VB.NET mit hMailServer

Post by SorenR » 2021-02-24 15:59

"oMessage" is a reserved word.

You CAN use it but eventually you will need it for something else :wink:

Also, there is NO built in Shell object (WShell) in the script engine that hMailServer is using. You will have to declare it yourself.

Check the scripting section of the forum, there are hundreds of examples.
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

Post Reply