In this Android Studio article iam going to show you How To Send Email In Android Studio . so for good explanation you can watch the video for this article at the end .
Also you can read more android development articles
1: Android Development Articles
Also you can check Python GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
5: wxPython GUI Development Tutorials
1: First of all create a new project in your Android Studio, and choose Empty Activity
also iam using API 20 for this project.
2: After that you need to open your main_activity.xml and add a Button in your xml layout
file like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity"> <Button android:id="@+id/sendBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Email" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.321" /> </android.support.constraint.ConstraintLayout> |
So now open your MainActivity.java file, first you need to make reference for your Button
and after that set onClickListener for your button. also we need to create a sendMail() method.
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 54 55 56 57 58 |
package com.forogh.parwiz.sendingemail; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button btnSend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSend = findViewById(R.id.sendBtn); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendEmail(); } }); } public void sendEmail() { String[] to = {"parwizforogh@gmail.com"}; String[] cc = {"parwiz@codeloop.org"}; Intent mail = new Intent(Intent.ACTION_SEND); mail.setData(Uri.parse("mailto:")); mail.setType("text/plain"); mail.putExtra(Intent.EXTRA_EMAIL, to); mail.putExtra(Intent.EXTRA_CC, cc); mail.putExtra(Intent.EXTRA_TEXT, "This email will be send " ); mail.putExtra(Intent.EXTRA_SUBJECT, "Codeloop.org"); startActivity(Intent.createChooser(mail, "mailto") ); } } |
You can see in the sendEmail() method, first of all we have created two strings for storing our to and also cc email addresses. we are going to send email using intent object, so an Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast receivers and services. Intents are also used to transfer data between activities. Intents are used generally for starting a new activity using startActivity(). so first of all you need to create the object of the intent and for the action we need to use ACTION_SEND.
Also you can watch the complete video for How To Send Email In Android Studio
Subscribe and Get Free Video Courses & Articles in your Email
This line:
String[] to = {“parwizforogh@gmail.com”};
I thought it will be automatically insert into the email program ??
But the user has to put in the email of the receiver ??
greetings
Bernhard