send email using android studio
1 |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SendEmail extends AppCompatActivity { private EditText eTo; private EditText eSubject; private EditText eMsg; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_email); eTo = (EditText)findViewById(R.id.txtTo); eSubject = (EditText)findViewById(R.id.txtSub); eMsg = (EditText)findViewById(R.id.txtMsg); btn = (Button)findViewById(R.id.btnSend); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it = new Intent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_EMAIL, new String[]{eTo.getText().toString()}); it.putExtra(Intent.EXTRA_SUBJECT,eSubject.getText().toString()); it.putExtra(Intent.EXTRA_TEXT,eMsg.getText()); it.setType("message/rfc822"); startActivity(Intent.createChooser(it,"Choose Mail App")); } }); } } |
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="20dp" android:paddingRight="20dp" android:orientation="vertical" > <EditText android:id="@+id/txtTo" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="To" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/txtSub" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:hint="Subject" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtTo" /> <EditText android:id="@+id/txtMsg" android:layout_width="match_parent" android:layout_height="280dp" android:layout_marginTop="8dp" android:layout_weight="1" android:gravity="top" android:hint="Message" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtSub" /> <Button android:id="@+id/btnSend" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="Send" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtMsg" /> </android.support.constraint.ConstraintLayout> |