r/javahelp • • Oct 11 '19

How do I send a SMS? Aka instant message.

I am doing an e-commerce site, and I want to add a layer of security to tie accounts to two factor verification. What is the simplest or best way to send a sms via java?

18 Upvotes

18 comments sorted by

15

u/mjg123 Oct 11 '19 edited Oct 11 '19

You can send SMS using a service like Twilio: https://www.twilio.com/docs/sms/quickstart/java.

Once you have an account you can send an SMS in 2 lines of code. There's also higher-level 2FA-specific services if you need them.

(NB I work for Twilio - any questions, hit us up in /r/twilio)

3

u/teknomaden Oct 11 '19

I can vouch for Twilio. Im not affiliated with them in any way, but I've integrated with them, and their API is really fantastic.

3

u/mjg123 Oct 11 '19

High Five! You should share your promo code with OP ;)

3

u/SplitsAtoms Oct 11 '19

This works pretty well. I fumbled my way through making a raspberry pi based fish tank monitor. Twilio was one of the only things I got working perfectly without too much trouble.

1

u/Tha_Gazer Oct 11 '19

How would u do it without an api?

4

u/MeisterBounty Oct 11 '19

You can’t simply send an sms by writing java code. It’s a whole different technology stack with its own infrastructure.

you can Programm your android device to send sms if it has a SIM card.

1

u/Tha_Gazer Oct 11 '19

Is there no protocol? Kind like Voip?

4

u/MeisterBounty Oct 11 '19

There is. But as I said, it’s a very different technological stack which you don’t have access to by using your pc and writing java code. Imagine you need high powered antennas, tons of bit shifting, very specific algorithms and so on. It’s a very complex topic.

8

u/morhp Professional Developer Oct 11 '19

Imagine you need high powered antennas, tons of bit shifting, very specific algorithms and so on. It’s a very complex topic.

That's not really the problem. The problem is more that you need access to the mobile phone network, so you either need to register as your own provider or you need some contract with an existing provider. After that, sending an SMS won't be that difficult, probably just need internet access to some gateway server.

Or you just pretend you're a normal client and use an android device (or even better a USB-Modem for a PC) and use that to send an SMS. That's probably the easiest solution but you need to make sure the software/driver has an interface that you can access easily with Java. Probably want to use a Linux PC and a compatible modem as that will be way easier.

4

u/MeisterBounty Oct 11 '19

Thanks for the solution oriented insight!

2

u/morhp Professional Developer Oct 11 '19

Here is an example how to do it with a raspberry pi: https://escapologybb.com/send-sms-from-raspberry-pi/

Obviously you can use any other Linux pc instead and you probably don't need to use this exact software, but assuming you do, executing something like echo "test" | sudo gammu sendsms TEXT <phone number> from java will be very easy with the process API. Other SMS interfaces just require you to write a file in a certain directory to send the SMS which is even easier from Java.

1

u/MeisterBounty Oct 11 '19

That was an interesting read, thanks! Although this kind of solution is the only feasible for a private project. Substituting the generic phone with a raspberry pi and using this as a client in the mobile network is very cool.

1

u/Tha_Gazer Oct 11 '19

Interesting

1

u/mjg123 Oct 11 '19

Ultimately you need a relationship with a telecomms provider. Whether you do that through a cloud service, a reseller or directly is up to you, depending on your scale, finances and technical resources. Twilio aims to make it as easy as possible for developers to get started & to scale up as you need.

3

u/irocgts Oct 11 '19 edited Oct 11 '19

So there is a way without third party software but its weird. You can send emails to phone numbers if you know the right address. Each carrier is different. This is the link for verizon, just read 'How do I send a text to a mobile number from my email?' part: https://www.verizonwireless.com/support/text-messaging-faqs/

edit: I didn't read the whole question. I guess this is more of a fun fact. Don't do this if you are making a commercial product. This is more of like a college project fun thing. You really should use a 3rd party API.

2

u/irocgts Oct 11 '19

This was sort of common knowledge a bunch of years ago (~20 years) before these 3rd parts APIs were well known. I am glad its still a thing.

2

u/wildjokers Oct 11 '19

Need to use an API or have a relationship with all the carriers.

https://rapidapi.com/collection/free-sms-apis

Those apis are either free or offer a free trial or offer a certain number of messages for free then start charging.

Twilio is crazy cheap though, something like 0.007 cents per SMS to a US number. You get $15 dollars free with your trial account. Have to pay $1 of that for a number leaving you $14 to send SMS messages. That is about 2000 SMS messages.

I am still using the free trial account to get text notifications when my 3d prints are done (should last me a few years).

1

u/TeddyHH Oct 11 '19

Aren't there java APIs for skype? Pretty sure skype can send text messages.