بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
آموزشی - ارسال ایمیل از طریق گوشی
خیلی از دوستان علاقه داشتند از طریق برنامه بتونند ایمیل ارسال کنند ، یکی از عزیزان از طریق وب سرور را نوشته بود ، ولی خیلی از برنامه ها به سرور وصل نیستند ؛ در این نوشته سورس کد ارسال ایمیل از طریق گوشی موبایل تقدیم می گردد :
ابتدا دسترسی های زیر را در مانیفست اضافه کنید :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
MainActivity.java :
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText editTextEmail, editTextSubject, editTextMessage;
Button btnSend, btnAttachment;
String email, subject, message, attachmentFile;
Uri URI = null;
private static final int PICK_FROM_GALLERY = 101;
int columnIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextEmail = (EditText) findViewById(R.id.editTextTo);
editTextSubject = (EditText) findViewById(R.id.editTextSubject);
editTextMessage = (EditText) findViewById(R.id.editTextMessage);
btnAttachment = (Button) findViewById(R.id.buttonAttachment);
btnSend = (Button) findViewById(R.id.buttonSend);
btnSend.setOnClickListener(this);
btnAttachment.setOnClickListener(this);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
/**
* Get Path
*/
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
attachmentFile = cursor.getString(columnIndex);
Log.e("Attachment Path:", attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
@Override
public void onClick(View v) {
if (v == btnAttachment) {
openGallery();
}
if (v == btnSend) {
try {
email = editTextEmail.getText().toString();
subject = editTextSubject.getText().toString();
message = editTextMessage.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[] { email });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject);
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
emailIntent
.putExtra(android.content.Intent.EXTRA_TEXT, message);
this.startActivity(Intent.createChooser(emailIntent,
"Sending email..."));
} catch (Throwable t) {
Toast.makeText(this,
"Request failed try again: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
}
public void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_GALLERY);
}
}
0
0
سلام.من از این کد استفاده کردم، پرمیشن های منیفست هم قرار دادم، کدهای لی اوت هم قرار دادم، توی محیط برنامه هیچ اروری ندارم ولی موقع اجرا کرش میکنم !
این هم لوگ کت : 04-17 19:04:27.199: E/AndroidRuntime(3297): java.lang.RuntimeException: Unable to start activity ComponentInfo{alfredo.uzumaki.app.Zekr_pro/alfredo.uzumaki.app.Zekr_pro.Activitycontactus}: java.lang.NullPointerException
(10 سال پیش)
برای این سوال 4 پاسخ وجود دارد.
پاسخ به سوال
MiRHaDi
12 سال پیش
+3
0
activity_main.xml :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:padding="5dp" >
<EditText
android:id="@+id/editTextTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:hint="Email Address!"
android:inputType="textEmailAddress"
android:singleLine="true" />
<EditText
android:id="@+id/editTextSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextTo"
android:layout_margin="5dp"
android:hint="Subject"
android:singleLine="true" />
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/editTextSubject"
android:layout_margin="5dp"
android:gravity="top|left"
android:hint="type message here!"
android:inputType="textMultiLine" />
<Button
android:id="@+id/buttonSend"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_below="@id/editTextMessage"
android:layout_margin="5dp"
android:text="Send" />
<Button
android:id="@+id/buttonAttachment"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="attachment" />
</RelativeLayout>
</ScrollView>
موفق باشید
پاسخ به سوال
Criss
12 سال پیش
+2
0
ممنون از این پست خوبتون <
چطور میشه با استفاده از smtp ایمیل ارسال کرد؟یعنی پسورد و یوزر جیمیل خودمون رو بهش بدیم<
برای مثال من با استفاده از Vb6 میتونم بصورت زیر ایمیل ارسال کنم < با استفاده از جیمیل خودم < همچنین میتونم فایل هم بهش اتچ کنم<
on Error Resume Next
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
Const cdoBasic = 1
Const cdoNTLM = 2
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "test-email"
objMessage.From = "criss@gmail.com"
objMessage.To = "criss-1@gmail.com,criss-2@yahoo.com"
objMessage.Bcc = "criss3@yahoo.com"
objMessage.TextBody = "salam-test-email"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "criss@gmail.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "123456"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.AddAttachment "D:\file.doc"
objMessage.Configuration.Fields.Update
objMessage.Send
پاسخ به سوال
MiRHaDi
12 سال پیش
+4
0
برای استفاده از smtp هم به لینکهای زیر رجوع فرمایید :
- http://mobiledevtuts.com/android/android-sdk-smtp-email-tutorial
- http://www.tiemenschut.com/how-to-send-e-mail-directly-from-android-application
- http://javapapers.com/android/android-email-app-with-gmail-smtp-using-javamail
- http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android
پاسخ به سوال
وحید
11 سال پیش
+2
0
دوستان من یه کدی دیدم برای ارسال ایمیل که خیلی کوتاهه.
چرا این همه تفصیل داده شده اینجا؟
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .