Saving oMessage in OnAcceptMessage to Get MessageID

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
cblaze22
Normal user
Normal user
Posts: 185
Joined: 2011-08-30 20:16

Saving oMessage in OnAcceptMessage to Get MessageID

Post by cblaze22 » 2019-05-07 07:08

I found this in the code base below. The Message ID is not available in the OnAcceptMessage, however if you hit .Save like it shows below, will that assign an ID to the message so we can send it out for delivery right away. PLEASE SOMEONE CONFIRM!

Code: Select all

[Test]
      public void TestAddTextDuringSending()
      {
         hMailServer.Account oAccount1 = SingletonProvider<Utilities>.Instance.AddAccount(_domain, "test@test.com", "test");

         // Send a message to the account.
         hMailServer.Message oMessage = new hMailServer.Message();
         Assert.AreEqual(0, oMessage.State);

         hMailServer.Scripting scripting = SingletonProvider<Utilities>.Instance.GetApp().Settings.Scripting;

         string signature = "MySignature";

         string script = "Sub OnAcceptMessage(oClient, oMessage) " + Environment.NewLine +
                               " Call EventLog.Write(\"Subject:\" +oMessage.Subject)" + Environment.NewLine +
                               " Call EventLog.Write(\"Date:\" +oMessage.Date)" + Environment.NewLine +
                               " Call EventLog.Write(\"Body:\" +oMessage.Body)" + Environment.NewLine +
                               " oMessage.Body = oMessage.Body & \"" + signature + "\" " + Environment.NewLine +
                               " oMessage.Save() " + Environment.NewLine +
                          "End Sub"  + Environment.NewLine +  Environment.NewLine;

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

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by SorenR » 2019-05-07 12:33

cblaze22 wrote:
2019-05-07 07:08
I found this in the code base below. The Message ID is not available in the OnAcceptMessage, however if you hit .Save like it shows below, will that assign an ID to the message so we can send it out for delivery right away. PLEASE SOMEONE CONFIRM!

Code: Select all

[Test]
      public void TestAddTextDuringSending()
      {
         hMailServer.Account oAccount1 = SingletonProvider<Utilities>.Instance.AddAccount(_domain, "test@test.com", "test");

         // Send a message to the account.
         hMailServer.Message oMessage = new hMailServer.Message();
         Assert.AreEqual(0, oMessage.State);

         hMailServer.Scripting scripting = SingletonProvider<Utilities>.Instance.GetApp().Settings.Scripting;

         string signature = "MySignature";

         string script = "Sub OnAcceptMessage(oClient, oMessage) " + Environment.NewLine +
                               " Call EventLog.Write(\"Subject:\" +oMessage.Subject)" + Environment.NewLine +
                               " Call EventLog.Write(\"Date:\" +oMessage.Date)" + Environment.NewLine +
                               " Call EventLog.Write(\"Body:\" +oMessage.Body)" + Environment.NewLine +
                               " oMessage.Body = oMessage.Body & \"" + signature + "\" " + Environment.NewLine +
                               " oMessage.Save() " + Environment.NewLine +
                          "End Sub"  + Environment.NewLine +  Environment.NewLine;
You cannot force creation of oMessage.ID. The event OnAcceptMessage() is when all of the email (headers, body, attachments etc.) is received - just before your server returns "SENT: 250 Queued (1.609 seconds)" to the originating server. Message is still in memory about to be written to a file on disk. Once the file ([GUID].eml) is created in .\hMailServer\Data a record is created in database for indexing thus the oMessage.ID is created.

So you see, you cannot do anything with oMessage.ID until Sub OnDeliveryStart(oMessage).
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

cblaze22
Normal user
Normal user
Posts: 185
Joined: 2011-08-30 20:16

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by cblaze22 » 2019-05-07 13:33

Is there a method that is available, that has the ID, but is called before it goes into the queue or right after it goes in the queue.

My goal is to send the email immediately based on a header instead of say waiting in the queue for a hour to be processed because an important email is backed up.

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

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by SorenR » 2019-05-07 15:28

cblaze22 wrote:
2019-05-07 13:33
Is there a method that is available, that has the ID, but is called before it goes into the queue or right after it goes in the queue.

My goal is to send the email immediately based on a header instead of say waiting in the queue for a hour to be processed because an important email is backed up.
Nope...
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

estradis
Normal user
Normal user
Posts: 156
Joined: 2014-09-09 10:47

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by estradis » 2019-06-07 14:30

SorenR wrote:
2019-05-07 12:33

... Message is still in memory about to be written to a file on disk. ...
Sorry SorenR, but I don't agree!

When the event OnAcceptMessage is fired, the file already exists in Data Directory. This fact is an essential part of our Eventhandler.vbs as we pass the event handling to another (high performant) server and process only the results in EventHandler.vbs. If the file wouldn't exist at this time, our complete scripting wouldn't work. (See anonymized snippet from our trace log before the event was passed to the event server below)

BUT somewhere after OnAcceptMessage the filename (GUID) will change, I guess in that moment it will be copied to the user directory.

Code: Select all

[2019.06.03 19:03:22.046]: EventHandlers.vbs: DEBUG: sEventName="OnAcceptMessage"
[2019.06.03 19:03:22.046]: EventHandlers.vbs: DEBUG: oMessage.From=""***** ******" <*****@outside.com>"
[2019.06.03 19:03:22.046]: EventHandlers.vbs: DEBUG: oMessage.FromAddress="*****@outside.com"
[2019.06.03 19:03:22.046]: EventHandlers.vbs: DEBUG: oMessage.Filename="C:\Program Files\hMailServer\Data\{E32586DA-8E04-43A9-822B-24C99AC78EC6}.eml"
[2019.06.03 19:03:22.046]: EventHandlers.vbs: DEBUG: sMsgFileName="C%3A%5CProgram%20Files%5ChMailServer%5CData%5C{E32586DA-8E04-43A9-822B-24C99AC78EC6}.eml"
[2019.06.03 19:03:22.046]: EventHandlers.vbs: DEBUG: sAdressesInHeaders=""john.doe@example.com" john.doe@example.com;jane.doe@example.com"
[2019.06.03 19:03:22.046]: EventHandlers.vbs: DEBUG: oRecipient.Address="john.doe@example.com"
[2019.06.03 19:03:22.046]: EventHandlers.vbs: DEBUG: oRecipient.OriginalAddress="john.doe@example.com"

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

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by SorenR » 2019-06-07 15:11

Yes I know ... From a later topic... It think it is a matter of semantic, when the "file is written" I believe it is filed under the specific account on disk, but I have not verified this yet..

https://www.hmailserver.com/forum/viewt ... 78#p212078

I do not remember seing the file changing it's name in the process however.
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

estradis
Normal user
Normal user
Posts: 156
Joined: 2014-09-09 10:47

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by estradis » 2019-06-07 15:38

SorenR wrote:
2019-06-07 15:11
Yes I know ... From a later topic... It think it is a matter of semantic, when the "file is written" I believe it is filed under the specific account on disk, but I have not verified this yet..

https://www.hmailserver.com/forum/viewt ... 78#p212078

I do not remember seing the file changing it's name in the process however.
I searched several times for the filenames from the trace log, but I could never find the files. The file name will be changed definitively! :roll:

And by the way, I just read my previous post again and should probably work on my semantics myself. Looks like I'm getting out of practice. :?

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

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by SorenR » 2019-06-07 17:27

estradis wrote:
2019-06-07 15:38
SorenR wrote:
2019-06-07 15:11
Yes I know ... From a later topic... It think it is a matter of semantic, when the "file is written" I believe it is filed under the specific account on disk, but I have not verified this yet..

https://www.hmailserver.com/forum/viewt ... 78#p212078

I do not remember seing the file changing it's name in the process however.
I searched several times for the filenames from the trace log, but I could never find the files. The file name will be changed definitively! :roll:

And by the way, I just read my previous post again and should probably work on my semantics myself. Looks like I'm getting out of practice. :?
I used: EventLog.Write(oMessage.FileName) as the first line in every trigger using "oMessage", never recorded my findings but IIRC it was the same filename all the way.

I have SAMoveVsCopy=1 in my .INI file, that may play a role ... :?: :mrgreen:
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

estradis
Normal user
Normal user
Posts: 156
Joined: 2014-09-09 10:47

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by estradis » 2019-06-11 10:30

SorenR wrote:
2019-06-07 17:27
estradis wrote:
2019-06-07 15:38
SorenR wrote:
2019-06-07 15:11

I do not remember seing the file changing it's name in the process however.
I searched several times for the filenames from the trace log, but I could never find the files. The file name will be changed definitively! :roll:
I used: EventLog.Write(oMessage.FileName) as the first line in every trigger using "oMessage", never recorded my findings but IIRC it was the same filename all the way.

I have SAMoveVsCopy=1 in my .INI file, that may play a role ... :?: :mrgreen:
During the events the filename will be the same as you can see in the trace log from the example above

Code: Select all

[2019.06.03 12:57:50.996]: EventHandlers.vbs: DEBUG: sEventName="OnSMTPData"
[2019.06.03 12:57:50.996]: EventHandlers.vbs: DEBUG: oMessage.Filename="C:\Program Files\hMailServer\Data\{2CEB9555-3840-4102-B94C-03D23A94ADB5}.eml"
...
[2019.06.03 12:57:51.839]: EventHandlers.vbs: DEBUG: sEventName="OnAcceptMessage"
[2019.06.03 12:57:51.839]: EventHandlers.vbs: DEBUG: oMessage.Filename="C:\Program Files\hMailServer\Data\{2CEB9555-3840-4102-B94C-03D23A94ADB5}.eml"
But as I wrote above
estradis wrote:
2019-06-07 14:30
... somewhere after OnAcceptMessage the filename (GUID) will change ...
Unfortunatly we currently don't trace OnDeliveryStart Messages, so I cannot say what the filename is in that event.

I also don't believe that SAMoveVsCopy=1 plays a role because SA will be processed between OnSMTPData and OnAcceptMessage, but the filename is not changed when the second event is fired.

By the way is that an official but undocumentated setting?

User avatar
jimimaseye
Moderator
Moderator
Posts: 8631
Joined: 2011-09-08 17:48

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by jimimaseye » 2019-06-11 14:13

estradis wrote:
2019-06-11 10:30
I also don't believe that SAMoveVsCopy=1 plays a role because SA will be processed between OnSMTPData and OnAcceptMessage, but the filename is not changed when the second event is fired.

By the way is that an official but undocumentated setting?
https://www.hmailserver.com/forum/viewt ... 10&t=30900
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

estradis
Normal user
Normal user
Posts: 156
Joined: 2014-09-09 10:47

Re: Saving oMessage in OnAcceptMessage to Get MessageID

Post by estradis » 2019-06-12 10:04

Thank you for sharing the link. :D
(Just a mind: Maybe you want to add the link to your personal signature as well?)

Post Reply