1

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.

Community
  • 1
  • 1
The Obscure Question
  • 1,034
  • 11
  • 25

1 Answers1

1

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.

A--C
  • 35,565
  • 10
  • 102
  • 90
  • Alright, thanks for your answer! So are you saying that I do not need to create a new activity to use that code? – The Obscure Question Jan 13 '13 at 19:47
  • @AndrewChen Np, welcome to StackOverflow! Please make sure you have read the FAQ, and that you know how accepting and answer works. And yes, you don't. I'll edit my answer. – A--C Jan 13 '13 at 19:49
  • Now I read your updated edit to your answer, and am now slightly confused. Unless I am mistaken, I don't believe that there is any place to define the subject and body of the email. If I were to not use MailSenderActivity, and just use GmailSender, would I not be able to define a custom subject nor body? – The Obscure Question Jan 13 '13 at 19:53
  • @AndrewChen this is like any other programming language. Make body and subject variables, get them as user input, then when they click your button pass those variables off instead. Android *uses* Java. – A--C Jan 13 '13 at 19:55
  • Alright, I believe that I understand how to define those variables. However, I have been playing around with the code, and this is where my inexperience with Android programming comes in. If I were to just instantiate the class, how would I run the code without an intent (unless that's not the purpose of the intent). I guess what I'm trying to ask is, how would I even use the code provided? Can you give me a quick run-through of what I'm supposed to do? – The Obscure Question Jan 13 '13 at 20:00
  • Intents are used to start Activities, Services, BroadcastReceivers, etc. For a like `GMailSender`, it's not needed to use an Intent - it's just another object. Since `SendMail (View v)` accepts a View, I'm assuming you made a button in your MainActivity's layout xml and set `android:onClick = "SendMail"` – A--C Jan 13 '13 at 20:03
  • That's correct. I made a button with android:onClick="SendMail" – The Obscure Question Jan 13 '13 at 20:04
  • When you click that button, `SendMail` is invoked. Provided that your username and password fields are proper, an email should be sent. Of course, if you want to define the body and subject you have to add EditTexts, etc. to get the input from the user. Also keep in mind that I haven't tested this class, so make sure to follow **all** the steps the answer you linked to says. – A--C Jan 13 '13 at 20:08
  • Yes, I do realize that. However, my thought on the matter was that SendMail would be used in the intent that would initiate the MailSenderActivity. If I read your previous comments correctly, then this would not be the case. However, the code in MailSenderActivity states in a comment that "/** Called when the activity is first created. */" Wouldn't this mean that MailSenderActivity is an activity that would be started from an intent? – The Obscure Question Jan 13 '13 at 20:13
  • **1)** MailSenderActivity is an Activity like any other so that needs to be started with an Intent. **2)** The comment refers to `onCreate()` being called first **3)** You can see the `onClickListener()` defined in `onCreate()`. This means that your code is basically the same as theirs - mail is *only* sent on a button click. – A--C Jan 13 '13 at 20:17
  • Okay. So I would use an intent to start MailSenderActivity, and instantiate GMailSender? – The Obscure Question Jan 13 '13 at 20:20
  • @AndrewChen Exactly. The since `MailSenderActivity` is an Activity, you need to make an Intent and call `startActivity()`. `GMailSender` is just a plain old class, so you only need to instantiate it. – A--C Jan 13 '13 at 20:22
  • Great, thanks! I have made an intent in my main activity to start up MailSenderActivity. However, I get the LogCat error: " java.lang.IllegalStateException: Could not execute method of the activity" – The Obscure Question Jan 13 '13 at 20:29
  • @AndrewChen This question is definitely getting too long. I'd say double check that you have the proper layout that the Activity needs (A button with id `send`). And if that doesn't work, you should probably ask another question. You'll get more people helping. Make sure to attach your full stack trace, layout xmls and source code if you do make another question, you'll get help quicker that way. – A--C Jan 13 '13 at 20:32
  • Alright. I really appreciate your help. I'll troubleshoot a bit more, and ask another question if necessary. Thanks! – The Obscure Question Jan 13 '13 at 20:49