Page 1 of 1

Automatic POP3 fetch with delay between each download

Posted: 2019-08-23 13:05
by derelvis
As I recently had some trouble with handshake errors during download of external accounts via POP3, I managed to get rid of the problem by using a vbs script included in a task scheduler task. In my case the problem occurred because many pop3 fetches started at the same time and then ended up in about 50% of "TCPConnection - TLS/SSL handshake failed" errors. So I looked for a way to get a little delay between the trigger of the external downloads. The script is based on a version from jimimaseye, thanks for the support!
For reference: https://www.hmailserver.com/forum/viewt ... =7&t=34245

WHAT IT DOES
It loops through all enabled external accounts in all active accounts of all active domains, starts the donwload and adds a configurable delay between each fetch.

HOW TO USE
Adjust the CONFIG part for your needs (Admin username, Admin Password, verbose mode, delay)
verbose=1 shows some more information for debugging when started in a cmd box
delay=1000 means setting a delay of 1 sec between every fetch

HOW TO IMPLEMENT
Either start it in a cmd prompt by hand or create a task scheduler task like this:
Program/Script: C:\Windows\System32\cscript.exe - Arguments: "<pathto>\Automatic_POP3_fetch.vbs"
Trigger for task: every x minutes

THE CODE (click 'SELECT ALL' and copy/paste):
Automatic_POP3_fetch.vbs:

Code: Select all

'  Script forces download of all enabled external accounts for all active accounts of every active domain
'  Use in task scheduler: Program/Script: C:\Windows\System32\cscript.exe - Arguments: "<pathto>\Automatic_POP3_fetch.vbs"

Option Explicit

' #### CONFIG START ####
	Const HMSADMINUSER = "Administrator"	' Admin username
	Const HMSADMINPWD = ""			' Admin password
	verbose = 0 				' set to 1 for some debugging output
	delay = 1000				' delay between fetches in ms
' #### CONFIG END ####

' Objects
Dim oApp, oDomain, oAccount, oExtAccount

' Numeric
Dim x, y, z, i, verbose, delay

	Set oApp = CreateObject("hMailServer.Application")
	Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

	i=1
	If verbose = 1 then wscript.echo(" Domain.Count: " & oApp.Domains.Count)
	For x = 1 To oApp.Domains.Count
		Set oDomain = oApp.Domains.Item(x-1)
		If oDomain.Active Then
			If verbose = 1 then wscript.echo(" - Domain " & x & ": " & oDomain.Name)
			If verbose = 1 then wscript.echo("  - Accounts.Count: " & oDomain.Accounts.Count)
			For y = 1 to oDomain.Accounts.Count
				Set oAccount = oDomain.Accounts.Item(y-1)
				If oAccount.active Then
					If verbose = 1 then wscript.echo("  - Address " & y & ": " & oAccount.Address)
					If verbose = 1 then wscript.echo("   - FetchAccounts.Count: " & oAccount.FetchAccounts.Count)
						For z = 1 To oAccount.FetchAccounts.Count
							Set oExtAccount = oAccount.FetchAccounts.Item(z-1)
							If oExtAccount.Enabled Then
								If verbose = 1 then wscript.echo("    - FetchAccount " & z & ": " & oExtAccount.Name)
								oExtAccount.DownloadNow()
								wscript.echo("      Download " & i & " - Account: " & oExtAccount.Name & " --> Ext. Account: " & oAccount.address)
								wscript.sleep delay 'wait for x sec until fetching next account
								i = i + 1
							End if 
						Next
				End if
			Next
		End if
	Next