How send email from Android processing?

I can’t find working example for EmailSend in Android Mode. :frowning:

Hi,

You might want to look at those links :

(how-to-send-email-with-attachment-in-background-android-mode)

Thanks for replay,josephh! But I’m looking for an example SendEmail for “Processing in AndroidMode”, not pure Android (in Android Studio)…

@miasoft === i dont understand your post: you can easily send email or sms with Processing using quite the same code that you use with AS; supposing that you accept to use some mail app installed on your phone.

1 Like

I have a simple task:

  1. get phone’s SN and geolocation
  2. periodically send this data to the email in accordance with the SN
    I’m stuck with Sending Email :disappointed:
    I use Temboo library (www.temboo.com) in Processing (Java mode). It works fine. It’s clean and simple.

This test sketch:

import com.temboo.core.*;
import com.temboo.Library.Utilities.Email.*;

// Create a session using your Temboo account application details
TembooSession session = new TembooSession("miasoft", "myFirstApp", "xxxxxxxxxxxxxx");

void setup() {
  // Run the SendEmail Choreo function
  runSendEmailChoreo();
}

void runSendEmailChoreo() {
  // Create the Choreo object using your Temboo session
  SendEmail sendEmailChoreo = new SendEmail(session);

  // Set inputs
  sendEmailChoreo.setFromAddress("myemail");
  sendEmailChoreo.setServer("smtp.my.ru");
  sendEmailChoreo.setUsername("myname");
  sendEmailChoreo.setPort("465");
  sendEmailChoreo.setUseSSL("1");
  sendEmailChoreo.setSubject("SMTP-test from Temboo-library");
  sendEmailChoreo.setToAddress("my@my.ru");
  sendEmailChoreo.setMessageBody("Test smtp from Temboo");
  sendEmailChoreo.setPassword("mypassword");

  // Run the Choreo and store the results
  SendEmailResultSet sendEmailResults = sendEmailChoreo.run();
  
  // Print results
  println(sendEmailResults.getSuccess());

}

But I can’t use this sketch in Processing for Android Mode - get errors.
On the other hand, there are many examples on the internet for sending an SMTP email for pure AndroidStudio. But these examples are too complicated for me - I do not know how to use them in Processing for Android mode :disappointed:

Perhaps this might help

1 Like

Thanks! I download the project from here
https://github.com/arpit999/SendMail
and build it in the AS. It works 100%. I think I need to port the module Gmail.JAVA in my Processing project, but I don’t understand how to do it correctly…
Gmail.java

package com.tranetech.openspace.sendmail;

import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class GMail {

	final String emailPort = "587";// gmail's smtp port
	final String smtpAuth = "true";
	final String starttls = "true";
	final String emailHost = "smtp.gmail.com";
    final String fromUser = "asdasdasd@gmail.com";
    final String fromUserEmailPassword = "adsfasdasd";

	String fromEmail="sasdasdasd@gmail.com";
	String fromPassword=" axdfczsadcfdxc";
	List<String> toEmailList;
	String emailSubject="Координаты";
	String emailBody;

	Properties emailProperties;
	Session mailSession;
	MimeMessage emailMessage;

	public GMail() {

	}

	public GMail(String fromEmail, String fromPassword,
			List<String> toEmailList, String emailSubject, String emailBody) {
		this.fromEmail = fromEmail;
		this.fromPassword = fromPassword;
		this.toEmailList = toEmailList;
		this.emailSubject = emailSubject;
		this.emailBody = emailBody;

		emailProperties = System.getProperties();
		emailProperties.put("mail.smtp.port", emailPort);
		emailProperties.put("mail.smtp.auth", smtpAuth);
		emailProperties.put("mail.smtp.starttls.enable", starttls);
		Log.i("GMail", "Mail server properties set.");
	}

	public MimeMessage createEmailMessage() throws AddressException,
			MessagingException, UnsupportedEncodingException {

		mailSession = Session.getDefaultInstance(emailProperties, null);
		emailMessage = new MimeMessage(mailSession);

		emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
		for (String toEmail : toEmailList) {
			Log.i("GMail", "toEmail: " + toEmail);
			emailMessage.addRecipient(Message.RecipientType.TO,
					new InternetAddress(toEmail));
		}

		emailMessage.setSubject(emailSubject);
		emailMessage.setContent(emailBody, "text/html");// for a html email
		// emailMessage.setText(emailBody);// for a text email
		Log.i("GMail", "Email Message created.");
		return emailMessage;
	}

	public void sendEmail() throws AddressException, MessagingException {

		Transport transport = mailSession.getTransport("smtp");
		transport.connect(emailHost, fromEmail, fromPassword);
		Log.i("GMail", "allrecipients: " + emailMessage.getAllRecipients());
		transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
		transport.close();
		Log.i("GMail", "Email sent successfully.");
	}

}

import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Draft;
import com.google.api.services.gmail.model.Message;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

These are all the import statements found in

https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwie672z977vAhUIQkEAHUygCUIQFjABegQIFhAD&url=https%3A%2F%2Fdevelopers.google.com%2Fgmail%2Fapi%2FSendEmail.java&usg=AOvVaw2WbkqwanOxWzM7VK28ws7d

@miasoft === and why note use the “standard” android ACTION_SEND and let the user choose the app for sending the mail??? it is much more simpler i think…Code below shows how
to do; of course, instead of mouseReleased you have to code some timer;f you want also you can change the xtra or add another one.

import android.content.Intent;
import android.widget.Button;

Button envoi;
boolean send= false;
String eTo;
String eMsg;
String eSubj;



void setup(){
  size(800,1000);
  background(255,0,0);
eTo = "akenaton.docks2a@gmail.com";
eMsg = "Coucou, comment ça va";
eSubj= "des nouvelles...";
}

void draw(){
  
  if (send){
    
    envoieMessage();
  }

};


public void mouseReleased(){
 send= true;
}

void envoieMessage(){
  Intent it = new Intent(Intent.ACTION_SEND);
                it.putExtra(Intent.EXTRA_EMAIL, new String[]{eTo.toString()});
                it.putExtra(Intent.EXTRA_SUBJECT,eSubj.toString());
                it.putExtra(Intent.EXTRA_TEXT,eMsg.toString());
                it.setType("message/rfc822");
                startActivity(Intent.createChooser(it,"Choose Mail App"));
                send = false;
}

dont forget to add permissions (internet); you can also modify the manifest to create a filter for your activity filter ACTION_SEND and mime type message/rfc822

I’d like my sketch send email automatically (without user’s interaction). How to change

startActivity(Intent.createChooser(it,"Choose Mail App"));

???

Not saying this is that case, but i knkw that android makes things particularly difficult in some cases or near impossible so that they can ensure security and cross compatibility (at least im guessing). I tried to create a file or folder picker and could not find any documentation to do this. It would always point to set an intent to lpen the users fave file browser. And then good luck reading the data because it uses virtual storage and the link to the file it retrieves does not play nice anywhere else.

Long story short this may be intentional behaviour from android.

@miasoft ===
@paulgoux === yes, you are absolutely right!!!
first poinst is that you cannot be sure (without the chooser screen) which email app is installed and used by the phone user; of course you can “bet” that Gmail is installed and override ths chooser but that is very dangerous because in some future Gmail app can change its package name; so if i am able to do that, (adding pakage class name in my intent) i am against this work around…
second point is that Android does not permit to “send an email automatically” for evident security reasons; (suppose that an app send emails without your agreement!)here also you can find a workaround but it is not simple , because you have to get the profile from the user, password and so on (some apps do that) and i cannot see the interest of doing so instead of clicking the gmail button. more details here:: https://groups.google.com/g/android-developers/c/Je3-y-tSAtE?pli=1
In case you want to skip the chooser screen, as i have told (and told that, in my mind it is not a good idea, )you can use the below code inside the previous one:; you can also add some exception and a toast, but that is also without interest.

void envoieMessage(){
  Intent it = new Intent(Intent.ACTION_SEND);
 
                it.setPackage("com.google.android.gm");
              it.putExtra(Intent.EXTRA_EMAIL, new String[]{eTo.toString()});
                it.putExtra(Intent.EXTRA_SUBJECT,eSubj.toString());
                it.putExtra(Intent.EXTRA_TEXT,eMsg.toString());
                it.setType("message/rfc822");
                //startActivity(Intent.createChooser(it,"Choose Mail App"));  //comment this line
                startActivity(it);
                send = false;
}
1 Like

Thanks, akenaton! I implemented your version. Everything works fine!

@miasoft === ok, put answered for others