I have an android app which is send sms to email.  SmsReciever.java file:
package com.example.smstoemailforwarder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

import com.example.smstoemailforwarder.EmailService;

public class SmsReceiver extends BroadcastReceiver {

    private static final String TAG = "SmsReceiver";
    private static final String DESIRED_NUMBER = "";  // Replace with your desired sender number

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus != null) {
                for (Object pdu : pdus) {
                    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
                    String sender = smsMessage.getDisplayOriginatingAddress();
                    String messageBody = smsMessage.getMessageBody();

                    if (DESIRED_NUMBER.equals(sender)) {
                        Log.d(TAG, "Received SMS from " + sender + ": " + messageBody);
                        Toast.makeText(context, "SMS from desired sender: " + messageBody, Toast.LENGTH_SHORT).show();

                        // Optionally, start the EmailService to send this message via email
                        Intent emailIntent = new Intent(context, EmailService.class);
                        emailIntent.putExtra("message_body", messageBody);

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            context.startForegroundService(emailIntent);
                        } else {
                            context.startService(emailIntent);
                        }
                    }
                }
            }
        }
    }
}

EmailService.java file:
package com.example.smstoemailforwarder;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
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 EmailService extends Service {
    private static final String CHANNEL_ID = "EmailServiceChannel";

    @SuppressLint("ForegroundServiceType")
    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();
        }
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Email Service")
                .setContentText("Running email service")
                // .setSmallIcon(R.drawable.ic_notification) // Uncomment and provide an icon resource if available
                .build();

        startForeground(1, notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null && intent.hasExtra("message_body")) {
            final String messageBody = intent.getStringExtra("message_body");
            // Assuming you also receive email credentials and recipient details
            final String username = "@gmail.com";
            final String password = "";
            final String recipient = "@gmail.com";
            final String subject = "New SMS received";

            new Thread(new Runnable() {
                @Override
                public void run() {
                    sendEmail(username, password, recipient, subject, messageBody);
                }
            }).start();
        }
        return START_STICKY;
    }

    private void sendEmail(String username, String password, String recipient, String subject, String messageBody) {
        try {
            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");

            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
            message.setSubject(subject);
            message.setText(messageBody);

            Transport.send(message);
            Log.d("EmailService", "Email sent successfully!");
        } catch (MessagingException e) {
            e.printStackTrace();
            Log.e("EmailService", "Error sending email", e);
        }
    }

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

    @RequiresApi(Build.VERSION_CODES.O)
    private void createNotificationChannel() {
        NotificationChannel serviceChannel = new NotificationChannel(
                CHANNEL_ID,
                "Email Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT
        );

        NotificationManager manager = getSystemService(NotificationManager.class);
        if (manager != null) {
            manager.createNotificationChannel(serviceChannel);
        }
    }
}

Then the app is trying to sent the app is crashed and closed.