Androidでファイルを添付したメールを送信するにはどうすればよいですか?
この例は、Androidでファイルが添付されたメールを送信する方法を示しています。
ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。
ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="4dp" tools:context=".MainActivity"> <EditText android:id="@+id/etTo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:hint="Receiver's Email Address!" android:inputType="textEmailAddress" android:singleLine="true" /> <EditText android:id="@+id/etSubject" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:hint="Enter Subject" android:singleLine="true" /> <EditText android:id="@+id/etMessage" android:layout_width="match_parent" android:layout_height="200dp" android:layout_margin="5dp" android:gravity="top|start" android:hint="Compose Email" android:inputType="textMultiLine" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btSend" android:layout_width="80dp" android:layout_height="50dp" android:layout_margin="5dp" android:text="Send" /> <Button android:id="@+id/btAttachment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:text="attachment" /> </RelativeLayout> <TextView android:id="@+id/tvAttachment" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableStart="@drawable/ic_attach" android:visibility="gone" /> </LinearLayout>
ステップ3 −次のコードをsrc / MainActivity.java
に追加しますimport android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText etEmail;
EditText etSubject;
EditText etMessage;
Button Send;
Button attachment;
TextView tvAttachment;
String email;
String subject;
String message;
Uri URI = null;
private static final int PICK_FROM_GALLERY = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etEmail = findViewById(R.id.etTo);
etSubject = findViewById(R.id.etSubject);
etMessage = findViewById(R.id.etMessage);
attachment = findViewById(R.id.btAttachment);
tvAttachment = findViewById(R.id.tvAttachment);
Send = findViewById(R.id.btSend);
Send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
//attachment button listener
attachment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFolder();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
URI = data.getData();
tvAttachment.setText(URI.getLastPathSegment());
tvAttachment.setVisibility(View.VISIBLE);
}
}
public void sendEmail() {
try {
email = etEmail.getText().toString();
subject = etSubject.getText().toString();
message = etMessage.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 openFolder() {
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);
}
} ステップ4 −次のコードをandroidManifest.xmlに追加します
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <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" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[Runicon]をクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-
-
電子メールで実行可能ファイルを送信する方法
実行可能ファイルをメールに添付して送信するだけでは不十分であることに気付いたかもしれません。ファイル拡張子を.jpgに変更したり、ファイルをzipフォルダーに入れて送信したり、ファイルにランダムで存在しないファイル拡張子を追加したりするなどの古い戦術も機能しません。 何が機能しますか? ここであなたと共有しようとしているこの十分に活用されていない方法はうまくいくでしょう。要約すると、実行可能ファイルを画像ファイルに暗号化し、メールで送信して、復号化して抽出します レシーバーによって。 この方法はより意図的なものであるため、人を傷つけるために使用される可能性はほとんどありません。覚えておい
-
大きなビデオ ファイルを送信する方法 – メールで大きなファイルを共有する
大きなファイルを電子メールで送信するのは非常に困難です。これは、各電子メール クライアントにファイル共有のサイズ制限があるためです。 Gmail と Yahoo はファイル サイズを 25 MB に制限しますが、Outlook と iCloud はファイルを 20 MB に制限します。 そのため、たとえば 25 MB を超えるビデオ ファイルを送信しようとすると、メッセージが大きすぎるためにサーバーによって拒否されたというエラーが表示される場合があります。 一部の電子メール クライアントはエラーを表示しませんが、代わりに大きなビデオ ファイルの送信方法を提案します。 たとえば、Gmai