Sending mail with attachment from java script fails

  • Platform information:
    • Hardware: Fujitsu , Incore I5 16 GB RAM 250 GB SSD
    • OS: Windows 11
    • Java Runtime Environment: Zulu17
    • openHAB version: 4.2
  • Issue of the topic: Email with attachment is not send from rule, without attachment email sending works

what I want to do:
sending a email if a shutter item gets closed /open.
logging is configured that a seperate log is written in …/logs/shutter_01.log
got an error ,
I can’t see if the shutter_01.log is not accessible to the rule,
so I do a test with a file on Volume d:

Maybe I did not uderstand where the rule code is executed
volume d: is on the same computer as openHAB is running

  • Rules code related to the issue
  • rule language is javascript

this works and the mail is send and received

console.log('execute rule Test Mail backup 002')
 var Things = Java.type('org.openhab.core.model.script.actions.Things');
 var mailAction = Things.getActions('mail', 'mail:smtp:t_Strato_SMTP');
 var aMailHeader ="Test mail Backup 002";
 var aMailMsg = "some content";
// send mail
var successSendMail =  mailAction.sendMail('xxxxxx@xxxx.de', aMailHeader, aMailMsg);
console.log('execute rule Test Mail backup 002 success: ', successSendMail);

Blockquote
2024-09-28 20:07:44.780 [INFO ] [ion.script.ui.r_test_backup_mail_002] - execute rule Test Mail backup 002
2024-09-28 20:07:45.144 [INFO ] [ion.script.ui.r_test_backup_mail_002] - execute rule Test Mail backup 002 success: true

this does not work:

console.log('execute rule Test Mail backup 002')
var Things = Java.type('org.openhab.core.model.script.actions.Things');
var mailAction = Things.getActions('mail', 'mail:smtp:t_Strato_SMTP');
var aFileUrl = 'file://D:test_datei.txt';
var attachmentUrlList = ["file://D:test_datei.txt"];
var successSendMailWithAttachment = mailAction.sendMailWithAttachment('xxxxxx@xxxx.de', aMailHeader,aMailMsg, aFileUrl );
console.log('execute rule Test Mail backup 002 success: ', successSendMailWithAttachment);

Blockquote
2024-09-28 21:26:17.065 [INFO ] [ion.script.ui.r_test_backup_mail_002] - execute rule Test Mail backup 002
2024-09-28 21:26:17.421 [WARN ] [mail.internal.action.SendMailActions] - Could not send mail: Error at index 0 in: “test_datei.txt”
2024-09-28 21:26:17.422 [INFO ] [ion.script.ui.r_test_backup_mail_002] - execute rule Test Mail backup 002 success: false

another try
this does not work

console.log('execute rule Test Mail backup 002')
var Things = Java.type('org.openhab.core.model.script.actions.Things');
var mailAction = Things.getActions('mail', 'mail:smtp:t_Strato_SMTP');
var aMailHeader ="Test mail Backup 002";
var aMailMsg = "some content";

var attachmentUrlList = ["file://D:test_datei.txt"];
var successSendMailWithAttachment = mailAction.sendMailWithAttachments(''xxxxxx@xxxx.de', aMailHeader,aMailMsg, attachmentUrlList);
console.log('execute rule Test Mail backup 002 success: ', successSendMailWithAttachment);

Blockquote
2024-09-28 21:22:45.723 [INFO ] [ion.script.ui.r_test_backup_mail_002] - execute rule Test Mail backup 002
2024-09-28 21:22:46.154 [WARN ] [mail.internal.action.SendMailActions] - Could not send mail: Error at index 0 in: “test_datei.txt”
2024-09-28 21:22:46.155 [INFO ] [ion.script.ui.r_test_backup_mail_002] - execute rule Test Mail backup 002 success: false

What I’m doing wrong?
Help appreciated

This isn’t wrong but you can replace

var Things = Java.type('org.openhab.core.model.script.actions.Things');
var mailAction = Things.getActions('mail', 'mail:smtp:t_Strato_SMTP');

with

var mailAction = actions.get('mail', 'mail:smtp:t_Strato_SMTP');

You pretty much never need to have Java.type in a JS Scripting rule unless you are dealing with an Action that requires a Java Object as the argument. sendMainWithAttachments is one of those cases, as illustrated by the example provided in the docs: Mail - Bindings | openHAB

import java.util.List

val List<String> attachmentUrlList = newArrayList(
  "http://some.web/site/snap.jpg&param=value",
  "file:///tmp/201601011031.jpg")
val mailActions = actions.get("mail","mail:smtp:sampleserver")
mailActions.sendHtmlMailWithAttachments("mail@example.com", "Test subject", "<h1>Header</h1>This is the mail content.", attachmentUrlList)

I can’t say why the single attachment doesn’t work behyond the fact that don’t you need a / or \\ after the D:?