在我的应用中,有一个区域需要用户输入密码。
在用户第一次尝试进入此区域时,他需要确认一些详细信息以获取密码,以便将其插入此区域。 我想在找到所需细节之后找到好的方法来识别密码。 我有超过5000个用户,所以我不想通过邮件将密码发送给每个用户。
此外,我希望密码特定于每个用户,因此向每个用户发送自动邮件不是一个好的解决方案。 我想过GCM(谷歌云按摩),但我知道这不是解决我问题的正确方法。
所以我的问题是,对每个用户自动分配密码和为每个用户分配不同密码的正确方法是什么?
答案 0 :(得分:0)
final TextView passwordText = (TextView) findViewById(R.id.passwordText); //The password
final EditText digitsField = (EditText) findViewById(R.id.digitsField); //Length of password
Button generatePassword = (Button) findViewById(R.id.generatePassword); //Generate Password
generatePassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int passwordLength = Integer.parseInt(digitsField.getText().toString());
String allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char[] allowedCharsArray = allowedChars.toCharArray();
char[] chars = new char[passwordLength];
Random random = new Random();
for (int i = 0; i < passwordLength; i++) {
chars[i] = allowedCharsArray[random.nextInt(allowedChars.length())];
}
passwordText.setText(chars, 0, passwordLength);
}
});
首先,在项目中添加这3个库:https://code.google.com/p/javamail-android/downloads/list
将此代码添加到您的活动中:
private void sendMail(String email, String subject, String messageBody) {
Session session = createSessionObject();
try {
Message message = createMessage(email, subject, messageBody, session);
new SendMailTask().execute(message);
} catch (MessagingException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("Your GMail", "Username of your GMail"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
message.setSubject(subject);
message.setText(messageBody);
return message;
}
private Session createSessionObject() {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
return Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("Your GMail", "Your password of GMail");
}
});
}
private class SendMailTask extends AsyncTask<Message, Void, Void> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(YourActivity.this, "Please wait", "Sending email...", true, false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
Toast.makeText(getBaseContext(), "Password Key has been sent. Please check your inbox.", Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Message... messages) {
try {
Transport.send(messages[0]);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
然后要发送自动电子邮件,请按一下按钮或任何您想要的电子邮件:
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
String email = user_email; // The email you will send to.
String subject = "Password Key";
String message = "Your password key is: " + password_key; //password_key is the password you will generate.
sendMail(email, subject, message);
} else {
Toast.makeText(getBaseContext(), "Network Error. Please connect to the internet.", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
经过一番咨询后,我发现了满足我需求的最佳方式。我使用了Google Apps脚本。这是Google为每个用户提供的系统,可以智能地控制他的Google产品,如Gmail,Google文档等。您只需访问script.google.com即可看到脚本的开头。在此页面中,您可以使用JavaScript编写脚本来控制产品。所以我刚刚编写了一个脚本来检查Gmail中的邮件,如果这是一封包含密码请求的电子邮件,并且所有必需的详细信息都存在,则脚本会生成密码并自动回复邮件。 以下是代码的一部分:
function
sendAutoPswrdEmails()
{
var label = GmailApp.getUserLabelByName('passwords');
var cmds;
var stop = 0;
cmds = label.getThreads(0, 10); //get first 10 mails
for( var j = 10; cmds.length > 0; j+=10 )
{
for( var i = 0; i < cmds.length; ++i )
{
var email = cmds[i].getMessages()[0]; //email is a gmailMessage object
if(email.isUnread())
{
var mailSubject = email.getSubject();
var mailBody = email.getBody();
//you may need to do extra parsing here, depending on your usage
if(mailSubject == "whatever you want")
{
var mainTextExist = mailBody.indexOf("whatever you need");
if(mainTextExist != -1)
{
var pswrd = someAlgorithm;
email.reply("whatever you want to answer" + pswrd);
cmds[i].markRead();
}
}
}
}
cmds = [];
cmds = label.getThreads(j, 10);//get next 10 mails
}
}
对我来说是完美的解决方案。