简体   繁体   English

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

[英]how to send email in phonegap android

I tried to use webintent http://smus.com/android-phonegap-plugins to send email in phonegap android 4 application. 我试图使用webintent http://smus.com/android-phonegap-plugins在phonegap android 4应用程序中发送电子邮件。 But I don't really understand the website nor the plugins readme file. 但是我不太了解网站,也不了解插件自述文件。

eg how to use this? 例如如何使用?

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

If I use html markup, the android phone will just filter the url and cause email recipient to be the whole string. 如果我使用html标记,则Android手机将仅过滤url并导致电子邮件收件人成为整个字符串。

Can anyone provide some sample codes or tutorial on how to sending email in phonegap (not necessary webintent though)? 谁能提供一些示例代码或教程,说明如何在phonegap中发送电子邮件(尽管不是必需的webintent)?

Thanks. 谢谢。

Used the markup email solution and the client came back saying that the emails were blank, not adding the content on certain devices. 使用了标记电子邮件解决方案,客户端返回说电子邮件为空白,而不是在某些设备上添加内容。 So I set out to implement the web intent plugin (as you can see from the package name) but I ended up just implementing a new email plugin. 因此,我着手实现了Web Intent插件(从包名称中可以看到),但最终我只是实现了一个新的电子邮件插件。

Here is the Java class. 这是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"));
}
}

Remember to update plugins.xml 记住要更新plugins.xml

Here is the js plugin code 这是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]);
}

And finally, you call the plugin as such. 最后,您这样称呼插件。

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

Note I didn't include callbacks in the parameters, but you can see where they would go. 注意,我没有在参数中包含回调,但是您可以看到它们的去向。

Well, that looks like the code Boris Smus provides for using google maps. 好吧,看起来Boris Smus提供了使用Google地图的代码。 The Email example is this. 电子邮件示例是这个。

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

I'm having trouble with it myself as for me, it brings up the MMS composer instead of email... 就我而言,我自己遇到了麻烦,它调出了MMS作曲家而不是电子邮件...

*Edit: Thought I had something, but no... Other sources seem to suggest using a mailto link. *编辑:以为我有东西,但没有。其他消息似乎建议使用mailto链接。

*Edit 2: Ignore this and see the other answer. *编辑2:忽略此内容,然后查看其他答案。

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

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