Question

I am relatively new to Android programming, but have had experience in Java and other coding languages. As part of a program that I am currently making, I want to be able to send a pre-defined email when a button is pressed. I am currently looking at this code:

Sending Email in Android using JavaMail API without using the default/built-in app

I am currently able to start an intent to start the MailSenderActivity.class. However, I am not able to understand how that is able to send an email through the GmailSender.class. I believe that I am misunderstanding how to use the code provided. Am I supposed to create two separate intents that will start both activities up, one after each other, in the code on the home page, as below? If not, how would I do it?

public void SendEmail(View v) {
    Intent i = new Intent(getBaseContext(), MailSenderActivity.class);    
    Intent j = new Intent(getBaseContext(), GMailSender.class);       
    startActivity(i);
}

Also, I am wondering about the defined spaces for to/from, subject, body and the like in the code. I see that the MailSenderActivity.class has

                try {   
                GMailSender sender = new GMailSender("username@gmail.com", "password");
                sender.sendMail("This is Subject",   
                        "This is Body",   
                        "user@gmail.com",   
                        "user@yahoo.com");

Are the user@gmail.com and user@yahoo.com both the recipients of the email? And are there any other places in the code where I am supposed to define the contents of the email?

Thanks for your time.

Était-ce utile?

La solution

Scroll down and read the rest of the answer, you'll see that the sendMail() method gives all the clues:

public synchronized void sendMail(String subject, String body, String sender, String recipients) 

So: "user@gmail.com" is the sender (From field).

"user@yahoo.com" is the recipient (To field). You can specify more with commas, eg

"user@yahoo.com,user_2@gmail.com"

You would also see that GMailSender is just a class, not an Activity. Therefore, it does not need an Intent; just instantiate the class. Also, MailSenderActivity is a code sample demonstrating the implementation of GMailSender. You do not have to use it.

Eg

public void SendMail (View v) {
  try {   
    GMailSender sender = new GMailSender("your_username@gmail.com", "password");
    sender.sendMail("Subject",   
                    "Email body",   
                    "Fromfield@gmail.com",   
                    "toField@example.com");   
  } catch (Exception e) {   
    Log.e("SendMail", e.getMessage(), e);   
  } 
}

Also keep in mind Java naming conventions state that methods should start with a lowercase letter. You should adhere to those conventions and refactor your code appropriately.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top