How to send emails from my Android application?

前端 未结 21 2318
春和景丽
春和景丽 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:45

    I've been using this since long time ago and it seems good, no non-email apps showing up. Just another way to send a send email intent:

    Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
    intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
    intent.setData(Uri.parse("mailto:default@recipient.com")); // or just "mailto:" for blank
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 00:45

    Use this for send email...

    boolean success = EmailIntentBuilder.from(activity)
        .to("support@example.org")
        .cc("developer@example.org")
        .subject("Error report")
        .body(buildErrorReport())
        .start();
    

    use build gradle :

    compile 'de.cketti.mailto:email-intent-builder:1.0.0'
    
    0 讨论(0)
  • 2020-11-22 00:47

    I use the below code in my apps. This shows exactly email client apps, such as Gmail.

        Intent contactIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", getString(R.string.email_to), null));
        contactIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject));
        startActivity(Intent.createChooser(contactIntent, getString(R.string.email_chooser)));
    
    0 讨论(0)
  • 2020-11-22 00:48

    I was using something along the lines of the currently accepted answer in order to send emails with an attached binary error log file. GMail and K-9 send it just fine and it also arrives fine on my mail server. The only problem was my mail client of choice Thunderbird which had troubles with opening / saving the attached log file. In fact it simply didn't save the file at all without complaining.

    I took a look at one of these mail's source codes and noticed that the log file attachment had (understandably) the mime type message/rfc822. Of course that attachment is not an attached email. But Thunderbird cannot cope with that tiny error gracefully. So that was kind of a bummer.

    After a bit of research and experimenting I came up with the following solution:

    public Intent createEmailOnlyChooserIntent(Intent source,
        CharSequence chooserTitle) {
        Stack<Intent> intents = new Stack<Intent>();
        Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
                "info@domain.com", null));
        List<ResolveInfo> activities = getPackageManager()
                .queryIntentActivities(i, 0);
    
        for(ResolveInfo ri : activities) {
            Intent target = new Intent(source);
            target.setPackage(ri.activityInfo.packageName);
            intents.add(target);
        }
    
        if(!intents.isEmpty()) {
            Intent chooserIntent = Intent.createChooser(intents.remove(0),
                    chooserTitle);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    intents.toArray(new Parcelable[intents.size()]));
    
            return chooserIntent;
        } else {
            return Intent.createChooser(source, chooserTitle);
        }
    }
    

    It can be used as follows:

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("*/*");
    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(crashLogFile));
    i.putExtra(Intent.EXTRA_EMAIL, new String[] {
        ANDROID_SUPPORT_EMAIL
    });
    i.putExtra(Intent.EXTRA_SUBJECT, "Crash report");
    i.putExtra(Intent.EXTRA_TEXT, "Some crash report details");
    
    startActivity(createEmailOnlyChooserIntent(i, "Send via email"));
    

    As you can see, the createEmailOnlyChooserIntent method can be easily fed with the correct intent and the correct mime type.

    It then goes through the list of available activities that respond to an ACTION_SENDTO mailto protocol intent (which are email apps only) and constructs a chooser based on that list of activities and the original ACTION_SEND intent with the correct mime type.

    Another advantage is that Skype is not listed anymore (which happens to respond to the rfc822 mime type).

    0 讨论(0)
  • 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 :"));
    
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-22 00:49

    I solved this issue with simple lines of code as the android documentation explains.

    (https://developer.android.com/guide/components/intents-common.html#Email)

    The most important is the flag: it is ACTION_SENDTO, and not ACTION_SEND

    The other important line is

    intent.setData(Uri.parse("mailto:")); ***// only email apps should handle this***
    

    By the way, if you send an empty Extra, the if() at the end won't work and the app won't launch the email client.

    According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:

    public void composeEmail(String[] addresses, String subject) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:")); // only email apps should handle this
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    0 讨论(0)
提交回复
热议问题