I'm trying to make a button in Android Studio that takes all the information put into EditText
fields, then sends that as an e-mail.
I got the sending e-mail part working, just not the button to do what I want.
I made a SendEmail
class and added a constructor that takes in two String values, which I want to send as an e-mail. I don't know how to integrate this class with the button including the parameters.
This is what I have now:
Button sendEmail = findViewById(R.id.button);
sendEmail.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText test = (EditText) findViewById(R.id.editText);
String testText = test.getText().toString();
String textBody = "This is a test.";
}
});
I'm not sure where to go from here. Is there a better way to do what I want?
Edit: I want java to send the e-mail instead of opening up an email app and having the user send the e-mail manually. My e-mail class is as follows:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail {
public static void main(String [] args) {
// Recipient's email ID needs to be mentioned.
String to = "test@gmail.com";
// Sender's email ID needs to be mentioned
String from = "***@gmail.com";
// Get system properties
Properties properties = System.getProperties();
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);
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, "***");
}
});
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("Subject Line test");
message.setText("This is a test for body of the e-mail.");
Transport.send(message);
System.out.println("Send message successfully");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
This code lets me send an email without having to open up an email client. I'm trying to figure out how I can combine this code with my button. Also, this SendEmail code is the one I made for testing without the constructor that I mentioned previously.