简体   繁体   English

Android中的mailto链接是否需要进行URL编码才能使邮件意图正常工作?

[英]Do mailto links in Android need to be URL encoded for mail Intents to work?

Environment: 环境:

Windows Server 2012 Standard Windows Server 2012标准版
Android Studio 0.5.8 - android-19 Android Studio 0.5.8-android-19
JRE 1.7.0_51 JRE 1.7.0_51

Description of problem: 问题描述:

Overriding URL loading in an Android web view to start an email intent fails to identify mailto properties. 在Android Web视图中覆盖URL加载以启动电子邮件意图无法识别mailto属性。

MailTo.parse("mailto:?subject=Something%20interesting%20from%20Google&body=GooglePlus%20(%20https://google.com/plus%20)")

returns: 返回:

key: "to"
value: "/plus)"

when email intent is prepared by: 通过以下方式准备电子邮件意图时:

Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
    intent.putExtra(Intent.EXTRA_TEXT, body);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_CC, cc);
    intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setType("message/rfc822");
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    return intent;

then: 然后:

if (url.startsWith("mailto:")) {
        MailTo mailTo = MailTo.parse(url);
        Intent intent = newEmailIntent(mailTo.getTo(), mailTo.getSubject(), mailTo.getBody(), mailTo.getCc());
        _mainActivity.startActivity(Intent.createChooser(intent, "Send Email..."));
        return true;
    }

If I remove the colon in the transfer protocol or replace it with %3a then the subject and body are parsed as expected. 如果我在传输协议中删除了冒号或将其替换为%3a,则将按预期解析主题和正文。

Question : in the title ^. 问题 :标题中的^。

Thanks in advance. 提前致谢。

It appears that MailTo has a problem parsing a mailto: URL which contains within the subject or body fields characters that appear in a URL (eg : and /). 似乎MailTo在解析mailto:URL时遇到问题,该URL在主题或正文字段中包含出现在URL中的字符(例如:和/)。 I encountered this when trying to parse a mailto URL which contained a web URL in the body field (ie I wanted the email body to contain a link that the recipient could click). 在尝试解析在主体字段中包含Web URL的mailto URL时,我遇到了此问题(即,我希望电子邮件主体包含收件人可以单击的链接)。

What I did to overcome this is to make a modified copy of the MailTo class (called MailTo2), and modified the parse method to be as follows: 我要克服的问题是制作MailTo类的修改副本(称为MailTo2),并将parse方法修改如下:

/**
 * Parse and decode a mailto scheme string.  This parser implements
 * RFC 2368.  The returned object can be queried for the parsed
 * parameters.
 * @param theUrl String containing a mailto URL
 * @return MailTo2 object
 * @exception IllegalArgumentException if the URL scheme is not mailto.
 */
public static MailTo2 parse(String theUrl) 
  throws IllegalArgumentException 
{
  MailTo2 theMailTo2 = null;
  String  theNoSchemeUrl = null;
  String  theQuery = null;
  Uri     theEmailUri = null;
  int     thePos = 0;

  // Validate the given URL.
  if (theUrl == null) 
  {
    throw new NullPointerException();
  }
  if (!isMailTo(theUrl)) 
  {
    throw new IllegalArgumentException("Not a mailto scheme: " + theUrl);
  }

  // Strip the scheme as the URI parser can't cope with it.
  theNoSchemeUrl = theUrl.substring(MAILTO_SCHEME.length());

  // Get the query part of the URL.
  thePos = theNoSchemeUrl.indexOf('?');
  if (thePos >= 0)
  {
    theQuery = theNoSchemeUrl.substring(thePos+1);
    theNoSchemeUrl = theNoSchemeUrl.substring(0, thePos);
  }

  // Create a URL from the URL string (without scheme and query).
  theEmailUri = Uri.parse(theNoSchemeUrl);

  // Construct the MailTo2 instance which will be returned.
  theMailTo2 = new MailTo2();

  // Parse out the query parameters
  if (theQuery != null) 
  {
    String[] theQueryParts = theQuery.split("&");
    String   theName = null;
    String   theValue = null;

    for (String theQueryPart : theQueryParts) 
    {
      thePos = theQueryPart.indexOf('=');
      if (thePos <= 0)  continue;
      theName = theQueryPart.substring(0, thePos);
      theValue = theQueryPart.substring(thePos+1).trim();

      // insert the headers with the name in lower-case so that
      // we can easily find common headers
      theMailTo2.HEADERS.put(Uri.decode(theName).toLowerCase(Locale.ROOT),
                             Uri.decode(theValue));
    }
  }

  // Address can be specified in both the headers and just after the
  // mailto line. Join the two together.
  String address = theEmailUri.getPath();
  if (address != null) 
  {
    String addr = theMailTo2.getTo();
    if (addr != null) 
    {
      address += ", " + addr;
    }
    theMailTo2.HEADERS.put(TO, address);
  }

  return theMailTo2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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