Android email intent

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 device
i.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值