Email not sending from android application

2019-08-21 10:38发布

I am developing in application in which there is one module of sending email. I have tried many tutorials but email is not sending and there is no exception or error there. I am sharing my all codes that i have used to send email here.

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

            emailIntent.setType("plain/text");

            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ email.getText().toString()});

            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, name.getText());

            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, smessage.getText());

          Email.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

This one is not working then i have tried

  Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                        "mailto","waleedahmed_786@yahoo.com", null));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
            emailIntent.setTypeAndNormalize("plain/text");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

It also didnt worked. Then i have tried

            Intent feedback = new Intent(Intent.ACTION_SEND);
            feedback.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
              //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
              //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
            feedback.putExtra(Intent.EXTRA_SUBJECT, subject);
            feedback.putExtra(Intent.EXTRA_TEXT, message);

              //need this to prompts email client only
            feedback.setType("text/plain");

              startActivity(Intent.createChooser(feedback, "Choose an Email client :"));

I have tried all these three codes but not even single code snippet worked , i have tested on Google nexus but it didn't work

Here is my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg">

 <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/header_bg" >

     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_weight="0.28"
         android:background="@drawable/back"
         android:orientation="vertical" >
     </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="38dp"
        android:layout_weight="0.28"
        android:paddingLeft="50dp"
        android:paddingTop="15dp"
        android:text="@string/ReceiptOrganizer"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>



 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" 
     android:background="@drawable/receipt_added_bg">

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="20dp" >

         <TextView
             android:id="@+id/textView2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Name" />

         <EditText
             android:id="@+id/name"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:ems="10"
             android:inputType="textPersonName" >

             <requestFocus />
         </EditText>

     </LinearLayout>

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="20dp" >

         <TextView
             android:id="@+id/textView2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Email" />

         <EditText
             android:id="@+id/email"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:ems="10"
             android:inputType="textEmailAddress" >

             <requestFocus />
         </EditText>

     </LinearLayout>

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="20dp" >

         <TextView
             android:id="@+id/textView2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Feedback" />

         <EditText
             android:id="@+id/msg"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:ems="10"
             android:inputType="textMultiLine" />


     </LinearLayout>

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="20dp" >

         <Button
             android:id="@+id/send"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:gravity="left"/>

     </LinearLayout>

 </LinearLayout>

3条回答
Evening l夕情丶
2楼-- · 2019-08-21 11:07

please set the type as setType("application/octet-stream") instead of plain/text and also i request you to confirm that whether you had internet permission in your manifest file.

try the following code snippet which has setType ad application/octext-stream as i mentioned above

// Creating a Object for the Class Intent

Intent sendIntent;

// Since we want to send ,we are using ACTION_SEND

sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("application/octet-stream");
sendIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {"san123@gmail.com"});
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Test");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hy Testing");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));

startActivity(Intent.createChooser(sendIntent, "Send Mail"));

and also add following permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
查看更多
该账号已被封号
3楼-- · 2019-08-21 11:17
String to = toEmail.getText().toString();
String subject = emailSubject.getText().toString();
String message = emailBody.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
// need this to prompts email client only
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client"));
查看更多
趁早两清
4楼-- · 2019-08-21 11:22

Try

Intent send = new Intent(Intent.ACTION_SENDTO);
        String uriText = "mailto:" + Uri.encode("a@b.com") + 
                  "?subject=" + Uri.encode("the subject") + 
                  "&body=" + Uri.encode("the body of the message");
        Uri uri = Uri.parse(uriText);

        send.setData(uri);
        startActivity(Intent.createChooser(send, "Send mail..."));

Add Permission in Manifest for Internet Access.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
查看更多
登录 后发表回答