title: Send an Email when the network is available
body: <p>I am developing an android application for scheduling an email to be sent at specified time.<br>
  Now I succeeded by setting a broadcast receiver and using a pending intent.<br>
  Now the problem is: suppose that at the scheduled network or internet connection is not available, how can I achieve that action?<br>
  I can register a broadcast receiver for the internet connection but i don't know how to use it then.<br>
  Help me out. </p>
  
  <p>whenever the user sets time I am calling this setAlarm method()</p>
  
  <pre><code>   private void setAlarm(Calendar targetCal) {
  
           email = editTextEmail.getText().toString();
           subject = editTextSubject.getText().toString();
           message = editTextMessage.getText().toString();
          //
          Toast.makeText(AlarmActivity.this, "Mail Scheduled at " + targetCal.getTime(),
                  Toast.LENGTH_LONG).show();
          Intent intent = new Intent(getBaseContext(), AlarmReciever.class);
          int uniqueValue = (int) System.currentTimeMillis();
          intent.putExtra("email", email);
          intent.putExtra("sub", subject);
          intent.putExtra("msg", message);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(
                  getApplicationContext(), uniqueValue, intent, 0);
  
          AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
          alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
                  pendingIntent);
          AlarmActivity.this.finish();
  
      }
  </code></pre>
  
  <p>At onReceive method i will call send email method()</p>
  
  <pre><code>  public static void sendMail(Context c,String em, String sub,String msg){
      try {
  
          sender.sendMail(sub, msg, un, em);
          Toast.makeText(c, "Mail Being Sent",Toast.LENGTH_LONG).show();
         no.flags=Notification.FLAG_AUTO_CANCEL; 
          nf.notify(Notify,no);
  
    } catch (Throwable t) {
          Toast.makeText(c,
                        "There are problem with sending mail. ",
                        Toast.LENGTH_LONG).show();
          mgr.notify(fail, note);
  
    }
  }
  </code></pre>