# How send email from Android processing?

**URL:** https://discourse.processing.org/t/how-send-email-from-android-processing/28616
**Category:** Processing for Android
**Created:** [March 17, 2021, 6:34pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616 "2021-03-17T18:34:44Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![miasoft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miasoft/32/1902_2.png) [@miasoft](https://discourse.processing.org/u/miasoft)
#### Post date: [March 17, 2021, 6:34pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/1 "2021-03-17T18:34:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 18, 2021, 11:08am UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/2 "2021-03-18T11:08:00Z")

</div>

Hi,

You might want to look at those links :

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

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/22538/how-to-send-email-with-attachment-in-background-android-mode)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

> <https://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a>

> **[How to send an email with JavaMail API in Android ?](https://ssaurel.medium.com/how-to-send-an-email-with-javamail-api-in-android-2fc405441079)**
>
> As an Android developer, you’re used to send email thanks to Android standard API provided in standard SDK. To achieve that, you use…

---

<div class="post-metadata">

### Author: ![miasoft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miasoft/32/1902_2.png) [@miasoft](https://discourse.processing.org/u/miasoft)
#### Post date: [March 18, 2021, 12:15pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/3 "2021-03-18T12:15:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [March 19, 2021, 5:29pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/4 "2021-03-19T17:29:55Z")

</div>

@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.

---

<div class="post-metadata">

### Author: ![miasoft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miasoft/32/1902_2.png) [@miasoft](https://discourse.processing.org/u/miasoft)
#### Post date: [March 20, 2021, 2:59am UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/5 "2021-03-20T02:59:48Z")

</div>

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 😞  
I use Temboo library ([www.temboo.com](https://www.temboo.com)) in Processing (Java mode). It works fine. It’s clean and simple.

This test sketch:

```auto
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 😞

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [March 20, 2021, 8:27am UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/6 "2021-03-20T08:27:03Z")

</div>

Perhaps this might help

> <https://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a>

---

<div class="post-metadata">

### Author: ![miasoft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miasoft/32/1902_2.png) [@miasoft](https://discourse.processing.org/u/miasoft)
#### Post date: [March 20, 2021, 10:44am UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/7 "2021-03-20T10:44:11Z")

</div>

Thanks! I download the project from here  
[https://github.com/arpit999/SendMail](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

```auto
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.");
	}

}

```

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [March 20, 2021, 1:06pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/8 "2021-03-20T13:06:55Z")

</div>

```auto
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](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)

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [March 20, 2021, 2:56pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/9 "2021-03-20T14:56:39Z")

</div>

@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.

```auto
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

---

<div class="post-metadata">

### Author: ![miasoft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miasoft/32/1902_2.png) [@miasoft](https://discourse.processing.org/u/miasoft)
#### Post date: [March 20, 2021, 3:14pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/10 "2021-03-20T15:14:26Z")

</div>

> [@akenaton](#):
>
> 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

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

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

```

???

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [March 20, 2021, 3:25pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/11 "2021-03-20T15:25:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [March 21, 2021, 7:10pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/12 "2021-03-21T19:10:32Z")

</div>

@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](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.

```auto
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;
}

```

---

<div class="post-metadata">

### Author: ![miasoft](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miasoft/32/1902_2.png) [@miasoft](https://discourse.processing.org/u/miasoft)
#### Post date: [March 23, 2021, 5:04pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/13 "2021-03-23T17:04:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [March 23, 2021, 6:06pm UTC](https://discourse.processing.org/t/how-send-email-from-android-processing/28616/14 "2021-03-23T18:06:56Z")

</div>

@miasoft === ok, put answered for others
