B4J Question Send Email using Default Email Provider

D

Deleted member 103

Guest
Hi,

is there a way to send e-mail with the Windows default Email Provider?
If a button is pressed, the standard email provider should open automatically so that you can send an email.
 
D

Deleted member 103

Guest
I tried this Java class, but somehow it doesn't work or I'm doing something wrong.
B4X:
Sub Process_Globals
    Private NativeMe As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
...
    NativeMe = Me
...
End Sub

Sub GetDefaultEMailClient_Click()
    NativeMe.InitializeNewInstance("RunningDefaultMailClient", Null)
    NativeMe.RunMethod("client", Array("[email protected]"))
End Sub

#if Java
package org.kodejava.example.awt;

import java.awt.*;
import java.io.IOException;
import java.net.URI;

Public class RunningDefaultMailClient {
    Public static void Main(String[] args) {
        // Get an instance of Desktop. If the platform doesn't support Desktop API an
        // UnsupportedOperationException will be thrown.
        Desktop desktop = Desktop.getDesktop();

        Try {
            // Open user-default mail client application.
            desktop.mail();
        } Catch (IOException e) {
            e.printStackTrace();
        }

        Try {
            // Open user-default mail client with the email message fields information.
            String message = "mailto:[email protected][email protected]&subject=First%20Email";
            URI uri = URI.create(message);
            desktop.mail(uri);
        } Catch (IOException e) {
            e.printStackTrace();
        }
    }
}
#End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
D

Deleted member 103

Guest
Thanks Manfred! :)
So easy?
Is it also possible to include attachments?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Your code does work, and you might be able to add attachments using it. It just needed a few 'tweaks' to get it working.
 
Upvote 0
D

Deleted member 103

Guest
Your code does work, and you might be able to add attachments using it. It just needed a few 'tweaks' to get it working.
But what 'tweaks' are needed?
The "mailto" function does not allow attachments .
 
Upvote 0

Jorge M A

Well-Known Member
Licensed User
Taken from here: https://stackoverflow.com/questions/5233556/using-mailto-to-send-email-with-an-attachment

Nope, this is not possible at all. There is no provision for it in the mailto: protocol, and it would be a gaping security hole if it were possible.

The best idea to send a file, but have the client send the E-Mail that I can think of is:

  • Have the user choose a file
  • Upload the file to a server
  • Have the server return a random file name after upload
  • Build a mailto: link that contains the URL to the uploaded file in the message body
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
This is how I changed the java part.
Call it with NativeMe.RunMethod("sendEmail",null)
Java:
#if java
import java.awt.*;
import java.io.IOException;
import java.net.URI;

public static void sendEmail() {
    // Get an instance of Desktop. If the platform doesn't support Desktop API an
    // UnsupportedOperationException will be thrown.
    Desktop desktop = Desktop.getDesktop();
    if (desktop==null) System.out.println("desktop failed");
    try {
        // Open user-default mail client with the email message fields information.
        String message = "mailto:[email protected][email protected]&subject=First%20Email&body=hello%20there";
        URI uri = URI.create(message);
        desktop.mail(uri);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
#End If
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
what if there is no mail client installed/configured? most people use gmail webmail these days.

you could use vbscript but then you need to rely on an smtp relay and port 25 that needs to be open on local & router firewalls.

a better option would be posting to a server and mail it from there then you're always sure that mails will be processed and you can add your own logging for it.
 
Upvote 0
D

Deleted member 103

Guest
what if there is no mail client installed/configured? most people use gmail webmail these days.

you could use vbscript but then you need to rely on an smtp relay and port 25 that needs to be open on local & router firewalls.

a better option would be posting to a server and mail it from there then you're always sure that mails will be processed and you can add your own logging for it.
Hi @sorex ,
my problem is that i still don't know what to do.

The customer should be able to send 2 different emails:
1. an email with a license request to me
2. Send the data you have created with my application, by e-mail or otherwise.
 
Upvote 0
D

Deleted member 103

Guest
If this is an app for a few customers then you can use SMTP with an email account that you provide.
Thanks Erel, but I'm trying to organize the whole thing now without email.
I simply give my users the necessary information and they should send me an email with their default program.
If that doesn't work, then I have to think of something else.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
As written earlier you could just add a contact form to you program and post it to some website and use php to send the mail to you.
Then you don't need to rely on smtp servers and the required port 25 which is usually closed in business envirenments exept for the local mail server.
 
Upvote 0
D

Deleted member 103

Guest
As written earlier you could just add a contact form to you program and post it to some website and use php to send the mail to you.
Then you don't need to rely on smtp servers and the required port 25 which is usually closed in business envirenments exept for the local mail server.
This is my solution without email.
I think if a customer needs the application, then buy it too, right?
 

Attachments

  • license.JPG
    license.JPG
    125.8 KB · Views: 209
Upvote 0
Top