1

我正在为我的工作(社会住房)开发一个应用程序,我希望它能够允许用户拍照并将其附加到电子邮件中,以便他们可以将其发送给我们(维修图片等)

我正在使用 Phonegap 和 Eclipse,因为我希望该应用程序是跨平台的,但目前主要在 Android 中进行测试。有没有办法做到这一点?我目前使用下面的代码无济于事。

      <script typr="text/javascript" charset="utf-8">

    function camera()
    {
        navigator.camera.getPicture(onSuccess, onFail, { quality: 20,
            destinationType: Camera.DestinationType.DATA_URL
         }); 

        function onSuccess(imageData) {
            var image = document.getElementById('image');
            var data = "data:image/jpeg;base64," + imageData;

            var link = "mailto:johnsmith@gmail.com?body="+data+"&subject=john smith";

            window.location.href = link;
        }

        function onFail(message) {
            alert('Failed because: ' + message);
        }

    }
    </script>

到目前为止,我已经尝试使用 mailto: &attachment 方法将数据传递到邮件应用程序,但从未附加图像(大多数邮件应用程序将此视为安全漏洞)。然后我尝试在邮件正文中嵌入图片的base64代码(如上图)。不幸的是,base64 只显示为纯文本,使邮件无响应。我还尝试在 Phonegap 中使用图像 URI 而不是 Base64 方法,但这会在我的 logcat 中引发“image.URI is not defined”错误。

这可能吗?我知道我可以将意图仅用于 android,如此处另一个问题中所述,但这不适用于 iOS 等。

任何帮助将不胜感激。

编辑 2012 年 2 月 12 日

我在这里想要实现的功能与您在原生 Android 画廊/相机应用程序中获得的功能相同。拍照后,您有共享选项,其中之一是邮件。如果您选择通过邮件共享,图像将作为附件传递到邮件应用程序。有什么方法可以在我的应用程序中实现相同的功能?

4

2 回答 2

1

所以看起来这个问题没有“一刀切”的解决方案。

mailto: 方法只是不会在大多数现代邮件应用程序中传递附件,因为它被视为安全风险。因此,无论它是 imageURI 还是 base64 编码的图像, mailto: 都不起作用。'subject' 和 'body' 的传递对于任何想要使用上述代码预先填写没有附件的电子邮件的人来说效果很好。

在其他地方提出这个问题之后,看起来我需要使用 phonegap 插件(iOS 的 emailComposer 和 Android 的 WebIntent)才能将图像成功地从我的 phonegap 应用程序传递到邮件应用程序。

谢谢。

于 2012-12-05T13:13:56.607 回答
0

使用此 JAVA 代码发送带有照片和文字的电子邮件。

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class GMailSender extends javax.mail.Authenticator {
    private String mailhost = "smtp.gmail.com";
    private String user;
    private String password;
    private Session session;

    static {
        Security.addProvider(new JSSEProvider());
    }

    public GMailSender(String user, String password) {
        this.user = user;
        this.password = password;

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
        try {
            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject);
            message.setDataHandler(handler);
            if (recipients.indexOf(',') > 0)
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
            else
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
            Transport.send(message);
        } catch (Exception e) {

        }
    }

    public synchronized void sendMail(String subject, String body, String senderEmail, String recipients, String filePath,String logFilePath) throws Exception {
        boolean fileExists = new File(filePath).exists();
        if (fileExists) {

            String from = senderEmail;
            String to = recipients;
            String fileAttachment = filePath;

            // Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);

            // create the message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();

            // fill message
            messageBodyPart.setText(body);

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(fileAttachment);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("screenShoot.jpg");
            multipart.addBodyPart(messageBodyPart);


            //part three for logs
            messageBodyPart = new MimeBodyPart();
            DataSource sourceb = new FileDataSource(logFilePath);
            messageBodyPart.setDataHandler(new DataHandler(sourceb));
            messageBodyPart.setFileName("logs.txt");
            multipart.addBodyPart(messageBodyPart);


            // Put parts in message
            message.setContent(multipart);

            // Send the message
            Transport.send(message);
        }else{
            sendMail( subject, body,  senderEmail,  recipients);
        }
    }

    public class ByteArrayDataSource implements DataSource {
        private byte[] data;
        private String type;

        public ByteArrayDataSource(byte[] data, String type) {
            super();
            this.data = data;
            this.type = type;
        }

        public ByteArrayDataSource(byte[] data) {
            super();
            this.data = data;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getContentType() {
            if (type == null)
                return "application/octet-stream";
            else
                return type;
        }

        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(data);
        }

        public String getName() {
            return "ByteArrayDataSource";
        }

        public OutputStream getOutputStream() throws IOException {
            throw new IOException("Not Supported");
        }
    }
}
于 2012-11-30T18:46:35.870 回答