Wednesday 30 May 2012

Android – Send Email Via GMail (Actually Via SMTP)

Create New Android Project and after that add below code into your MainActivity.java and activity_main.xml file.


MainActivity.java

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Button login = (Button) findViewById(R.id.mBtnSubmit);
  login.setOnClickListener(new View.OnClickListener() {

  public void onClick(View arg0) {
  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("dipakkeshariya@android.com", "dipakkeshariya");
  }
  });

  try {
  Message message = new MimeMessage(session);
  message.setFrom(new InternetAddress("dipak.keshariya@android.com"));
  message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("dipak.keshariya@mobileappdeveloper.com"));
message.setSubject("Testing Subject");
  message.setContent("Hi Dipak Keshariya (Android Developer)", "text/html; charset=utf-8");

  Transport.send(message);

  } catch (MessagingException e) {
  throw new RuntimeException(e);
  }
            }
  });
    }
}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/mBtnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:text="Send Mail" />

</RelativeLayout>


Download below 3 jar file and add into project's build path.

1) activation.jar
2) additionnal.jar
3) mail.jar

Give Following uses-permission into your Android Manifest file

1) <uses-permission android:name="android.permission.INTERNET"/>

Now Run Your Project.

Enjoy :-)

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.

Wednesday 23 May 2012

Simple Notification Example

Put Below Code in onClicklistener( ) event of Button

Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
public void run() {
// Notification Title and Message
Notification("This is message from Dipak Keshariya (Android Application Developer)", "This is Android Notification Message");
}
}, 0);


And write below Method in your MainActivity

private void Notification(String notificationTitle, String notificationMessage) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, "A New Message from Dipak Keshariya (Android Developer)!",
  System.currentTimeMillis());

Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(AndroidNotifications.this, notificationTitle, notificationMessage, pendingIntent);
  notificationManager.notify(10001, notification);
}

And Now Run Your Project.

Enjoy :-)

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.