0

I'm trying to develop an app that sends SMS messages to email addresses. Using the default messaging app on my Note 5, I can enter in my email address as the recipient and send a message no problem. (I have Sprint.) But when I try to do this in code:

        Intent intentSent = new Intent(Globals.SENT);

        intentSent.putExtra("phoneNumber",phoneNumber);
        Intent intentDelivered = new Intent(Globals.DELIVERED);
        intentDelivered.putExtra("emailAddress",emailAddress);

        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
                intentSent, 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
                intentDelivered, 0);

        SmsManager sms = SmsManager.getDefault();
        if (emailAddress != null && messageBody.length() > 0) {
            sms.sendTextMessage(emailAddress, null, messageBody, sentPI, deliveredPI);
        }

The Sent receiver is not called and the message shows up in my messaging app as that it was sent to "Anonymous" and that delivery failed. The Delivered receiver does call onReceive():

public class SMSDeliveredReceiver extends BroadcastReceiver {
  private String TAG = "SMSDeliveredReceiver";
  Context context;

  @Override
  public void onReceive(Context context, Intent intent) {
    this.context = context;
    Bundle extras = intent.getExtras();
    if (extras != null) {
        String emailAddress = extras.getString("emailAddress","");
        Log.d(TAG,emailAddress );
        switch (getResultCode())
        {
            case Activity.RESULT_OK:
                Toast.makeText(context,"Email received",Toast.LENGTH_LONG).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(context, "SMS not delivered",
                        Toast.LENGTH_LONG).show();
                break;
        }

    }

  }
}

Is there a way to do this successfully? This answer will not help me as I want to do this for all carriers. Android: How to send SMS to email account

Community
  • 1
  • 1
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • I would guess that Messenger is actually converting the message to MMS. If it's not giving any indication of that, you can check it by querying the SMS and MMS Providers separately, and seeing where the message ends up. You're not going to be able to do this with `sendTextMessage()`, AFAIK. Since API 21, the `sendMultimediaMessage()` method is available, though I've not had a chance to play around with it, and using it is [rather involved, apparently](https://github.com/android/platform_development/blob/master/samples/ApiDemos/src/com/example/android/apis/os/MmsMessagingDemo.java). – Mike M. Nov 11 '16 at 12:32
  • Just to clarify, I meant that Messenger converts the message to MMS when it itself sends it. When you try to send the message with `sendTextMessage()`, it'll probably still end up in the SMS table, after it fails. Sorry if there was any confusion. I was just reviewing my comments, and noticed that was a little ambiguous. – Mike M. Nov 11 '16 at 14:03
  • 1
    Thanks very much for your comments. I think I am going to attack this in a different method. – Kristy Welsh Nov 11 '16 at 20:00

0 Answers0