0

我在项目中使用电子邮件附件插件。我面临的问题是我的文本文件未附加但电子邮件发送正确。我正在使用这个插件。我在这个路径上添加这个文件。 org.apache.cordova.plugin

在 config.xml 中添加这一行

这是我的java文件..

/**
 *
 * Phonegap Email composer plugin for Android with multiple attachments handling
 *
 * Version 1.0
 *
 * Guido Sabatini 2012
 *
 */

package org.apache.cordova.plugin;

import java.io.File;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.text.Html;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.LOG;

public class EmailComposer extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if ("showEmailComposer".equals(action)) {

            try {
                JSONObject parameters = args.getJSONObject(0);
                if (parameters != null) {
                    sendEmail(parameters);
                }
            } catch (Exception e) {

            }
            callbackContext.success();
            return true;
        }
        return false;  // Returning false results in a "MethodNotFound" error.
    }

    private void sendEmail(JSONObject parameters) {

        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

        //String callback = parameters.getString("callback");

        boolean isHTML = false;
        try {
            isHTML = parameters.getBoolean("bIsHTML");          
        } catch (Exception e) {
            LOG.e("EmailComposer", "Error handling isHTML param: " + e.toString());
        }

        if (isHTML) {
            emailIntent.setType("text/html");
        } else {
            emailIntent.setType("text/plain");
        }

        // setting subject
        try {
            String subject = parameters.getString("subject");
            if (subject != null && subject.length() > 0) {
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
            }
        } catch (Exception e) {
            LOG.e("EmailComposer", "Error handling subject param: " + e.toString());
        }

        // setting body
        try {
            String body = parameters.getString("body");
            if (body != null && body.length() > 0) {
                if (isHTML) {                                       
                    String bodyHtml = Html.fromHtml(body).toString();
                    LOG.e("EmailComposer", "Creating HTML email with body: " + bodyHtml);
                    ArrayList<String> extra_text = new ArrayList<String>();
                    extra_text.add(bodyHtml);
                    emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);
                } else {
                    LOG.e("EmailComposer", "Creating text email with body: " + body);
                    ArrayList<String> extra_text = new ArrayList<String>();
                    extra_text.add(body);
                    emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);
                }
            }
        } catch (Exception e) {
            LOG.e("EmailComposer", "Error handling body param: " + e.toString());
        }

        // setting TO recipients
        try {
            JSONArray toRecipients = parameters.getJSONArray("toRecipients");
            if (toRecipients != null && toRecipients.length() > 0) {
                String[] to = new String[toRecipients.length()];
                for (int i=0; i<toRecipients.length(); i++) {
                    to[i] = toRecipients.getString(i);
                }
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to);
            }
        } catch (Exception e) {
            LOG.e("EmailComposer", "Error handling toRecipients param: " + e.toString());
        }

        // setting CC recipients
        try {
            JSONArray ccRecipients = parameters.getJSONArray("ccRecipients");
            if (ccRecipients != null && ccRecipients.length() > 0) {
                String[] cc = new String[ccRecipients.length()];
                for (int i=0; i<ccRecipients.length(); i++) {
                    cc[i] = ccRecipients.getString(i);
                }
                emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc);
            }
        } catch (Exception e) {
            LOG.e("EmailComposer", "Error handling ccRecipients param: " + e.toString());
        }

        // setting BCC recipients
        try {
            JSONArray bccRecipients = parameters.getJSONArray("bccRecipients");
            if (bccRecipients != null && bccRecipients.length() > 0) {
                String[] bcc = new String[bccRecipients.length()];
                for (int i=0; i<bccRecipients.length(); i++) {
                    bcc[i] = bccRecipients.getString(i);
                }
                emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc);
            }
        } catch (Exception e) {
            LOG.e("EmailComposer", "Error handling bccRecipients param: " + e.toString());
        }

        // setting attachments
        try {
            JSONArray attachments = parameters.getJSONArray("attachments");
            if (attachments != null && attachments.length() > 0) {
                ArrayList<Uri> uris = new ArrayList<Uri>();
                //convert from paths to Android friendly Parcelable Uri's
                for (int i=0; i<attachments.length(); i++) {
                    try {
                        File file = new File(attachments.getString(i));
                        if (file.exists()) {
                            Uri uri = Uri.fromFile(file);
                            uris.add(uri);
                        }
                    } catch (Exception e) {
                        LOG.e("EmailComposer", "Error adding an attachment: " + e.toString());
                    }
                }
                if (uris.size() > 0) {
                    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                }
            }
        } catch (Exception e) {
            LOG.e("EmailComposer", "Error handling attachments param: " + e.toString());
        }

        this.cordova.startActivityForResult(this, emailIntent, 0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // TODO handle callback
        super.onActivityResult(requestCode, resultCode, intent);
        LOG.e("EmailComposer", "ResultCode: " + resultCode);
        // IT DOESN'T SEEM TO HANDLE RESULT CODES
    }

}

这是我的 js 文件。

/*
Email Plugin js file
*/
cordova.define("cordova/plugin/emailcomposer", function(require, exports, module) {
var exec = require('cordova/exec');

var EmailComposer = function() {
    this.resultCallback = null; // Function
};

EmailComposer.ComposeResultType = {
    Cancelled:0,
    Saved:1,
    Sent:2,
    Failed:3,
    NotSent:4
}

// showEmailComposer : all args optional
EmailComposer.prototype.showEmailComposer = function(successCallback,failureCallback,subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML,attachments) {
console.log("showEmailComposer()");
var args = {};
if(toRecipients)
    args.toRecipients = toRecipients;
if(ccRecipients)
    args.ccRecipients = ccRecipients;
if(bccRecipients)
    args.bccRecipients = bccRecipients;
if(subject)
    args.subject = subject;
if(body)
    args.body = body;
if(bIsHTML)
    args.bIsHTML = bIsHTML;
if(attachments)
    args.attachments = attachments;

cordova.exec(successCallback, failureCallback, "EmailComposer", "showEmailComposer", [args]);
}

var emailcomposer = new EmailComposer();
module.exports = emailcomposer;
});

我是这样用的。。

$(document).on('click', '.email_h', function() {
  var txtfilepath= window.localStorage.getItem("TEXTFILE_PATH"); 
  var PATH=txtfilepath+'/'+CASENAME+'/'+DOCUMENT_NAME+'.rtf';
  alert(PATH);

    var Email_PATH=txtfilepath+'/'+CASENAME+'/'+DOCUMENT_NAME+'.html';
     alert(Email_PATH);

     cordova.require('cordova/plugin/emailcomposer').showEmailComposer(
        function(result) {
          alert(result)
         console.log( 'successfully called email composer' ); },
        function() { console.log( 'failed to call email composer' ); },
        "Look at this photo",
        "Take a look at <b>this<b/>:",
        ["", ""],
        [],
        [],
        true,
        [PATH,Email_PATH]
      );
});
4

1 回答 1

0

你确定你给插件的附件数组不为空吗?或者只是您要发送的文件存在?

于 2013-07-25T12:51:02.927 回答