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.
Scroll down and read the rest of the answer, you’ll see that the
sendMail()method gives all the clues:So:
"user@gmail.com"is the sender (From field)."user@yahoo.com"is the recipient (To field). You can specify more with commas, egYou would also see that
GMailSenderis just a class, not an Activity. Therefore, it does not need an Intent; just instantiate the class. Also,MailSenderActivityis a code sample demonstrating the implementation ofGMailSender. You do not have to use it.Eg
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.