Javascript 如何将相机图像作为附件传递给邮件。(Android、iOS、Windows Phone、Phonegap)
我正在为我的工作(社会住房)开发一个应用程序,我希望它能够允许用户拍照并将其附加到电子邮件中,以便他们可以将其发送给我们(维修照片等) 我正在使用Phonegap和Eclipse,因为我希望该应用程序能够跨平台,但目前主要在Android上进行测试。有办法做到这一点吗?我目前正在使用下面的代码,但没有用Javascript 如何将相机图像作为附件传递给邮件。(Android、iOS、Windows Phone、Phonegap),javascript,android,ios,eclipse,cordova,Javascript,Android,Ios,Eclipse,Cordova,我正在为我的工作(社会住房)开发一个应用程序,我希望它能够允许用户拍照并将其附加到电子邮件中,以便他们可以将其发送给我们(维修照片等) 我正在使用Phonegap和Eclipse,因为我希望该应用程序能够跨平台,但目前主要在Android上进行测试。有办法做到这一点吗?我目前正在使用下面的代码,但没有用 <script typr="text/javascript" charset="utf-8"> function camera() {
<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>
功能摄像机()
{
navigator.camera.getPicture(onSuccess,onFail,{quality:20,
destinationType:Camera.destinationType.DATA\u URL
});
函数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=链接;
}
函数onFail(消息){
警报('失败原因:'+消息);
}
}
到目前为止,我已经尝试使用mailto:&attachment方法将数据传递到邮件应用程序,但从未附加图像(大多数邮件应用程序将此视为安全漏洞)。然后我尝试将图像的base64代码嵌入电子邮件正文中(如上所示)。不幸的是,base64只显示为纯文本,使邮件没有响应。我还尝试在Phonegap中使用image URI而不是Base64方法,但这会在我的logcat中抛出一个“image.URI未定义”错误
这可能吗?我知道我可以在这里的另一个问题中详细说明android的意图,但这在iOS上不起作用
任何帮助都将不胜感激
编辑2012年12月2日
我在这里尝试实现的功能与您在本机Android gallery/camera应用程序中获得的功能相同。拍完照片后,你就有了股票期权,其中之一就是邮寄。如果您选择通过邮件共享,则图像将作为附件传递到邮件应用程序。我有没有办法在我的应用程序中实现相同的功能 使用此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");
}
}
}
使用此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");
}
}
}
因此,对于这个问题似乎没有“一刀切”的解决方案 mailto:方法在大多数现代邮件应用程序中不传递附件,因为它被视为一种安全风险。因此,不管它是imageURI还是base64编码的图像,mailto:都无法工作。“主题”和“正文”的传递对于任何想使用上述代码预先填写没有附件的电子邮件的人来说都很有效 在其他地方提出这个问题后,我似乎需要使用phonegap插件(iOS的emailComposer和Android的WebIntent),以便将图像成功地从我的phonegap应用程序传递到邮件应用程序
谢谢。因此,对于这个问题似乎没有“一刀切”的解决方案 mailto:方法在大多数现代邮件应用程序中不传递附件,因为它被视为一种安全风险。因此,不管它是imageURI还是base64编码的图像,mailto:都无法工作。“主题”和“正文”的传递对于任何想使用上述代码预先填写没有附件的电子邮件的人来说都很有效 在其他地方提出这个问题后,我似乎需要使用phonegap插件(iOS的emailComposer和Android的WebIntent),以便将图像成功地从我的phonegap应用程序传递到邮件应用程序
谢谢。谢谢你。它看起来只是gmail,需要身份验证。是这样吗?理想情况下,我希望将图像传递到手机邮件应用程序,这样用户就不必每次从我的应用程序发送邮件时都登录。看起来我可能不得不在服务器端做一些事情:(不幸的是,我不能在我的Phonegap应用程序中使用本机Java,而不编写自己的插件(据我所知)这超出了我目前的能力。不过谢谢你的建议。谢谢你。它看起来只是gmail,需要身份验证。是这样吗?理想情况下,我希望将一个图像传递到手机邮件应用程序,这样用户就不必每次从我的应用程序发送邮件时都登录。看起来我可能需要做些什么德:(不幸的是,我不能在我的Phonegap应用程序中使用本机Java而不编写自己的插件(据我所知),这超出了我目前的能力。不过谢谢你的建议。