Search code examples
androidandroid-intentwebviewwebviewclient

Android WebView Intent send email with "to" pre-filled


below is the html code, when i click this. i want open email and fill the "to" field with "someone@gmail.com"

<a href="mailto:someone@gmail.com">Send Mail</a>

i'm already try this code, its open the email but still "to" field is empty

public boolean shouldOverrideUrlLoading(WebView view, String url) {
       if (url.startsWith("tel:")) {
           Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
           startActivity(intent);
           return true;
       } else if (url.startsWith("mailto:")) {
           String mail = url.replace("mailto:", "");
           Intent intent = new Intent(Intent.ACTION_SEND);
           intent.setType("message/rfc822");
           intent.putExtra(Intent.EXTRA_EMAIL, mail );
           startActivity(Intent.createChooser(intent, "Send Email"));
           return true;
       } else {
           return false;
       }
}

Whats wrong with my code, thanks


Solution

  • This is how I did it using the MailTo class:

    MailTo mailTo = MailTo.parse(url);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mailTo.getTo()});
    intent.putExtra(Intent.EXTRA_TEXT, mailTo.getBody());
    intent.putExtra(Intent.EXTRA_SUBJECT, mailTo.getSubject());
    intent.putExtra(Intent.EXTRA_CC, mailTo.getCc());
    intent.setType("message/rfc822");
    startActivity(intent);