PDA

View Full Version : ارسال ایمیل



bgalborz
شنبه 11 شهریور 1391, 18:36 عصر
سلام آقا من میخوام یه کدی بنویسم که ایمیلی رو به آدرسی خواص یعنی با to ثابت ارسال کنم . ولی هر کدی میزنم یا کار نمیکنه یا ارور زیر رو میده من فقط دارم با emulator کار میکنم :
اینم ارور : send email .....No aplications can perfom this action.
میخوام آدرس ایمیل فرستنده و موضوع و متن ایمیل رو از edittext بخونه و با کلیک روی دکمه ایمیل رو ارسال کنه .
ممنون میشم اگه کامل توضیح بدید .

bgalborz
یک شنبه 12 شهریور 1391, 00:16 صبح
سلام دمتون گرم ولی خودم گشتم پیدا کردم.


این کد اصلی برنامه:

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));


1. Android Layout
File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textViewPhoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editTextTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" >

<requestFocus />

</EditText>

<TextView
android:id="@+id/textViewSubject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editTextSubject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</EditText>

<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message : "
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="5" />

<Button
android:id="@+id/buttonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send" />

</LinearLayout>


___________________________________________


2. Activity
Full activity class to send an Email. Read the onClick() method, it should be self-explanatory.

package com.mkyong.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SendEmailActivity extends Activity {

Button buttonSend;
EditText textTo;
EditText textSubject;
EditText textMessage;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);

buttonSend.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
String message = textMessage.getText().toString();

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
//email.putExtra(Intent.EXTRA_CC, new String[]{ to});
//email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);

//need this to prompts email client only
email.setType("message/rfc822");

startActivity(Intent.createChooser(email, "Choose an Email client :"));

}
});
}
}

____________________________________

اینم نمونه کد DOWNLOAD (http://www.mkyong.com/wp-content/uploads/2012/03/Android-Send-Email-Example.zip)


نکته کلیدی اینه که اگه با emulator کار میکنید اگه error داد نترسید اول تو emulator برید ایمیل خودتون رو فعال کنید بعد بیاین تو برنامه به یه آدرس دیگه ایمیل بفرستید . اول save بعد send...