问题描述
在我的应用程序正在使用smtp.i要附加图像文件mail.how可以附上它发送邮件?我试着在mail.its赠送小图标它。但是没有得到的图像。
请help.thanks提前。
下面是邮寄code和邮件的形象它的外观 -
公共类MailImageFile扩展javax.mail.Authenticator {公共MailImageFile(){}
公共无效邮件(用户字符串,字符串传递){ 属性道具=新特性();
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,真);
props.put(mail.smtp.port,465); 会话的会话=作为Session.getDefaultInstance(道具,新javax.mail.Authenticator(){
受保护的PasswordAuthentication的getPasswordAuthentication(){
返回新的PasswordAuthentication(用户名,密码);
}
});
尝试{ 消息消息=新的MimeMessage(会话);
message.setFrom(新网际地址(用户名));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(TO));
message.setSubject(测试题目);
多部分多部分=新MimeMultipart的();
MimeBodyPart messageBodyPart =新MimeBodyPart();
数据源源=新FileDataSource(新文件(Environment.getExternalStorageDirectory()getAbsolutePath()+/ wallpaper.jpg));
messageBodyPart.setDataHandler(新的DataHandler(源));
messageBodyPart.setFileName(image.png);
messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
messageBodyPart.setHeader(内容ID,&所述;时尚>中);
multipart.addBodyPart(messageBodyPart); message.setContent(多部分); Transport.send(消息);
}赶上(MessagingException E){
抛出新的RuntimeException(E);
}
}
}
在第二个想法:
使用 javax.mail.util.ByteArrayDataSource:
多节多部分=新MimeMultipart的();
数据源源=新ByteArrayDataSource(镜像文件,图像/ BMP);//为消息造成的身体部位
MimeBodyPart messageBodyPart =新MimeBodyPart();
messageBodyPart.setContent(嗨......,text / html的;字符集= UTF-8);MimeBodyPart attachPart;
attachPart =新MimeBodyPart();
attachPart .setDataHandler(新的DataHandler(源));
attachPart .setFileName(文件名);multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachPart);message.setContent(多部分);
的构造预计为参数或者一个字节数组或一个InputStream。所以,如果你有在变量BMP位图,你会得到它的ByteArray是这样的:
ByteArrayOutputStream流=新ByteArrayOutputStream();
bmp.com preSS(Bitmap.Com pressFormat.PNG,100,流);
字节[]的字节数组= stream.toByteArray();
和会使用它在这样的ByteArrayDataSource:
数据源源=新ByteArrayDataSource(字节数组,图像/ PNG);
in my application i'm sending mail using smtp.i want to attach image file to mail.how can i attach it?i tried it.but not getting image in mail.its giving small icon.please help.thanks in advance.below is mailing code and image of mail how it looks-
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(USERNAME, PASSWORD);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(USERNAME));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(TO));
message.setSubject("Testing Subject");
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/wallpaper.jpg"));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("image.png");
messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
messageBodyPart.setHeader("Content-ID","<vogue>");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
On second thought:
Use javax.mail.util.ByteArrayDataSource:
Multipart multipart = new MimeMultipart();
DataSource source = new ByteArrayDataSource(imageFile, "image/bmp");
// creates body part for the message
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("Hi...", "text/html; charset=utf-8");
MimeBodyPart attachPart;
attachPart = new MimeBodyPart();
attachPart .setDataHandler(new DataHandler(source));
attachPart .setFileName(filename);
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachPart);
message.setContent(multipart);
ByteArrayDataSource's constructor expects as parameters either a byte array or an InputStream. So, if you have your Bitmap in the variable bmp you would get it to the byteArray like this:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
and would use it in the ByteArrayDataSource like this:
DataSource source = new ByteArrayDataSource(byteArray, "image/png");
这篇关于附加文件,并在Android的使用SMTP发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!