在我的Android应用程序,我需要用的图像发送邮件的attachment.I我与发送mail.but如何与图像发送邮件作为附件的mail.Here我张贴的代码完成发送mail.Please帮我将图像发送,如下面的代码的附件。
这里是代码 -
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("abc@gmail.com", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@gmail.com"));
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("abc@gmail.com", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@gmail.com"));
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);
}
// 得到这个解决方案的形式在这里
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[]{"sendersemailaddreass@gmail.com"});
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");
让我知道,如果它为你工作。
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");