title: java.mail does not attach files, Android. Error: IOException
body: <p>I can not attach a file to an attachment.</p>
  
  <pre><code>ERROR: IOException while sending message 
  </code></pre>
  
  <p>Without attachments sends excellent
  I check File.exists() is true</p>
  
  <p>I tried many methods. Not one does not work. The same result</p>
  
  <pre><code>...
  Properties props = new Properties();
  props.put("mail.transport.protocol", "smtp");
  props.put("mail.smtp.host", mailhost);
  props.put("mail.smtp.port", port);
  props.put("mail.smtp.ssl.enable", "true");
  props.put("mail.smtp.auth", "true");
  
  Session session = Session.getInstance(props);
  MimeMessage msg = new MimeMessage(session);
  Multipart multipart = new MimeMultipart();
  Transport transport = session.getTransport();
  
  msg.setSender(new InternetAddress(sender));
  msg.setSubject(subject);
  
  MimeBodyPart part = new MimeBodyPart();
  part.setText(body);
  
  
  String file = new String("/sdcard/DCIM/2.jpg");
  FileDataSource fileDataSource = new FileDataSource(file);
  MimeBodyPart attachmentPart = new MimeBodyPart();
  attachmentPart.setDataHandler(new DataHandler(fileDataSource));
  attachmentPart.setFileName(fileDataSource.getName());
  
  if (new File(file).exists()) Log.e("sendMail", "file exists");
  
  multipart.addBodyPart(part);
  multipart.addBodyPart(attachmentPart);
  msg.setContent(multipart);
  
  msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  
  transport.connect(mailhost, username, password);
  transport.sendMessage(msg, msg.getAllRecipients());
  </code></pre>
  
  <p><strong>solved</strong> the problem in android permissions and</p>
  
  <pre><code>attachmentPart.setHeader("Content-Type", "application/octet-stream");  
  </code></pre>
  
  <p><em>image/jpeg</em> smtp server complains of spam :)</p>