如何在android中创建忘记密码?

时间:2014-05-03 04:55:28

标签: android email password-recovery forgot-password

我想要一个使用忘记密码的应用。 我有一个名为忘记密码的TextView。当我点击它时,它会显示一个包含edittext的PopUp,我可以在其中编写Email Adrress,当我点击OK按钮时,它会在我的邮件ID上发送新的passward。 我使用JSSEProvider类作为协议。

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

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

但不知道如何在电子邮件ID上发送密码。 谁能告诉我,我该怎么做呢。

3 个答案:

答案 0 :(得分:2)

尝试这个非常简单的功能..

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

      String[] TO = {"amrood.admin@gmail.com"};
      String[] CC = {"mcmohd@gmail.com"};
      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();
      }
   }

将您的密码和其他文字添加为Intent.EXTRA_TEXT的值..

答案 1 :(得分:1)

我建议您制作一个简单的按钮,并使用#00000000使背景透明,并使按钮的文本随心所欲...繁荣起来!!!您有忘记密码。

答案 2 :(得分:0)

  1. 将Cus_email作为android java文件中的参数传递。
  2. 制作网络服务并在服务器上托管此服务
  3. 希望此示例代码适合您...

            <?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 = 'support@xyz.in';
    
        $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";
        }
    
        }
        ?>