JAVA code example

ViaNett provides you with code examples and programming objects, to help you connect to our gateway using the programming language of your choice. You are welcome to try these scripts. Sign up now to get 5 free SMS.

What is Java?

Java refers to a number of computer software products and specifications from Sun Microsystems that together provide a system for developing application software and deploying it in a cross-platform environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones on the low end, to enterprise servers and supercomputers on the high end.

Please visit the official Java homepage if you want to know more.

Java code - Simple send SMS example

This example uses a ready-made Java Object API. To download this package, simply register for free and get 5 free SMS.

import no.vianett.sms.SmsEventListener;
import no.vianett.sms.Sms;
import no.vianett.sms.SmsEvent;
import no.vianett.sms.component.SmsTransceiver;
import no.vianett.sms.log.SmsScreenLogger;
import no.vianett.sms.event.SmsDeliveredEvent;
import no.vianett.sms.event.SmsSendingFailedEvent;
import no.vianett.sms.event.SmsDeliveryFailedEvent;
 
public class SmsSender implements SmsEventListener
{
    private SmsTransceiver transceiver = null;
    private Object link = null; // Just to keep this object alive.
    private int counter = 0;
 
    public SmsSender()
    {
        this.link = this; // Keeps this object alive.
        this.transceiver = SmsTransceiver.getInstance(); // Get the transceiver object.
 
        // Initialize transceiver.
        String smsHost = "yap.vianett.no";
        String smsPort = "31337";
        String smsUsername = "username";
        String smsPassword = "password";
        this.transceiver.initialize( smsHost, Integer.parseInt( smsPort ), smsUsername, smsPassword, new SmsScreenLogger() );
 
        this.transceiver.addSmsEventListener( this ); // Registrer this class as listener for SMS events.
 
        // Send message
        Sms sms = new Sms();
        sms.setId( ++this.counter );
        sms.setReplyPath( 100 );
        sms.setSender( "1963" ); // Set the sender number.
        sms.setMessage( "Hello World!" );
        sms.setRecipient( "xxxxxxxx" ); // The recipients phone number.
 
        this.transceiver.send( sms );
    }
 
    // Just make new instance of this class.
    public static void main( String[] args ){ new SmsSender(); }
 
    /**
    * Listener.
    *
    * @param event a <code>no.vianett.sms.SmsEvent</code>
    */
    public void eventHappened( SmsEvent event )
    {
        if( event instanceof SmsDeliveredEvent )
        {
            System.out.println( "Sms delivered." );
            System.out.println( "Refno : " + event.getReferenceId() );
            System.out.println( "Sms generated by : " + event.getSource().getClass() );
        }
        else if( event instanceof SmsSendingFailedEvent )
        {
            System.out.println( "Sms sending failed." );
            System.out.println( "Refno : " + event.getReferenceId() );
            System.out.println( "Return code : " + ( ( SmsSendingFailedEvent ) event ).getReturnCode() );
            System.out.println( "Sms generated by : " + event.getSource().getClass() );
        }
        else if( event instanceof SmsDeliveryFailedEvent )
        {
            System.out.println( "Sms delivery failed." );
            System.out.println( "Refno : " + event.getReferenceId() );
            System.out.println( "Error code : " + ( ( SmsDeliveryFailedEvent ) event ).getErrorCode() );
            System.out.println( "Sms generated by : " + event.getSource().getClass() );
        }
    }
 
}

Java code - Other examples

SMS send/receive - Click to show example
This example shows how to create a Java application with send and receive functionality.

JSP Send SMS - Click to show example
This example shows how to create an SMS sender object in JSP. This is the simplest example, and is easy to understand.

Download Example source code

You need to log in to download the source code. Sign up now and get 5 free SMS messages.

Download Simple send SMS example
Download SMS send/receive example
Download JSP send SMS example

Download API Package

You need to log in to download the source code. Sign up now and get 10 free SMS messages. The packages you can download below contain a Java JAR file, Javadoc and JMLDoc.

Description Version File
API package 2.1 ViaNett_SMS_Package_v2_1.zip
API package 1.7 ViaNett_SMS_Package_v1_7.zip
API package 1.6 ViaNett_SMS_Package_v1_6.zip