Breaking News

How to Send Email in Android app using Intent | Contact us Form in Android app

How to Send Email in Android app using Intent | Contact us Form in Android app


In this post, we create a Contact Us form in the Android App. We create 2 Edit text and a Send button to send email in the android app.

We get a subject and message from our app and pass it to the email client.

Create 2 EditText and a Button in activity_main.xml

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout 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:orientation="vertical"  
   tools:context=".Contact_us">  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="contact@harpreetstudio.com"  
     android:enabled="false"  
     android:padding="10dp"  
     />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="Subject"  
     android:id="@+id/email_subject"  
     android:padding="10dp"  
     />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="Message"  
     android:maxLines="4"  
     android:padding="10dp"  
     android:id="@+id/email_message"  
     />  
   <Button  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Send"  
     android:id="@+id/email_send"/>  
 </LinearLayout>  

Now, we create an Intent to send values to Email client in you Android.

MainActivity.java
 package studio.harpreet.sampleproject;  
 import androidx.appcompat.app.AppCompatActivity;  
 import android.content.Intent;  
 import android.net.Uri;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.Toast;  
 public class Contact_us extends AppCompatActivity {  
     EditText et_subject,et_message;  
     Button btn;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_contact_us);  
     et_subject = findViewById(R.id.email_subject);  
     et_message = findViewById(R.id.email_message);  
     btn = findViewById(R.id.email_send);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         String subject = et_subject.getText().toString().trim();  
         String message = et_message.getText().toString().trim();  
         String email = "contact@harpreetstudio.com";  
         if(subject.isEmpty())  
         {  
           Toast.makeText(Contact_us.this, "Please add Subject", Toast.LENGTH_SHORT).show();  
         }  
         else if(message.isEmpty())  
         {  
           Toast.makeText(Contact_us.this, "Please add some Message", Toast.LENGTH_SHORT).show();  
         }  
         else  
         {  
           String mail = "mailto:" + email +  
               "?&subject=" + Uri.encode(subject) +  
               "&body=" + Uri.encode(message);  
           Intent intent = new Intent(Intent.ACTION_SENDTO);  
           intent.setData(Uri.parse(mail));  
           try {  
             startActivity(Intent.createChooser(intent,"Send Email.."));  
           }  
           catch (Exception e)  
           {  
             Toast.makeText(Contact_us.this, "Exception: "+e.getMessage(), Toast.LENGTH_SHORT).show();  
           }  
         }  
       }  
     });  
   }  
 }  


Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

12 comments:

  1. Android App Maker makes it easy to create an app of your own. Cyberflix APK

    ReplyDelete
  2. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks email extractor 9.0

    ReplyDelete
  3. I have read your article couple of times because your views are on my own for the most part. It is great content for every reader. how to send customized email from gmail

    ReplyDelete
  4. A great content material as well as great layout. Your website deserves all of the positive feedback it’s been getting. I will be back soon for further quality contents. one funnel away challenge

    ReplyDelete
  5. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. spacedesk

    ReplyDelete
  6. Deciding on a brand name is one of the common mistakes of startup companies. Having names that is thoroughly thought off is an edge that can lead your company to success. Email Extractor

    ReplyDelete
  7. Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share. https://satta-king.center/

    ReplyDelete
  8. The seller stamps his name in the clear space under "Request Form" and mails them to forthcoming clients. At the point when he gets requests (and he will in the event that he mails them consistently), he keeps his bonus, which is generally about half.

    ReplyDelete
  9. This is important, though it's necessary to help you head over to it weblink: LinkedIn Profile Scraper

    ReplyDelete
  10. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. Zendable

    ReplyDelete
  11. Thank you so much for ding the impressive job here, everyone will surely like your post. best remote access tools

    ReplyDelete
  12. iPhone Facebook app development has changed the scenario of the smart phone. The social networking site has been remodeled by the iPhone Facebook apps developer to give you all the applications with their complete view and full functionality. spotify++

    ReplyDelete