2

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);
    }           
}   
 }
yuva ツ
  • 3,707
  • 9
  • 50
  • 78

2 Answers2

2

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");
DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • here what is the use of bundle,you are passing bitmap only.and there no any method setData() – yuva ツ Mar 21 '13 at 14:11
  • i know that i have to assign it the path value.i have not did just copy and past. – yuva ツ Mar 21 '13 at 16:07
  • firstly there is no such method setData(Bundle) – yuva ツ Mar 21 '13 at 16:24
  • its workinh partially, what is the imagefile you are passing in ByteArrayDataSource(),i wrot there path of my imagefile,when i'm executing code,in attachmnet of mail i'm getting image path and raw small icon instead of image – yuva ツ Mar 22 '13 at 05:30
1

you can attach file like this

specify the path in that n it will attach that file

Edited:

Multipart _multipart; = new MimeMultipart();

BodyPart messageBodyPart = new MimeBodyPart();

File sdCard =Environment.getExternalStorageDirectory();     

String path=sdCard.getAbsolutePath() + "/AttendanceSystem/MonthlyReport.xls";

messageBodyPart.setFileName(path);

_multipart.addBodyPart(messageBodyPart);

// Put parts in message 

msg.setContent(_multipart)

do it as per above way i just tested on my device its working n attaching chk itenter code here

DigCamara
  • 5,540
  • 4
  • 36
  • 47
Bhushan Shirsath
  • 258
  • 3
  • 12
  • i used your code.but not working. ' String path = sdCard.getAbsolutePath()+"xyz.JPEG"; message.setContent("Hi..."+"","text/html; charset=utf-8"); ' but its not working. – yuva ツ Mar 21 '13 at 15:36
  • hey j0k i just edited ans see it i tested it on my device its working m the same person to whom u were saying tht m promoting the site.. – Bhushan Shirsath Mar 21 '13 at 17:40
  • getting runtime exception.,IOExceptions -No content – yuva ツ Mar 22 '13 at 05:19
  • check if your code is having internet permission in the manifest file – Bhushan Shirsath Mar 22 '13 at 06:35
  • String path = sdCard.getAbsolutePath()+"/v.png"; attachPart .setFileName(path); try with this path – Bhushan Shirsath Mar 22 '13 at 06:37
  • still same result.actully setFilename is just a name. ' source = new ByteArrayDataSource(path, "image/bmp"); attachPart .setDataHandler(new DataHandler(source)); ' is doing main part – yuva ツ Mar 22 '13 at 06:46