Android smtp mail project sample

Here is a sample Android project that uses SMTP to send an email:

Project Structure:

EmailSenderActivity.java:

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

public class EmailSenderActivity extends Activity {
    private EditText etTo;
    private EditText etSubject;
    private EditText etMessage;
    private Button btnSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email_sender);

        etTo = findViewById(R.id.et_to);
        etSubject = findViewById(R.id.et_subject);
        etMessage = findViewById(R.id.et_message);
        btnSend = findViewById(R.id.btn_send);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String to = etTo.getText().toString();
                String subject = etSubject.getText().toString();
                String message = etMessage.getText().toString();

                Intent intent = new Intent(EmailSenderActivity.this, EmailSenderService.class);
                intent.putExtra("to", to);
                intent.putExtra("subject", subject);
                intent.putExtra("message", message);
                startService(intent);
            }
        });
    }
}

EmailSenderService.java:

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import javax.mail.Authenticator;
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 EmailSenderService extends Service {
    private static final String TAG = "EmailSenderService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStartCommand(Intent intent, int flags, int startId) {
        String to = intent.getStringExtra("to");
        String subject = intent.getStringExtra("subject");
        String message = intent.getStringExtra("message");

        sendEmail(to, subject, message);

        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }

    private void sendEmail(String to, String subject, String message) {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", "your-smtp-host");
            props.put("mail.smtp.port", "your-smtp-port");
            props.put("mail.smtp.auth", "true");

            Session session = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("your-email", "your-password");
                }
            });

            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("your-email"));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            msg.setSubject(subject);
            msg.setText(message);

            Transport.send(msg);
            Log.d(TAG, "Email sent successfully!");
        } catch (MessagingException e) {
            Log.e(TAG, "Error sending email: " + e.getMessage());
        }
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.emailsender">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".EmailSenderActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".EmailSenderService"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>

activity_email_sender.xml:

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

    <EditText
        android:id="@+id/et_to"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="To" />

    <EditText
        android:id="@+id/et_subject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Subject" />

    <EditText
        android:id="@+id/et_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Message" />

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

</LinearLayout>

Note: