0

Sending Email in Android using JavaMail API without using the default/built-in app

I'm trying to make an email app for my android according to the above link so I created three classes, made a simple layout but i can't make it to work? When i start the app in the emulator and the layout comes up and i press send there are no response. I suspect that the problem lies within the "send". Any tips?

I've also added, in the manifest. uses-permission android:name="android.permission.INTERNET"

And made a simple layout... and everytime i press the send button it doesnt send and only prints out "buttonbutton"!

package gaia.feedback.com;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class GaiaFeedbackActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button send = (Button) this.findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                try {   
                    GmailSender sender = new GmailSender("username@gmail.com", "password");
                    sender.sendMail("This is Subject",   
                            "This is Body",   
                            "user@gmail.com",   
                            "user@yahoo.com");   
                    System.out.println("buttonbutton");

                } catch (Exception e) {   
                    Log.e("SendMail", e.getMessage(), e);   
                    System.out.println("coolcool");
                } 

            }
        });

    }
}
Community
  • 1
  • 1
Henry Dang
  • 181
  • 3
  • 10

2 Answers2

1

If you need to send only mail you can use the below lines

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:"));
startActivity(Intent.createChooser(intent, "Send via..."));
Ishu
  • 5,357
  • 4
  • 16
  • 17
1
If you want to add subject and body then you can use the below method

private void sendMail(String subject,String body){
String mail = "mailto:?to=asd@gmail.com&subject="+subject+"&body="+body;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(mail));
startActivity(Intent.createChooser(intent, "Send via..."));
}
Ishu
  • 5,357
  • 4
  • 16
  • 17