title: sending image attachment as .jpg instead of .file using JavaMail
body: <p>In my android application I am sending a <code>.jpg</code> image as an attachment using <code>JavaMail</code>, it works fine but the problem is that the image is recieved as <code>.file</code> instead of <code>.jpg</code>, I haved tried to used <code>messageBodyPart.setHeader("Content-ID","&lt;image&gt;");</code> but still the image is recieved as <code>.file</code>. So please help, how can I make my code to send image as <code>.jpg</code> instead of <code>.file</code>.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>import android.content.Context;
  import android.os.AsyncTask;
  import java.io.File;
  import java.util.Properties;
  import android.os.Environment;
  import javax.activation.DataHandler;
  import javax.activation.DataSource;
  import javax.activation.FileDataSource;
  import javax.mail.Address;
  import javax.mail.Authenticator;
  import javax.mail.BodyPart;
  import javax.mail.Message;
  import javax.mail.PasswordAuthentication;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeBodyPart;
  import javax.mail.internet.MimeMessage;
  import javax.mail.internet.MimeMultipart;
  
  
  public class EmailHandler extends AsyncTask&lt;Void, Void, Boolean&gt; {
  
      private static String to = "receiver@gmail.com";
      private static String from = "senderp@gmail.com";
      private static String subject = "Subject";
      private static String sender = "Sender";
      private static String mail;
      private static String username = "sender";
      private static String password = "pasword";
  
      @Override
      protected Boolean doInBackground(Void... nothing) {
          try {
              Authenticator auth = new EmailAutherticator();
              Properties properties = new Properties();
              properties.setProperty("mail.smtp.auth", "true");
              properties.setProperty("mail.smtp.starttls.enable", "true");
              properties.setProperty("mail.smtp.host", "smtp.gmail.com");
              properties.setProperty("mail.smtp.port", "587");
              properties.setProperty("mail.smtp.user", username);
              properties.setProperty("mail.smtp.password", password);
              Session session = Session.getDefaultInstance(properties,auth);
  
              MimeMessage message = new MimeMessage(session);
              Address address = new InternetAddress(from,sender);
              message.setFrom(address);
              message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
              message.setSubject(subject);
              message.setText("JPG Image Test");
              MimeMultipart multipart = new MimeMultipart();
  
              MimeBodyPart messageBodyPart = new MimeBodyPart();
              String pic = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator+"picture0"+".jpg";
              DataSource source = new FileDataSource(pic);
              messageBodyPart.setDataHandler(new DataHandler(source0));
              messageBodyPart.setHeader("Content-ID","&lt;image&gt;");
              messageBodyPart.setFileName("Theft back");
              multipart.addBodyPart(messageBodyPart);
  
              message.setContent(multipart);
              Transport.send(message);
              return true;
          }
          catch(Exception exp) {
              exp.printStackTrace();
              return false;
          }
      }
  }
  
  class EmailAutherticator extends Authenticator {
  
      private String username = "sender";
      private String password = "password";
  
      public EmailAutherticator() {
          super();
      }
  
      public EmailAutherticator(String user,String pwd){
          super();
          username = user;
          password = pwd;
      }
  
      public PasswordAuthentication getPasswordAuthentication(){
          return new PasswordAuthentication(username,password);
      }
  
  }
  </code></pre>