/// 安全性の低いアプリへのアクセスを許可しないと動作しない。送信元にログインできない。
https://www.simplifiedcoding.net/android-email-app-using-javamail-api-in-android-studio/
///
<uses-permission android:name="android.permission.INTERNET"/>
///
activation.jar
additionnal.jar
mail.jar
///
package jp.co.wata.myapplication;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//Declaring EditText
private EditText editTextEmail;
private EditText editTextSubject;
private EditText editTextMessage;
//Send button
private Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing the views
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextSubject = (EditText) findViewById(R.id.editTextSubject);
editTextMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend = (Button) findViewById(R.id.buttonSend);
//Adding click listener
buttonSend.setOnClickListener(this);
}
private void sendEmail() {
//Getting content for email
String email = editTextEmail.getText().toString().trim();
String subject = editTextSubject.getText().toString().trim();
String message = editTextMessage.getText().toString().trim();
//Creating SendMail object
SendMail sm = new SendMail(this, email, subject, message);
//Executing sendmail to send email
sm.execute();
}
@Override
public void onClick(View v) {
sendEmail();
}
}
class Config {
public static final String EMAIL ="watanabejyuku@gmail.com";
public static final String PASSWORD ="*******";
}
class SendMail extends AsyncTask<Void,Void,Void> {
//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
//Progressdialog to show while sending email
private ProgressDialog progressDialog;
//Class Constructor
public SendMail(Context context, String email, String subject, String message){
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Message Sent",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//Creating a new session
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
}
});
//session.setDebug( true ); // デバッグモードにする
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress(Config.EMAIL));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
///
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="Recipient Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextEmail" />
<TextView
android:text="Subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextSubject" />
<TextView
android:text="Message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:lines="4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextMessage" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonSend"
android:text="Send"/>
</LinearLayout>