Monday, 16 June 2014

sending messages,call,and sending multiple emails in android(Teliphony Ex)

Step1:
activity_main.xml:

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

            android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="30dp"
        android:text="cno"
        android:textAppearance="?android:attr/textAppearanceMedium" />

            android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="41dp"
        android:text="msg"
        android:textAppearance="?android:attr/textAppearanceMedium" />

   



Step2:

Note:Add three classes and write code as below:
GMailSender.java

package com.example.telephonyex;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class GMailSender extends javax.mail.Authenticator {
    private String mailhost = "smtp.gmail.com";
    private String user;
    private String password;
    private Session session;

    static {
        Security.addProvider(new JSSEProvider());
    }

    public GMailSender(String user, String password) {
        this.user = user;
        this.password = password;

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
     
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
        message.setSender(new InternetAddress(sender));
        message.setSubject(subject);
        message.setDataHandler(handler);
        if (recipients.indexOf(',') > 0)
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
        else
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
        Transport.send(message);
     
    }

    public class ByteArrayDataSource implements DataSource {
        private byte[] data;
        private String type;

        public ByteArrayDataSource(byte[] data, String type) {
            super();
            this.data = data;
            this.type = type;
        }

        public ByteArrayDataSource(byte[] data) {
            super();
            this.data = data;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getContentType() {
            if (type == null)
                return "application/octet-stream";
            else
                return type;
        }

        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(data);
        }

        public String getName() {
            return "ByteArrayDataSource";
        }

        public OutputStream getOutputStream() throws IOException {
            throw new IOException("Not Supported");
        }
    }

}

JSSEProvider:
package com.example.telephonyex;
import java.security.AccessController;
import java.security.Provider;

public final class JSSEProvider extends Provider {

private static final long serialVersionUID = 1L;

public JSSEProvider() {
        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
        AccessController.doPrivileged(new java.security.PrivilegedAction() {
            public Void run() {
                put("SSLContext.TLS",
                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                put("KeyManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                put("TrustManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                return null;
            }
        });
    }

}

LongOperation.java

package com.example.telephonyex;

import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.util.Log;

public class LongOperation extends AsyncTask {




@Override
protected String doInBackground(Void... params)
{
try
{
// StringTokenizer tokenizer = new StringTokenizer(yourString, "!*^/");

 for(int i=0;i
 {
 GMailSender sender = new GMailSender("srikanthpatel132@gmail.com","08464316144");
        sender.sendMail(MainActivity.sub,MainActivity.msg,ApplicationConstants.list.get(i),ApplicationConstants.list.get(i));
}
 }
 catch(Exception e){Log.e("error",e.getMessage(),e);return "Email Not Sent";}
return "Email Sent";
}
@Override
protected void onPostExecute(String result)
{

}
@Override
protected void onPreExecute()
{
}

@Override
protected void onProgressUpdate(Void... values)
{
}

}



ApplicationConstants.java:

package com.example.telephonyex;

import java.util.ArrayList;

public class ApplicationConstants {
public static ArrayList list;

}

Step 4:
Note:write this code in MainActivity.java class
MainActivity.java:

package com.example.telephonyex;

import java.util.ArrayList;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

static String to;
static String msg;
static String sub;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
 

public void sms(View v){

EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);

SmsManager manager=SmsManager.getDefault();

manager.sendTextMessage(et1.getText().toString(), null, et2.getText().toString(),null, null);
 
}

public void call(View v){

EditText et1=(EditText)findViewById(R.id.editText1);

Intent i=new Intent();
i.setAction(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:"+et1.getText().toString()));
startActivity(i);





}



public void sendMail(View v){

EditText et3,et4,et5;


et3=(EditText)findViewById(R.id.editText3);
et4=(EditText)findViewById(R.id.editText4);
et5=(EditText)findViewById(R.id.editText5);



Intent i=new Intent();
i.setAction(Intent.ACTION_SEND);

i.putExtra(Intent.EXTRA_EMAIL,new String[]{et3.getText().toString(),});
i.putExtra(Intent.EXTRA_SUBJECT,new String[]{et4.getText().toString()});
i.putExtra(Intent.EXTRA_TEXT,new String[]{et5.getText().toString()});


i.setType("message/rfc822"); // this is used to enable MIME

startActivity(Intent.createChooser(i, "Select Any Email Client ..."));


// ArrayList list=new ArrayList();
//list.add(msg);
}


public void sendJavamail(View v){


EditText et3,et4,et5;


et3=(EditText)findViewById(R.id.editText3);
et4=(EditText)findViewById(R.id.editText4);
et5=(EditText)findViewById(R.id.editText5);

to=et3.getText().toString();
sub=et4.getText().toString();
msg=et5.getText().toString();

ArrayList list=new ArrayList();
   StringTokenizer tokenizer = new StringTokenizer(to, ";");
        while(tokenizer.hasMoreTokens())
        {
           list.add(tokenizer.nextToken());
        }
        ApplicationConstants.list=list;

   LongOperation operation=new LongOperation();
operation.execute();

}
}


Step5:
Note: write below permissions in AndroidManifest.xlm file
  
   
  
 


No comments:

Post a Comment