Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Java and YAC: Take Caller ID Beyond the Phone and into the Network : Page 3

Your modem probably supports caller ID, so why not leverage the service? Learn how to apply a client/server model that provides caller ID information via sockets and then consumes and distributes it via Java.


advertisement

Using JavaMail for Email Caller ID Notification

So far, you have simply reported the caller ID information payload you receive to the console using System.out. Using the JavaMail API, you can send the caller ID information you receive to a specified email address. (If you are unfamiliar with the JavaMail API, take a look at JGuru’s Fundamentals of the JavaMail API short course.) To be able to send mail using the JavaMail API, you must have the JARs of the JavaMail API, as well as the Java Beans Activation Framework, in your classpath.

To get the YACListener class to send out an email for received caller ID information, you need to specify the following settings at the beginning of the class:

  • Your SMTP server info
  • The email address that you want to be the sender of your caller ID info (which your SMTP server allows to send mail out)
  • The address of who you want to receive the caller ID email

The beginning of your class will look like the following example:



static String SMTPSERVER = "smtp.austin.ibm.com"; static String MAILSENDERADDRESS = "sender@somewhere.com"; static String MAILRECIPIENTADDRESS = "recipient@someotherplace.com";

The static sendMail method sends out your email. The following is the core processing logic for this method:

Properties props = System.getProperties(); props.put("mail.smtp.host", SMTPSERVER); Session session = Session.getDefaultInstance(props, null); // -- Create a new message -- Message message = new MimeMessage(session); // -- Set the FROM and TO fields -- message.setFrom(new InternetAddress(MAILSENDERADDRESS)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(MAILRECIPIENTADDRESS, false)); // -- Set the subject and body text -- String subject = "Caller ID info received from YAC"; message.setSubject(subject); String body = yacPayLoad; message.setText(body); message.setSentDate(new Date()); // -- Send the message -- Transport.send(message); System.out.println("Message sent OK.");

To summarize, you create a javax.mail.Session using a Properties object populated with your SMTP server information. Using the Session, you create a javax.mail.Message object, to which you associate your sender and recipient email addresses, using the setFrom and setRecipients methods respectively. Using the setSubject method, you set the subject of the Message to the String: "Caller ID info received from YAC". Next, you set the body of your Message object to the message that was sent from the YAC server. It is this message that contains your caller ID information. Finally, you send the message using the following call:

Transport.send(message);

Using JavaMail for Text Messaging Caller ID Notification

In addition to sending your caller ID information via email, wouldn't sending it to your cell phone via text messages be nice? This operation would typically require a third-party SMS gateway, but alternatively you could send text messages via e-mail, a service many cell phone providers offer. For example, to send a text message to a Sprint PCS phone, you can send an email to 8155551234 @messaging.sprintpcs.com if your Sprint PCS cell phone number is (815) 555-1234. Accordingly, to send messages via the YACListener.java class, simply replace the MAILRECIPIENTADDRESS static String with the email address corresponding to your desired cell phone number destination. Most cell phone providers in the U.S. provide an email address to cell phone mapping. You might also want to take a look at Teleflip, a free service that delivers text messages to cell phones.

Provide Caller ID Information via Sockets

This article demonstrated how to use YAC, an open-source Windows application that uses a client/server model to provide caller ID information via sockets. It showed how to consume the caller ID information via the Java programming language and leverage the JavaMail API to send caller ID information via e-mail.



Kulvir Singh Bhogal works as an IBM consultant, devising and implementing J2EE-centric solutions at customer sites across the nation.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap