Untuk dapat mengirim email di android, perlu dilampirkan "mailto:" sebagai URL menggunakan method setData() dan type data "text/plain" dengan method setType() seperti sintaks berikut.
emailIntent.setData(Uri.parse("mailto:")); emailIntent.setType("text/plain");
Ok. Berikut tutorial cara mengirim pesan email di android menggunakan Eclipse
1. Buat project baru, seperti gambar
2. ketikan kode berikut pada directori "res/layout/activity_main.xml"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/kirimEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Menulis Email"/> </LinearLayout>
package com.ahmadsahidin.kirimemail; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startBtn = (Button) findViewById(R.id.kirimEmail); startBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { sendEmail(); } }); } protected void sendEmail() { Log.i("Send email", ""); String[] TO = {"amrood.admin@gmail.com"}; String[] CC = {"mcmohd@gmail.com"}; Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setData(Uri.parse("mailto:")); emailIntent.setType("text/plain"); emailIntent.putExtra(Intent.EXTRA_EMAIL, TO); emailIntent.putExtra(Intent.EXTRA_CC, CC); emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject"); emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here"); try { startActivity(Intent.createChooser(emailIntent, "Send mail...")); finish(); Log.i("Finished sending email...", ""); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }3. Jalankan aplikasinya. berikut tampilannya
Tentang Penulis :
0 komentar:
Post a Comment