title: Sending mail in android without intents using SMTP
body: <p>Hi I am developing an android app which will send mail on click of a button. Code worked at first but due to some reason its not working now. Could anyone please help me with this?
  <strong>xyz@outlook.com is the recipient.
  abc@gmail.com is the sender.</strong> 
  I have hard coded the subject and the body of the mail.</p>
  
  <pre><code>package com.example.clc_construction;
  import java.io.File;
  import java.io.UnsupportedEncodingException;
  import java.util.Properties;
  import javax.activation.DataHandler;
  import javax.activation.DataSource;
  import javax.activation.FileDataSource;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.Multipart;
  import javax.mail.PasswordAuthentication;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.AddressException;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeBodyPart;
  import javax.mail.internet.MimeMessage;
  import javax.mail.internet.MimeMultipart;
  import android.app.Activity;
  import android.app.ProgressDialog;  
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Environment;
  
  
  public class Email extends Activity
  {
  public String jobNo;
  public String teamNo;
  private static final String username = "abc@gmail.com";
  private static final String password = "000000";
  private static final String emailid = "xyz@outlook.com";
  private static final String subject = "Photo";
  private static final String message = "Hello";
  private Multipart multipart = new MimeMultipart();
  private MimeBodyPart messageBodyPart = new MimeBodyPart();
  public File mediaFile;
  
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.camera_screen);
      Intent intent = getIntent();
      jobNo = intent.getStringExtra("Job_No");
      teamNo = intent.getStringExtra("Team_No"); 
      sendMail(emailid,subject,message);
  
  }
  private void sendMail(String email, String subject, String messageBody)
   {
          Session session = createSessionObject();
  
          try {
              Message message = createMessage(email, subject, messageBody, session);
              new SendMailTask().execute(message);
          }
          catch (AddressException e)
          {
              e.printStackTrace();
          }
          catch (MessagingException e)
          {
              e.printStackTrace();
          }
          catch (UnsupportedEncodingException e)
          {
              e.printStackTrace();
          }
      }
  
  
  private Session createSessionObject()
  {
      Properties properties = new Properties();
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.starttls.enable", "true");
      properties.put("mail.smtp.host", "smtp.gmail.com");
      properties.put("mail.smtp.port", "587");
  
      return Session.getInstance(properties, new javax.mail.Authenticator()
      {
          protected PasswordAuthentication getPasswordAuthentication()
          {
              return new PasswordAuthentication(username, password);
          }
      });
  }
  
  private Message createMessage(String email, String subject, String messageBody, Session session) throws 
  
  MessagingException, UnsupportedEncodingException
  {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("xzy@outlook.com", "Naveed Qureshi"));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
      message.setSubject(subject);
      message.setText(messageBody);
      return message;
  }
  
  
  
  public class SendMailTask extends AsyncTask&lt;Message, Void, Void&gt;
  {
      private ProgressDialog progressDialog;
  
      @Override
      protected void onPreExecute()
      {
          super.onPreExecute();
          progressDialog = ProgressDialog.show(Email.this, "Please wait", "Sending mail", true, false);
      }
  
      @Override
      protected void onPostExecute(Void aVoid)
      {
          super.onPostExecute(aVoid);
          progressDialog.dismiss();
      }
  
      protected Void doInBackground(javax.mail.Message... messages)
      {
          try
          {
              Transport.send(messages[0]);
          } catch (MessagingException e)
          {
              e.printStackTrace();
          }
          return null;
      }
  }
  }
  </code></pre>