RunCodes
Programming & Tech

How to Send Free SMS using Java?[With Source Code]

4 3,294

[sociallocker]

As you all know guyz, the era that we are living is an era of internet, so everyone is hanging on their PC all the time. Everyday we are sending some SMS to our friends in order to inform something to them. So, if there is some method that can send SMS for free, everyone gonna use that method and loved it. Today, in this tutorial we are gonna discuss about that method using java. And also if you wanna send the message to your friends without displaying your number and name, this tutorial is for you! sometime we are in the mode of cracking jokes and wanna fool your friends or sending some threatening message(only for entertainment purpose otherwise it will be illegal) you can use this method. This method of sending SMS is called email2sms Method.

“This tutorial may be very useful for those who want to integrate SMS features in their project which is developed in java”.

For sending SMS using email2sms approach: you have to do two task:

1.   Selecting Carrier

2.    Writing java code

we are selecting txtlocal as our carrier. So, All you have do is go to http://www.txtlocal.co.uk and there you need to create one account. after creating account. (you need to fill up some of your information and txtlocal will send you a verification email into your email and verify your account [watch the video for detail]) you have to hover your mouse over setting menu and click check box of active email2sms.

After doing this, you complete the task of selecting your carrier. Now write the following piece of code in any text editor software or any IDE, i am using Netbeans IDE.

Now let’s start.

I am assuming that you have Netbeans IDE, Open your Netbens IDE and click on File>New and then select java from the categories and java application from project and click next and then the provide the name of your project as you want and click finish. After completing this, you may see new folder in your project explorer in your Netbeans. Expand that folder, expand source package folder and under that folder another sub-folder will be appear. So right click on that folder and click on new and then select jframe form.

New window will appear and then provide the name of your form and then click finish.

Then you may able to see the full window having jframe form, drag the corner to expand it and from the palette drag and drop four label, two text field, one password field, one text area and one button.

Four Label named: phone no, gmail, password, message
Two TextField for phone no and gmail
One Password Field for gmail password
One TextArea for message
one Button for send

And change the text of label as well as button and variable name of text field from their properties window just by right clicking.

After completing this designing part, you need to download these two jar file:

Download mail.jar: http://www.oracle.com/technetwork/java/index-138643.html

Download activation.jar: http://www.oracle.com/technetwork/java/jaf11-139815.html

After downloading these two file, you need to add these java file in your project libraries section. So right click on the libraries folder from the project explorer, then click on add JAR/folder and select that downloaded jar file and click on add.

[sociallocker]

Double click on the send button and scroll up and then import these packages in your program:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage;

Now after complete these step, write the following piece of code by double clicking in your button:

try{
String host = "smtp.gmail.com";
String user = txtGmail.getText();
String pss = txtPass.getText();
String to = txtPhone.getText();
String from = user;
String subject = "Message";
String message = txtMessage.getText();
boolean sessionDebug = false;
Properties pros = System.getProperties();
pros.put("mail.smtp.starttls.enable", "tue");
pros.put("mail.smtp.host", "host");
pros.put("mail.smtp.auth","true");
pros.put("mail.smtp.port","587");
pros.put("mail.smtp.starttls.required","true");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Session mailSession = Session.getDefaultInstance(pros,null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address ={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pss);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
JOptionPane.showMessageDialog(null,"message send successfully");

}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
}

May be after sending 10 SMS your credit will be finished! but don’t be sad you can send more and more SMS but you need to create another account with another email account in www.txtlocal.co.uk ! that’s it. you may not be that much happy to hear this (sending only 10 SMS) but don’t forget you are able to send these 10 SMS for free and also there is one line “To get something, you must lose something”. it means you have to spend some time in creating account in txtlocal.co.uk in order to send free SMS.

Finally, if you guyz think, this is awesome tutorial and help you in some extend please share this tutorials with your all friend so that they can know about this method of sending SMS!

[/sociallocker]

For more details watch the video:

 

 

 

Show Comments (4)