getInstance().sendMessage - Email message does not include attachment - wrong path

22 views
Skip to first unread message

P5music

unread,
Feb 10, 2021, 12:24:01 PM2/10/21
to CodenameOne Discussions
My CodenameApp can export some data by sending it by e-mail, in the form of json files.
This is the method:

public static void sendMyJsonFile(Form form,String fileName,String JSONText)
    {
        File file;

       try {

           file=new File("temp");
           if (!file.exists()) file.mkdir();
           File[] files=file.listFiles();
           for (int i=0;i<files.length;i++)
           {
               files[i].delete();
           }

           file=new File("temp/"+fileName+".ext");
           System.out.println("fileName "+file.getAbsolutePath());
        OutputStream os = Storage.getInstance().createOutputStream(file.getAbsolutePath());
        os.write(JSONText.getBytes("UTF-8"));
        os.flush();
        os.close();
    } catch (IOException e) {
        return;
    }
    Message m = new Message("");
    m.getAttachments().put(file.getAbsolutePath(), "text/plain");
        System.out.println("attachments "+m.getAttachments().size());
    getInstance().sendMessage(new String[] {""}, file.getName(), m);

    }

A temp folder is created if it does not exist yet.
Then its content is deleted it it contains files.

The file is created and sent.

An alternate method version can use
file=new File(tempFolder,fileName+".ext");
but it is the same, please read the following:

I am testing it in the simulator and on Android device.
I can say that:
1-the file is not attached at all on Android and also on Windows10 (the mail app opens and composes the message).
2-on the simulator the temp folder is created in .cn1\ but the files are outside, so they are not deleted the next time the method is called.
Maybe it is also the reason for the file not being attached.
Paths seem to be correct.
log:
fileName file://home/temp/prova3.ext
attachments 1
sending message to  <- this is not from my app

No errors are issued.

What's wrong?
Thanks in advance

Shai Almog

unread,
Feb 10, 2021, 9:59:33 PM2/10/21
to CodenameOne Discussions
 I suggest you use FileSystemStorage and save the files to the app home directory which is a special case on Android. Things like attachments etc. Work better when they're stored in the app home folder.

P5music

unread,
Feb 11, 2021, 5:18:26 AM2/11/21
to CodenameOne Discussions
It is the same also without putting the file in the "temp" folder under the private home directory.
And
file name are created like file__home_filename.
I also prefixed the file name with "file__home_" but always the attachment is null, the fileUrl field is empty, and also mimetype is wrong because it is like image, if I look into it at debug.
Is this an issue in
Message.getAttachments().put();
?
And with
"I suggest you use FileSystemStorage and save the files to the app home directory which is a special case on Android"
are you saying that, as a matter of fact, Storage does not work on Android?

Shai Almog

unread,
Feb 12, 2021, 12:12:18 AM2/12/21
to CodenameOne Discussions
Storage works. I was talking about the File API vs. FileSystemStorage. The latter has a get app home directory which is a specific special case for external intents. I suggest using that. Look in the Javadoc samples where this approach is used.
Reply all
Reply to author
Forward
0 new messages