I'm trying to send an email programmatically in kotlin.
Here's my code, which I translated from a java example I found on the Web.
package com.example.emaildemo
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editTextTo:EditText = findViewById(R.id.editText)
val editTextSubject:EditText = findViewById(R.id.editText2)
val editTextMessage:EditText = findViewById(R.id.editText3)
val button1:Button = findViewById(R.id.button)
button1.setOnClickListener(View.OnClickListener{
val to = editTextTo.getText().toString()
val subject = editTextSubject.getText().toString()
val message = editTextMessage.getText().toString()
val intent = Intent(Intent.ACTION_SEND)
val addressees = arrayOf(to)
intent.putExtra(Intent.EXTRA_EMAIL, addressees)
intent.putExtra(Intent.EXTRA_SUBJECT, subject)
intent.putExtra(Intent.EXTRA_TEXT, message)
intent.setType("message/rfc822")
startActivity(Intent.createChooser(intent, "Select Email Sending App :"))
})
}
}
It seems to work correctly, except that the "TO" field isn't copied into the user's email app. The java code said:
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{TO});
and the android developer docs say that Intent.EXTRA_EMAIL is "A string array of all "To" recipient email addresses." How should I define the addressees
variable?
EDIT: I've discover that if I hard code my email address,
val addressees = arrayOf("me@email.com")
then the app works as expected. When I get to the email app, my address is in the "To:" field, and I can send mail to myself. If, however, the I type the same address in the program, it doesn't show up in the email client. Moreover, when I look at it under the debugger, the intent shows exactly the same values both ways. Curiouser and curiouser.
EDIT2
Here is my XML file. (Somehow I can't get the first two lines indented correctly.)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="To"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Subject"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="Message"
android:id="@+id/textView3"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="4"
android:id="@+id/editText3"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here To Send Email from android application programmatically"
android:id="@+id/button"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</RelativeLayout>