How to send emails from my Android application?

前端 未结 21 2683
春和景丽
春和景丽 2020-11-22 00:38

I am developing an application in Android. I don\'t know how to send an email from the application?

21条回答
  •  失恋的感觉
    2020-11-22 00:48

    simple try this one

     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        buttonSend = (Button) findViewById(R.id.buttonSend);
        textTo = (EditText) findViewById(R.id.editTextTo);
        textSubject = (EditText) findViewById(R.id.editTextSubject);
        textMessage = (EditText) findViewById(R.id.editTextMessage);
    
        buttonSend.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                String to = textTo.getText().toString();
                String subject = textSubject.getText().toString();
                String message = textMessage.getText().toString();
    
                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
                // email.putExtra(Intent.EXTRA_CC, new String[]{ to});
                // email.putExtra(Intent.EXTRA_BCC, 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 :"));
    
            }
        });
    }
    

提交回复
热议问题