Friday 31 May 2013

Sendig Email from our custome application with out using native application in android tutorial

Dear friends this is the tutorial for building Email sending application. It will allow sending Emails from our application
1. First create your new project 
2. And download all jar files from this link       
3. Paste this jar files to your project and right click all jar files go to build path and click add to build path
4. Then copy this code to your project this is my layout file

<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>



5. Now copy the Source code to your Activity class

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

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() {

            @Override
            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() {
                            @Override
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(
                                        "sender_email@example.com", "password");
                            }
                        });

                try {
                    MimeMessage message = new MimeMessage(session);
                    message.setFrom(new InternetAddress(
                            "
sender_email@example.com"));
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse("receiver_email@gmail.com"));
                    message.setSubject("Testing Subject");
                    message.setContent(
                            "Your text");

                    Transport.send(message);

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


6. Include all permissions in your manifest file

  <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>



Now you can Run your project 
By clicking send button the content that you have written in the code will send to the specified gmail address

Thank you..............:-)

Please write reviews below if any problem occurs please write below or contact me ribindasmayilpeeli@gmail.com

Thank you once again.......:-)

No comments:

Post a Comment