简体   繁体   English

如何将相机图像作为附件传递给邮件。 (Android,iOS,Windows Phone,Phonegap)

[英]How to pass a camera image to mail as an attachment. (Android, iOS, Windows Phone, Phonegap)

I am developing an app for my work (social housing) and I want it to be able to allow the user to take a photo and attach it to an email so they can send it to us (pictures of repairs etc) 我正在为自己的工作开发一个应用程序(社会住房),我希望它能够允许用户拍摄照片并将其附加到电子邮件中,以便他们可以将其发送给我们(维修图片等)

I'm using Phonegap and Eclipse as I want the app to be cross platform but am testing in Android primarily at the moment. 我正在使用Phonegap和Eclipse,因为我希望该应用程序可以跨平台运行,但目前主要在Android中进行测试。 Is there a way to do this? 有没有办法做到这一点? I am currently using the code below with no avail. 我目前正在使用下面的代码,但无济于事。

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

So far I've tried passing the data through to the mail app using the mailto: &attachment method but that never attaches an image (most mail apps treat this as a security hole). 到目前为止,我已经尝试使用mailto:&attachment方法将数据传递到邮件应用程序,但是它从未附加图像(大多数邮件应用程序将此视为安全漏洞)。 Then I tried to embed the base64 code of the image in the body of the email (as shown above). 然后,我尝试将图像的base64代码嵌入到电子邮件正文中(如上所示)。 Unfortunately the base64 just displays as plain text and makes mail unresponsive. 不幸的是,base64仅显示为纯文本,并使邮件无响应。 I've also tried using the image URI instead of the Base64 method in Phonegap but that throws an 'image.URI is not defined' error in my logcat. 我也曾尝试在Phonegap中使用图像URI而不是Base64方法,但是这在我的logcat中引发了“未定义image.URI”错误。

Is this possible? 这可能吗? I know I can use intents for just android as detailed in another question here but this won't work on iOS etc. 我知道我可以在Android上使用Intent,如此处另一个问题所述,但这在iOS等系统上不起作用。

Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT 02/12/2012 编辑02/12/2012

What I'm trying to achieve here is the same functionality you get in the native Android gallery/camera app. 我要在这里实现的功能与您在本机Android gallery / camera app中获得的功能相同。 After you take a picture you have share options, one of which is mail. 拍照后,您可以使用共享选项,其中之一就是邮件。 If you chose to share via mail the image is passed to the mail app as an attachment. 如果您选择通过邮件共享,则图像将作为附件传递到邮件应用程序。 Is there any way I can implement this same functionality in my app? 有什么办法可以在我的应用程序中实现相同的功能?

So it looks like there is no 'one size fits all' solution for this problem. 因此,似乎没有“一刀切”的解决方案。

The mailto: method just doesn't pass attachments in most modern mail apps as it's seen as a security risk. mailto:方法只是在大多数现代邮件应用程序中不传递附件,因为它被视为存在安全风险。 So regardless of whether its an imageURI or a base64 encoded image, mailto: just won't work. 因此,无论是imageURI还是base64编码图像,mailto:都将无法工作。 The passing of 'subject' and 'body' works well though for anyone that wants to use the above code for pre-filling in an email with no attachments. 不过,对于希望使用以上代码预先填写无附件的电子邮件的人来说,传递“主题”和“正文”效果很好。

After posing this question elsewhere it looks like I'll need to use a phonegap plugin (emailComposer for iOS and WebIntent for Android) in order to pass an image sucessfully to a mail app from my phonegap app. 在其他地方提出此问题后,我似乎需要使用phonegap插件(适用于iOS的emailComposer和适用于Android的WebIntent)才能将图像成功地从我的phonegap应用传递到邮件应用。

Thanks. 谢谢。

use this JAVA code to send Email with photo and text. 使用此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");
        }
    }
}

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

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