Need the coding to send mail

Tutorials with advanced 'difficulty' and more Lines of Code.

Need the coding to send mail

Postby rahul_kalidindi » Tue Apr 06, 2010 8:47 am

Hi guys,

I am designing an app that needs sending the mail to predefined mail id. It must take inputs from 5 text boxes, date picker, time picker and a spinner. Can anyone tell me how to design it. There's no need to get values from database. Getting values from the input as strings is enough. So guys please help me out...
Regards,
Rahul
rahul_kalidindi
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 06, 2010 8:41 am
Location: Visakhapatnam

Top

Postby MichaelEGR » Tue Apr 06, 2010 9:16 am

Consider creating an Activity defined by an XML layout with the fields you need.

When the user selects the send button pull String data from all of the fields and create the email message. You may also want to perform any validation before preparing to send the email as necessary.

You can send an email quite easily using an Intent:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);                     emailIntent.setType("plain/text");
  3.  
  4. emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { <one or more email recipients> });
  5.  
  6. emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, <your subject String>);
  7.  
  8. emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, <your email body/message as a String>);
  9.  
  10. <your-activity>.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  11.  
  12.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Replace the values between the <> with appropriate values. <your-activity> should be the current Activity that contains the form you create.

Best of luck...
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Postby rahul_kalidindi » Tue Apr 06, 2010 9:54 am

Can you please give an example app which gets the strings and sends email... Plzzzz....
Regards,
Rahul
rahul_kalidindi
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 06, 2010 8:41 am
Location: Visakhapatnam

Postby MichaelEGR » Tue Apr 06, 2010 10:04 am

I suggest you Google, "android example email"

A reasonable example:
http://snipplr.com/view/16644/sending-e ... ple-class/
Replace the "GMailSender" code with the email Intent from my 1st message.

If you need to understand how XML layouts work check the Android developer docs as there is plenty of info there.

I can't assist more than that without an hourly fee.. ;P You should have all you need now to get going.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Postby rahul_kalidindi » Tue Apr 06, 2010 10:40 am

I have tried to retrieve the strings and send the mail... this is what my code looks like...


[syntax="java"]public class appointments extends Activity {

List<Strings> appnt=new ArrayList<Strings>();
ArrayAdapter<Strings> adapter=null;
EditText name=null;
EditText phone=null;
Date date=null;
Spinner spinner=null;
EditText make=null;
EditText miles=null;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name=(EditText)findViewById(R.id.nametext);
phone=(EditText)findViewById(R.id.phonetext);
make=(EditText)findViewById(R.id.maketext);
miles=(EditText)findViewById(R.id.miles);



Button mail=(Button)findViewById(R.id.email);
mail.setOnClickListener(onMail);

Button reset=(Button)findViewById(R.id.reset);
reset.setOnClickListener(onReset);


Spinner hubSpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.services , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hubSpinner.setAdapter(adapter);

}
private View.OnClickListener onMail=new View.OnClickListener() {
public void onClick(View v)
{
Strings r=new Strings();
r.setName(name.getText().toString());
r.setPhone(phone.getText().toString());
r.setMake(make.getText().toString());
r.setMiles(miles.getText().toString());
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"helpline@toyota.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,r.getServices());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, r.getName());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, r.getMake());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, r.getMiles());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
};
private View.OnClickListener onReset=new View.OnClickListener() {
public void onClick(View v)
{

name.setText("");
phone.setText("");
make.setText("");
miles.setText("");


}
};
}



But after executing the project when i click send mail there is an error in the emulator that NO APPLICATIONS CAN PERFORM THIS ACTION...Can you tell me what the error is??? should i declare the onClick on the button to send mail in a different class???
Regards,
Rahul
rahul_kalidindi
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 06, 2010 8:41 am
Location: Visakhapatnam

Postby MichaelEGR » Tue Apr 06, 2010 1:08 pm

Ah... I'm afraid you may need a real device. The android.content.Intent.ACTION_SEND likely doesn't have an Intent listener setup on the emulator. On most devices the email application will accept this Intent, but the emulator doesn't have the bundled Google applications or a replacement email application.

I never use the emulator for reasons like this and because my focus is real time app & game dev, so working with the emulator is mostly useless for this purpose.

Perhaps search for "android emulator email intent" on Google and see what comes up. Quite likely the scenario occurs due to the description above, but I can't verify offhand.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Top

Postby rahul_kalidindi » Wed Apr 07, 2010 10:32 am

I am using Gmail sender function defined here to send a mail to the predefined mail id.

http://davanum.wordpress.com/2007/12/22 ... -via-smtp/


But my problem is that i want to embed more text into the body of my mail... The Gmail sender is only accepting one sting to be embedded into the body of the mail. Can anyone tell me how to embed more strings into the body of the mail... If Gmail sender is not efficient method to send a mail then please suggest me an alternative with an example....
Regards,
Rahul
rahul_kalidindi
Freshman
Freshman
 
Posts: 8
Joined: Tue Apr 06, 2010 8:41 am
Location: Visakhapatnam

Postby MichaelEGR » Wed Apr 07, 2010 4:33 pm

Use the Java StringBuilder class and the "append" method then "toString" or simply use "+" to concatenate one or more Strings into one String like this "

String message = string1 +string2 +string3 +"\n"
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests