简体   繁体   中英

How to send email by clicking on submit button in Android application

I am creating an android application.in which it has a feedback form. Now, as the user clicks on "Submit Comments" button, this should send all the details to my email address, all the details that user entered in the form. I've seen so many examples and questions here, but didn't get proper answer. I don't know how to do it. I am new in android. Please help me.

在此处输入图片说明

You can try this on your send button click event:

Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"info@xxx.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "title"); 
            i.putExtra(Intent.EXTRA_TEXT, message);//message is your details
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(about.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }

Try this on Submit Button:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
        "mailto","email@email.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(intent, "Choose an Email client :"));

If you don't have a specific recipient - go like this:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
        "mailto",null, null));
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 :"));

            }

For sending email, you will have to use the inbuilt/installed email clients/apps which have been configured with a correct email account. There is no API in android for sending mail. Sending receiving emails uses protocols which has been implemented by the email apps. Implementing that in your app would make it very complex.

Better option for you would be call a web service and pass the data to the server and store it in DB. If you really want to send email, then send the received data at the server as email. Depending on the server you are using, you will be able to find connectors for emails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM