如何在phonegap android中发送电子邮件

时间:2012-01-31 01:14:12

标签: android cordova

我尝试使用webintent http://smus.com/android-phonegap-plugins在phonegap android 4应用程序中发送电子邮件。但我真的不了解网站或插件自述文件。

e.g。怎么用呢?

window.plugins.webintent.startActivity({
    action: WebIntent.ACTION_VIEW,
    url: 'geo:0,0?q=' + address}, 
    function() {}, 
    function() {alert('Failed to open URL via Android Intent')};
);

<a href="mailto:support@fareastgadget.com&subject=Report%20issues&body=Reporting%20following%20issues:">

如果我使用html标记,Android手机只会过滤网址并导致电子邮件收件人成为整个字符串。

任何人都可以提供一些示例代码或教程,了解如何在phonegap中发送电子邮件(虽然不是必需的webintent)吗?

感谢。

2 个答案:

答案 0 :(得分:1)

使用标记电子邮件解决方案,客户端回来说电子邮件是空白的,而不是在某些设备上添加内容。所以我开始实现web intent插件(正如你从包名中看到的那样),但我最终只是实现了一个新的电子邮件插件。

这是Java类。

package com.webintent.emailcomposer;

import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import com.phonegap.api.Plugin;

public class EmailComposer extends Plugin {
    public final String ACTION_SEND_EMAIL = "SendEmail";

    public PluginResult execute(String action, JSONArray arg1, String callbackId) {

    PluginResult result = new PluginResult(Status.INVALID_ACTION);
    if (action.equals(ACTION_SEND_EMAIL)) {
        try {
            String email = arg1.getString(0);
            String subject = arg1.getString(1);
            String message = arg1.getString(2);
            this.sendEmail(email, subject, message);
            result = new PluginResult(Status.OK);
        }
        catch (JSONException ex) {
            result = new PluginResult(Status.JSON_EXCEPTION, ex.getMessage());
        }           
    }
    return result;
}

private void sendEmail(String email, String subject, String message) {
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});
    intent.putExtra(
            Intent.EXTRA_TEXT, 
            Html.fromHtml(new StringBuilder()
                .append(message)
                .toString())
            );
    intent.setType("text/html");
    this.ctx.startActivity(Intent.createChooser(intent, "Choose email account"));
}
}

请记住更新plugins.xml

这是js插件代码

var EmailPlugin = function () {};

cordova.addConstructor(function() {
   return cordova.addPlugin("email", new EmailPlugin());
});

EmailPlugin.prototype.send = function (email, subject, message){
cordova.exec(function(){ alert('email success')}, 
    function(){ alert('email fail')}, 
    'EmailComposer', 
    'SendEmail', 
    [email, subject, message]);
}

最后,你这样调用插件。

window.plugins.email.send(email, subject, body);

注意我没有在参数中包含回调,但你可以看到它们的去向。

答案 1 :(得分:0)

嗯,这看起来像Boris Smus为使用谷歌地图提供的代码。电子邮件示例是这样的。

Android.sendEmail = function(subject, body) { 
var extras = {};
extras[WebIntent.EXTRA_SUBJECT] = subject;
extras[WebIntent.EXTRA_TEXT] = body;
window.plugins.webintent.startActivity({ 
  action: WebIntent.ACTION_SEND,
  type: 'text/plain', 
  extras: extras 
}, 
function() {}, 
function() {
  alert('Failed to send email via Android Intent');
}
); 
};

我自己也遇到了麻烦,就像我一样,它会调出MMS编辑器而不是电子邮件......

*编辑:以为我有一些东西,但没有...其他来源似乎建议使用mailto链接。

*编辑2:忽略这个并看到另一个答案。

相关问题