java 如何发送带有链接的电子邮件以打开 Android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5697899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:21:55  来源:igfitidea点击:

How to send email with link to open Android application

javaandroidgmailandroid-intent

提问by Nick Banks

I have created some code that emails an html email to a given address. The email contains a link like the following:

我创建了一些代码,将 html 电子邮件通过电子邮件发送到给定地址。该电子邮件包含如下链接:

<a href="crystalcloud://android?4567">Click to validate account</a>

I was testing this by sending it to my gmail account. For some reason, when I receive the email, it will not let me click on the link. If I put this link in a web page, my application starts up fine.

我通过将它发送到我的 gmail 帐户来测试它。出于某种原因,当我收到电子邮件时,它不会让我点击链接。如果我将此链接放在网页中,我的应用程序就可以正常启动。

Is there something special I have to do to get this link to show up in an email, or goes gmail just block these kind of links? Is there anyway to get around this issue in gmail?

有什么特别的我必须做才能让这个链接显示在电子邮件中,还是 gmail 只是阻止这些类型的链接?有没有办法在 gmail 中解决这个问题?

EDIT: Added snippets of my code

编辑:添加了我的代码片段

Code to send link:

发送链接的代码:

    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));
    message.setSubject("New Crystal Cloud User");
    message.setContent("Thank you for creating a new Crystal Cloud account!<br><a href=\"crystalcloud://android?4567">Click to validate account</a>", "text/html; charset=utf-8");
    Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

Android code to respond to intent:

响应意图的Android代码:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="crystalcloud" />
        </intent-filter>

回答by skorulis

This is a problem with the android email clients that they don't show links as clickable unless they are http or https.

这是 android 电子邮件客户端的一个问题,除非它们是 http 或 https,否则它们不会将链接显示为可点击。

My solution was to use a http scheme to open my app:

我的解决方案是使用 http 方案打开我的应用程序:

<data android:scheme="http" android:host="com.company.app" android:port="3513" />

This works but in annoying because it prompts the user to open the app in either the browser or your application. Another solution is to have a link to a website that then does a javascript redirect to your app though this can cause more problems.

这有效但很烦人,因为它提示用户在浏览器或您的应用程序中打开应用程序。另一种解决方案是拥有一个指向网站的链接,然后将 javascript 重定向到您的应用程序,尽管这可能会导致更多问题。

回答by Rasika

In your mail header put in Content-Type: text/htmlto indicate that the content is in HTML to indicate that the mail is coming in HTML format. Then the mail client will render the HTML appropriately. It will help to enclose the link in a <html></html>block as well.

在您的邮件标题中放入Content-Type: text/html以指示内容为 HTML 以指示邮件以 HTML 格式发送。然后邮件客户端将适当地呈现 HTML。它也将有助于将链接包含在一个<html></html>块中。

回答by Binesh Kumar

I am facing same problem at last i got solution. You can able to share text as well as link

我面临同样的问题,最后我得到了解决方案。您可以共享文本和链接

  String link_val = "http://play.google.com/store/apps/details?id=" + MainActivity.this.getPackageName();
        String body = "<a href=\"" + link_val + "\">" + link_val + "</a>";
        String data = "Hello I am using this App.\nIt has large numbers of Words meaning with synonyms.\nIts works offline very smoothly.\n\n" + body;
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(data));