0

I want to send an email from android without user interaction. I use this code Send email from android

It's running good on my Samsung phone(which version is 2.3.2 and API label 9). But the same app is not working in my Walton phone (which version is 4.2.2 and API label 17). I use minsdk 8 and targetsdk 19. Here is my manifest file

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.hiddencamera"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />

         <uses-permission android:name="android.permission.CAMERA" />
        <uses-feature android:name="android.hardware.camera" android:required="false" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />

         <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.hiddencamera.Main"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>
Community
  • 1
  • 1
corei11
  • 153
  • 1
  • 9
  • You have failed to specify how it fails (if you are going to develop on Android, you must learn to use logcat) but with 90% probably this is a NetworkOnMaindThreadException. – Chris Stratton Feb 22 '15 at 06:22

1 Answers1

0

You can refer to the following code. It works fine.

public class Email extends Activity implements View.OnClickListener {

    EditText personsEmail, intro, personsName, stupidThings, hatefulAction,
            outro;
    // Your mail address here
    String emailAdd;
    Button sendEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);
        initializeVars();
        sendEmail.setOnClickListener(this);
    }

    private void initializeVars() {
        // TODO Auto-generated method stub
        sendEmail = (Button) findViewById(R.id.bSentEmail);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        //Your mail addresses needs to be converted to Array/List before passing, so you can append more mail_id's too
        String emailaddress[] = { emailAdd };
        String message = "Your mail content here ";

        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject of the mail");
        emailIntent.setType("plain/text");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
        startActivity(emailIntent);

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

In Manifest just remember to add the permission for Internet once only. You have mentioned it twice.Though it do not have any effect I thunk, but still you should be careful with everything.

rohanpro
  • 132
  • 7
  • This does not accomplish the goal of the question - launching an Activity will send mail with user interaction, while they want to do it without that. – Chris Stratton Feb 22 '15 at 06:23
  • You can launch this Intent, even without user interaction. You just need to paste the code inside onClick method in the method/ place (timer, BroadcastReceiver or something) that will trigger the this email sending part you want to do. – rohanpro Feb 22 '15 at 15:50
  • No. You can bring up the email client, but that will not send without user interaction, so this is not a solution. – Chris Stratton Feb 22 '15 at 15:52
  • It will be good if you can paste email related part of your code here. Without it understanding the problem will be an issue too. – rohanpro Feb 22 '15 at 16:14