Android email intent

来源:互联网 发布:智器PDF阅读软件 编辑:程序博客网 时间:2024/05/17 06:37
Intent i = new Intent(Intent.ACTION_SEND);  //i.setType("text/plain"); //use this line for testing in the emulator  i.setType("message/rfc822") ; // use from live devicei.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});  i.putExtra(Intent.EXTRA_SUBJECT,"subject goes here");  i.putExtra(Intent.EXTRA_TEXT,"body goes here");  startActivity(Intent.createChooser(i, "Select email application."));



public class MainActivity extends Activity {private EditText recipient;private EditText subject;private EditText body;   @Override   protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      recipient = (EditText) findViewById(R.id.recipient);      subject = (EditText) findViewById(R.id.subject);      body = (EditText) findViewById(R.id.body);            Button sendBtn = (Button) findViewById(R.id.sendEmail);      sendBtn.setOnClickListener(new View.OnClickListener() {         public void onClick(View view) {         sendEmail();         // after sending the email, clear the fields         recipient.setText("");         subject.setText("");         body.setText("");         }   });   }   protected void sendEmail() {      String[] recipients = {recipient.getText().toString()};      Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));      // prompts email clients only      email.setType("message/rfc822");      email.putExtra(Intent.EXTRA_EMAIL, recipients);      email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());      email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());      try {    // the user can choose the email client         startActivity(Intent.createChooser(email, "Choose an email client from..."));           } catch (android.content.ActivityNotFoundException ex) {         Toast.makeText(MainActivity.this, "No email client installed.",         Toast.LENGTH_LONG).show();      }   }   }


public static Intent createEmailIntent(final String toEmail,                                        final String subject,                                        final String message){    Intent sendTo = new Intent(Intent.ACTION_SENDTO);    String uriText = "mailto:" + Uri.encode(toEmail) +            "?subject=" + Uri.encode(subject) +            "&body=" + Uri.encode(message);    Uri uri = Uri.parse(uriText);    sendTo.setData(uri);    List<ResolveInfo> resolveInfos =         getPackageManager().queryIntentActivities(sendTo, 0);    // Emulators may not like this check...    if (!resolveInfos.isEmpty())    {        return sendTo;    }    // Nothing resolves send to, so fallback to send...    Intent send = new Intent(Intent.ACTION_SEND);    send.setType("text/plain");    send.putExtra(Intent.EXTRA_EMAIL,               new String[] { toEmail });    send.putExtra(Intent.EXTRA_SUBJECT, subject);    send.putExtra(Intent.EXTRA_TEXT, message);    return Intent.createChooser(send, "Your Title Here");}



0 0