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.

27 comments:

  1. From where we should download and add these three jar files
    1) activation.jar
    2) additionnal.jar
    3) mail.jar


    can u share these three jar files

    ReplyDelete
    Replies
    1. I did not received Email on Gmail Account, what can i do plz rply me urgently.

      Delete
    2. Hello Naveed,

      I updated my post, now check it.

      Delete
    3. still there is no link man..

      Delete
    4. Hello Serhat,

      You are asking about which type of link?

      Delete
  2. Its work for me thanks bro.
    But i have a question for u. I want to send mail using Edittext fields like your first post. when i message u. plz resolve problem in your first post.
    GMailSender.java
    SendMail.java
    JSSEProvider.java
    In these classes.

    ReplyDelete
    Replies
    1. Hello Naveed,

      Pass Edittext Value of UserName, FromId, ToId, Password instead of default value.

      Delete
  3. plz ans how to send my own server like my companies own xyz.xxx@companyanem.com

    ReplyDelete
  4. Hi how r you bro. Hope u will b fine...
    I have a question?
    I want to use this whole code in a single function in my forgot password activity, And i m using Async task with SOAP web service. when i call this function in do in background its does not send mail and give error that "class not found and further my project pakage name".
    What i ma doing in class?
    when user forgotpassword and enter email address in edittext field, email during in my app registration form in edittext field.
    when press enter user will received new password on account that he or she enter in edittext. like gmail, yahoo, hotmail accounts.
    Wating for your rply.................

    ReplyDelete
  5. Hi i get following error, send the correct code

    03-01 07:53:38.906: E/AndroidRuntime(1711): FATAL EXCEPTION: main
    03-01 07:53:38.906: E/AndroidRuntime(1711): android.os.NetworkOnMainThreadException
    03-01 07:53:38.906: E/AndroidRuntime(1711): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at java.net.InetAddress.getLocalHost(InetAddress.java:365)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.Transport.send(Transport.java:117)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at com.my.mail.MainActivity$1.onClick(MainActivity.java:49)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at android.view.View.performClick(View.java:4202)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at android.view.View$PerformClick.run(View.java:17340)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at android.os.Handler.handleCallback(Handler.java:725)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at android.os.Handler.dispatchMessage(Handler.java:92)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at android.os.Looper.loop(Looper.java:137)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at android.app.ActivityThread.main(ActivityThread.java:5039)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at java.lang.reflect.Method.invokeNative(Native Method)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at java.lang.reflect.Method.invoke(Method.java:511)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    03-01 07:53:38.906: E/AndroidRuntime(1711): at dalvik.system.NativeStart.main(Native Method)

    ReplyDelete
    Replies
    1. You have to do the networking stuff in a background thread. See AsyncTask. :)

      Delete
  6. thanks..its working...if i want to attache file how can i attach???

    ReplyDelete
  7. the app stoops unexpectedly in my phone and getting the following error please help.Thank in advance........(am a beginner in android)...


    04-01 12:32:41.822: E/AndroidRuntime(9616): FATAL EXCEPTION: main
    04-01 12:32:41.822: E/AndroidRuntime(9616): java.lang.NoClassDefFoundError: com.tpppp.tsend.MainActivity$1$1
    04-01 12:32:41.822: E/AndroidRuntime(9616): at com.tpppp.tsend.MainActivity$1.onClick(MainActivity.java:37)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at android.view.View.performClick(View.java:2532)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at android.view.View$PerformClick.run(View.java:9277)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at android.os.Handler.handleCallback(Handler.java:587)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at android.os.Handler.dispatchMessage(Handler.java:92)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at android.os.Looper.loop(Looper.java:143)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at android.app.ActivityThread.main(ActivityThread.java:4196)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at java.lang.reflect.Method.invokeNative(Native Method)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at java.lang.reflect.Method.invoke(Method.java:507)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    04-01 12:32:41.822: E/AndroidRuntime(9616): at dalvik.system.NativeStart.main(Native Method)

    ReplyDelete
  8. my application creshed when I click on Send Email Button.

    log error is- java.lang.NoClassDefFoundError: javax.activation.DataHandler.

    ReplyDelete
  9. nice, could you send me the link for the attached libraries?thanks!

    ReplyDelete
  10. while running the app it throws exception. log error is android.os.NetworkOnMainThreadException.

    ReplyDelete
  11. if someone has the same problem that I follow:

    problem with:

    Session session = Session.getDefaultInstance (props, new javax.mail.Authenticator () {
                         protected PasswordAuthentication getPasswordAuthentication () {
                             return new PasswordAuthentication ("dipakkeshariya@android.com", "dipakkeshariya");
                         }
                     });

    problem solved:

    Session session = Session.getDefaultInstance (props, new javax.mail.Authenticator () {
    protected javax.mail.PasswordAuthentication getPasswordAuthentication () {
    return new javax.mail.PasswordAuthentication ("dipakkeshariya@android.com", "dipakkeshariya);
    }
    });

    ReplyDelete
  12. GETTING THIS ERROR !!
    Don't know what to do !!
    If any one know how to crack this , please share your view !


    08-19 09:10:26.335: W/dalvikvm(1036): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    08-19 09:10:26.355: E/AndroidRuntime(1036): FATAL EXCEPTION: main
    08-19 09:10:26.355: E/AndroidRuntime(1036): java.lang.NoClassDefFoundError: com.android.app.frgoto.Forgotpassword$2
    08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.app.frgoto.Forgotpassword.sendEMail(Forgotpassword.java:128)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.app.frgoto.Forgotpassword.sendMail(Forgotpassword.java:71)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.app.frgoto.Forgotpassword$1.onClick(Forgotpassword.java:51)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at android.view.View.performClick(View.java:4084)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at android.view.View$PerformClick.run(View.java:16966)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at android.os.Handler.handleCallback(Handler.java:615)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at android.os.Handler.dispatchMessage(Handler.java:92)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at android.os.Looper.loop(Looper.java:137)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at android.app.ActivityThread.main(ActivityThread.java:4745)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at java.lang.reflect.Method.invokeNative(Native Method)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at java.lang.reflect.Method.invoke(Method.java:511)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    08-19 09:10:26.355: E/AndroidRuntime(1036): at dalvik.system.NativeStart.main(Native Method)
    08-19 09:10:28.245: I/Process(1036): Sending signal. PID: 1036 SIG: 9

    ReplyDelete
  13. And the problem arises when the pointer reaches to this section of the code (I tired to debug it)

    Session session = Session.getDefaultInstance (props, new javax.mail.Authenticator () {
    protected PasswordAuthentication getPasswordAuthentication () {
    return new PasswordAuthentication ("dipakkeshariya@android.com", "dipakkeshariya");
    }
    });

    ReplyDelete
  14. getting an exception at transport.send(message) .... how to resolve this problem

    ReplyDelete
  15. 01-16 10:05:24.462: E/AndroidRuntime(4926): FATAL EXCEPTION: main
    01-16 10:05:24.462: E/AndroidRuntime(4926): android.os.NetworkOnMainThreadException
    01-16 10:05:24.462: E/AndroidRuntime(4926): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at java.net.InetAddress.getLocalHost(InetAddress.java:365)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.Transport.send(Transport.java:117)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at com.example.emailsample.MainActivity$1.onClick(MainActivity.java:52)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at android.view.View.performClick(View.java:4091)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at android.view.View$PerformClick.run(View.java:17072)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at android.os.Handler.handleCallback(Handler.java:615)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at android.os.Handler.dispatchMessage(Handler.java:92)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at android.os.Looper.loop(Looper.java:153)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at android.app.ActivityThread.main(ActivityThread.java:5086)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at java.lang.reflect.Method.invokeNative(Native Method)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at java.lang.reflect.Method.invoke(Method.java:511)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
    01-16 10:05:24.462: E/AndroidRuntime(4926): at dalvik.system.NativeStart.main(Native Method)

    ReplyDelete
  16. It is giving me unfortunately stopped on clicking send

    ReplyDelete
  17. send mails good...
    what about receiving?

    ReplyDelete
  18. I want to add email address to reply to path via edit text

    ReplyDelete
  19. I don’t think many of websites provide this type of information.
    Gmail Technical Support

    ReplyDelete
  20. plzzz give me zip file of this code with jar files..
    n tell where to paste these files??

    ReplyDelete