如何在Android中以附件的形式发送带有图片的邮件?
问题描述:
在我的android应用程序中,我需要发送带有图像的邮件作为附件。我完成了发送mail.but完成如何发送邮件与图像作为邮件的附件。在这里我发布的代码为发送邮件。请帮助我发送图像作为附件在下面的代码。如何在Android中以附件的形式发送带有图片的邮件?
这里是代码 -
public class MailImageFile extends javax.mail.Authenticator {
public MailImageFile(){}
public void Mail(String user, String pass) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setContent("Hi...", "text/html; charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
答
public class MailImageFile extends javax.mail.Authenticator {
public MailImageFile(){}
public void Mail(String user, String pass) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setContent(_multipart);
message.setSubject("Testing Subject");
message.setContent("Hi...", "text/html; charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
// Got this solution form here
private Multipart _multipart;
_multipart = new MimeMultipart();
public void addAttachment(String filename,String subject) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(subject);
_multipart.addBodyPart(messageBodyPart2);
}
}
答
试试这个代码:
Intent myEmailIntent = new Intent(android.content.Intent.ACTION_SEND);
myEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Email Subject");
myEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{"[email protected]"});
myEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"your email message");
//message type
myEmailIntent.setType("text/plain");
File myAttachmentfile = getFileStreamPath("path of the file that you want to attach Eg: image");
myEmailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//attachment type
myEmailIntent.setType("image/jpeg");
myEmailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+myAttachmentfile.getAbsolutePath()));
startActivityForResult(Intent.createChooser(myEmailIntent, "SENDER NAME"),101);
//这是如何连接的图像或任何文件以电子邮件
emailIntent.putExtra(Intent.EXTRA_STREAM, "your file path");
让我知道它是否适合你。
+0
我需要使用SMTP不是这种方式来发送邮件.. – Piya 2013-03-22 06:15:36
答
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
emailIntent.setType("image/png");
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file1));
uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file2));
emailIntent.putExtra(Intent.EXTRA_STREAM, uris));
startActivity(emailIntent);
答
这必将有助于您附上您的图像,而无需使用意图。
Multipart multipart = new MimeMultipart();
MimeBodyPart attachPart = new MimeBodyPart();
String attachFile = "/storage/emulated/0/pic.jpg";//picture location
DataSource source = new FileDataSource(attachFile);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(attachFile).getName());
//Trick is to add the content-id header here
attachPart.setHeader("Content-ID", "image_id");
multipart.addBodyPart(attachPart);
//third part for displaying image in the email body
attachPart = new MimeBodyPart();
attachPart.setContent("<h1>Attached Image</h1>" +
"<img src='cid:image_id'>", "text/html");
multipart.addBodyPart(attachPart);
//Set the multipart message to the email message
message.setContent(multipart);
Transport.send(message);
System.out.println("Done");
其图像的邮件连接..但,当我看到邮件的邮件我无法查看图像... – Piya 2013-03-22 07:02:30
你能下载图像吗? – 2013-03-22 07:28:00
是啊...现在它的工作..添加到代码messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT); – Piya 2013-03-22 07:38:23