【发布时间】:2014-06-19 20:43:46
【问题描述】:
我想要一个使用忘记密码的应用程序。 我有一个 TextView,名为忘记密码。当我点击它时,它会显示一个包含编辑文本的弹出窗口,我可以在其中写电子邮件地址,当我点击确定按钮时,它会在我的邮件 ID 上发送新密码。 我使用 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 上发送密码。 谁能告诉我,我该怎么做。
【问题讨论】:
-
我不明白这怎么能保证安全,如果另一个人输入他的电子邮件地址并获得新密码怎么办?
标签: android email password-recovery forgot-password