Khushboo
Khushboo

Reputation: 107

How to create Forgot password in android?

I want an app in which i use Forgot Password. I have a TextView, named forgot password. When i click on it, It shows a PopUp having edittext in which i can write Email Adrress and when i click on OK button it sent new passward on my mail id. I use JSSEProvider class for protocols.

public final class JSSEProvider extends Provider {

public JSSEProvider() {
    super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
    AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
        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;
        }
    });
}
}

And

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

String subject = null;
String recipients = null;
String sender = null;
String body = null;

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.smtp.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 {   
    try{
    Log.e(""," recipients "+recipients);

    this.subject = subject;
    this.recipients = recipients;
    this.sender =sender;
    this.body = body;

    // Send this email in Async task, otherwise NetworkOnMainThread Exception will be thrown
    new MobiSnifferAsync().execute();

    }catch(Exception e){
       e.printStackTrace();
    }
}   

class MobiSnifferAsync extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        try{
            Log.e("AsyncTask"," subject "+subject); 
            Log.e("AsyncTask"," body "+body);
            Log.e("AsyncTask"," sender "+sender);
            Log.e("AsyncTask"," password "+password);
            Log.e("AsyncTask"," recipients "+recipients); 

            Properties m_properties     = new Properties();
            m_properties.put("mail.smtp.host", "smtp.gmail.com");
            m_properties.put("mail.smtp.socketFactory.port", "465");
            m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            m_properties.put("mail.smtp.auth", "true");
            m_properties.put("mail.smtp.port", "465");


            Session m_Session  =  Session.getInstance(m_properties, new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(sender, password); // username and the password
                }
            });


            InternetAddress m_fromAddress    =   new InternetAddress( sender);
            InternetAddress m_toAddress      =   new InternetAddress( recipients);

            MimeMessage m_simpleMessage  =   new MimeMessage(m_Session);
            m_simpleMessage.setFrom(m_fromAddress);
            m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
            m_simpleMessage.setSubject( subject);
            m_simpleMessage.setContent( body, "text/plain");

            Transport.send(m_simpleMessage);
            }catch(Exception e){
               e.printStackTrace();
            }
        return null;
    }
}


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");   
    }   
}   
}  

But don't know how to sent password on email id. Can anybody tell me, how can i do this.

Upvotes: 1

Views: 24656

Answers (3)

Hardik Jain
Hardik Jain

Reputation: 11

I would suggest you to make a simple button and make the background transparent using #00000000 and make text of button whichever color you want...boom!!! you have your Forgot Password.

Upvotes: 1

Amardeepvijay
Amardeepvijay

Reputation: 1628

  1. Pass Cus_email as parameter from android java file.
  2. make a webservice and host this on server

hope this sample code work for you...

        <?php
    if (isset($_POST['cus_email']))
    {
    $cus_email = $_POST['cus_email'];
    $ans = mysql_query("SELECT cus_pass from customer where cus_email='$cus_email'") or die(mysql_error());
    $to = $_POST['cus_email'];
    $cus_mail = '[email protected]';

    $cus_sub = 'Your Recovery password';
    $cus_msg = 'this is your password'.$ans;


    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: ".$cus_mail.""."\r\n";

    mail($to,$cus_sub,$cus_msg,$headers);
    if(mail)
    {
      echo "1";
    }
    else
    {
      echo "0";
    }

    }
    ?>

Upvotes: 0

Akshay
Akshay

Reputation: 6142

Try this very simple function..

    protected void sendEmail() {
      Log.i("Send email", "");

      String[] TO = {"[email protected]"};
      String[] CC = {"[email protected]"};
      Intent emailIntent = new Intent(Intent.ACTION_SEND);
      emailIntent.setData(Uri.parse("mailto:"));
      emailIntent.setType("text/plain");


      emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
      emailIntent.putExtra(Intent.EXTRA_CC, CC);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");

      try {
         startActivity(Intent.createChooser(emailIntent, "Send mail..."));
         finish();
         Log.i("Finished sending email...", "");
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, 
         "There is no email client installed.", Toast.LENGTH_SHORT).show();
      }
   }

Add your Password and other Text as Intent.EXTRA_TEXT's value..

Upvotes: 2

Related Questions