-1

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.

Liko Salas
  • 21
  • 1
  • 5
  • Can you share SendEmail class – Shivam Kumar Sep 05 '18 at 06:20
  • https://www.tutorialspoint.com/java/java_object_classes.htm – misman Sep 05 '18 at 06:22
  • This question already has an answer here: [Link](https://stackoverflow.com/q/21720640/5995648) [duplicate] – Arbaz.in Sep 05 '18 at 07:18
  • Possible duplicate of [Sending Email from Android app when click on button](https://stackoverflow.com/questions/21720640/sending-email-from-android-app-when-click-on-button) – Tiago Mussi Sep 05 '18 at 07:30
  • I added the SendEmail class. The already answered question code opens up an email client where the user has to manually send the email. I want it so that when the user presses the button all the information in the EditText fields get automatically sent to a set e-mail address. – Liko Salas Sep 06 '18 at 00:54

1 Answers1

-1

Try this, hope it is working -

Button sendEmail = findViewById(R.id.button);
sendEmail.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        EditText test = (EditText) findViewById(R.id.editText);
        String subject = test.getText().toString();
        String textBody = "This is a test.";

        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                                   "mailto","email@email.com", null));
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, textBody);
        startActivity(Intent.createChooser(intent, "Choose an Email client :"));
    }
});
  • if use someone [answerd](https://stackoverflow.com/a/21721588/5995648) which already posted keep it in comment. no need to write as a answer. **Just give as a reference**. – Arbaz.in Sep 05 '18 at 07:19
  • @Arbaz.in , I saw this question and paste my code as answer which I used, not seen any already posted answer. But Noted for future... – Priyank Sharma Sep 05 '18 at 10:38
  • @Arbaz.in , thanks for your suggestion but , i am not able to write comment to the question. – Priyank Sharma Sep 05 '18 at 11:00