简体   繁体   中英

Android send E-Mail

I'm trying to send an E-Mail from my App for Registration. It contains a random number, which needs to be entered to activate the account. But I can't figure out how to do that in Java.

I tried this:

public void registerButtonMethod(View view){
        if(usernameBox.getText().length() > 2)
        {
             if(passwordBox.getText() == confirmPasswordBox.getText())
             {
                String adress = emailBox.getText().toString();
                int randomzahl = (int) ((Math.random()*pMaximum)+1);
                String host = "smtp.gmail.com";
                String from = adress;
                String pass = password;
                Properties props = System.getProperties();
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", host);
                props.put("mail.smtp.user", from);
                props.put("mail.smtp.password", pass);
                props.put("mail.smtp.port", "587");
                props.put("mail.smtp.auth", "true");

                String[] to = {emailBox.getText().toString()};
                Session session = Session.getDefaultInstance(props, null);
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                InternetAddress[] toAddress = new InternetAddress[to.length];
                message.setSubject("test");
                message.setText("test");
                Transport transport = session.getTransport("smtp");
                transport.connect(host, from, pass);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
            }
        }
}

But I don't think this will work :/ I'd be very happy about some help from you guys :D

Thank you!

change

if(passwordBox.getText() == confirmPasswordBox.getText())

to

if(passwordBox.getText().toString().equals(confirmPasswordBox.getText().toString()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM