Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MailTo does not working in Android WebView

I am working on Android Webview of a website. I have include mailto for Contact and Email to a Friend. The code snippet at web side:

For Contact: <a href="mailto:[email protected]">[email protected]</a>

For Email to a Friend: <a href="mailto:?subject=Check out this product&amp;body=I found this awesome product%20http://www.example.com/product-detail/61/" title="Email to a Friend" class=""><span>Email to a Friend</span></a>

I include the following code snippet for the Android side at shouldOverrideUrlLoading method:

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if( url.startsWith("http:") || url.startsWith("https:") ) {
            return false;
        }

        else if (url.startsWith("tel:")) {
            Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
            startActivity(tel);
            return true;
        }
        else if(url.startsWith("mailto:")) {
            MailTo mailTo = MailTo.parse(url);
            String emailAddress = mailTo.getTo();
            String subject = mailTo.getSubject();
            String body = mailTo.getBody();
            String cc = mailTo.getCc();
            Intent mail = new Intent(Intent.ACTION_SEND,Uri.parse(url));
            mail.setType("application/octet-stream");
            mail.putExtra(Intent.EXTRA_EMAIL, new String[]{emailAddress});
            mail.putExtra(Intent.EXTRA_SUBJECT, subject);
            mail.putExtra(Intent.EXTRA_TEXT, body);
            mail.putExtra(Intent.EXTRA_CC, cc);
            startActivity(mail);
            return true;
        }
        return false;
    }

This is working fine with Contact. But, it does not working with Email to a Friend. There is blank in Subject and Composed email field, but field To include /product-detail/105/ which must be blank. When i open website in browser in mobile it works fine. So, what should i need to do in above code for webview to handle this issue ? What can be done to solve this ?

like image 203
Satan Pandeya Avatar asked Aug 25 '26 16:08

Satan Pandeya


1 Answers

I solve my issue by using this :

 public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("tel:")) {
            Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
            startActivity(tel);
            return true;
        }
        else if (url.contains("mailto:")) {
             view.getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;

        }else {       
            view.loadUrl(url);
            return true;
        }
    }
like image 98
Satan Pandeya Avatar answered Aug 27 '26 06:08

Satan Pandeya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!