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

时间:2014-05-22 19:17:22

标签: android android-intent mailto

环境:

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

问题描述:

在Android网页视图中覆盖网址加载以启动电子邮件意图无法识别mailto属性。

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

返回:

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

准备电子邮件意图时:


    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;

然后:


    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;
    }

如果我在转移协议中删除了冒号或将其替换为%3a,则会按预期解析主体和正文。

问题:标题为^。

提前致谢。

1 个答案:

答案 0 :(得分:0)

MailTo似乎在解析mailto:URL时出现问题,该URL包含URL中出现的主题或正文字符(例如:和/)。我在尝试解析包含正文字段中的网址的mailto URL时遇到了这种情况(即我希望电子邮件正文包含收件人可以点击的链接)。

我为克服这个问题所做的是制作MailTo类的修改副本(称为MailTo2),并将解析方法修改为如下:

/**
 * 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;
}