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

2022-01-23 00:00:00 gmail android-intent android java

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

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

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

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

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 只是阻止这些链接?有没有办法在 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?

添加了我的代码片段

发送链接的代码:

    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 代码:

Android code to respond to intent:

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

推荐答案

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

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

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

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

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

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

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.

相关文章