60 likes | 197 Vues
This guide details two main methods for sending emails in Android applications: using email clients like Gmail and configuring SMTP/IMAP services. The guide emphasizes the importance of having the user’s email account set up for email clients, while SMTP/IMAP requires proper configuration. Additionally, a sample implementation is provided using the javax.mail package to send emails via a Gmail client. A user-friendly layout for sending emails is included, along with a practical exercise for creating a registration activity that confirms the user’s contact information via email.
E N D
Android and Emails Ken Nguyen Clayton state University 2012
Sending Emails • There are two main methods to send an email • Via email clients such as gmail or similar clients • Via SMTP/IMAP or similar services – auto emailing • Using email client required the user have their email account already setup • Using the SMTP/IMAP or other service required the service properly configured – look at javax.mail package
Sending emails via gmail client // EmailMainActivity.java public class EmailMainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_email_main); Button send=(Button) findViewById(R.id.buttonSend); //send an email when the button is click send.setOnClickListener(new OnClickListener() { public void onClick(View v) { final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ “email@address.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Your email subject"); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Your email message"); EmailMainActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail... – notification while sending")); } }); } }
Layout.xml <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/buttonSend" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="send email" /> </LinearLayout>
Exercise • Create a registration activity where the user can enter their contact information and an email will be sent to them as a confirmation