Posts Tagged Internet Protocol

SuzanneUse JavaMail to Automate Email Distribution

Saturday, March 6th, 2010

Every action can possibly be automated with software, then why not automate the email distribution to end-users/customers. In this case, the JavaMail API, currently in version 1.4.3, can support this development in a protocol-independent manner and remain platform independent as always.

Here’s the JavaMail code for sending the email:

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

class EmailClient
{
    final String emailInfo = "EmailInfo.properties";
    Properties properties = new Properties();

    public static void main(String args[])
    {
        EmailClient emailClient = new EmailClient ();
        emailClient.sendEmail();
    }

    private void sendEmail()
    {
       try{
            //This is required to load all the properties
           FileInputStream fileInputStream = new FileInputStream(emailInfo);
           properties.load(fileInputStream);
           fileInputStream.close();
        }catch(IOException ioe)
       {
            //throw IOException of your choice.
            //can end here
       }
        System.out.println("Email properties read successfully.");

       String smtpAddress = properties.getProperty("smtpAddress");
       String fromAddress = properties.getProperty("fromAddress");
       String toAddress = properties.getProperty("toAddress");
       String emailSubject = properties.getProperty("emailSubject");
       String emailBody = properties.getProperty("emailBody");

       Properties props = new Properties();
        props.put("mail.smtp.host", smtpAddress);
        props.put("mail.from", fromAddress);
       Session session = Session.getInstance(props, null);

       try
       {
           MimeMessage mimeMessage = new MimeMessage(session);
           mimeMessage.setRecipients(Message.RecipientType.TO,toAddress);
           mimeMessage.setSentDate(new Date());
           mimeMessage.setSubject(emailSubject);
           mimeMessage.setText(emailBody);
           System.out.println("Sending e-mail...");
           Transport.send(mimeMessage);
           System.out.println("e-mail sent.");
       }
        catch(MessagingException me)
       {
           System.out.println("e-mail send failed.");
           me.getMessage();
       }
    }
}

The code is fairly simple to understand:

  • The properties have been loaded for the email to be sent from a properties file. This could come from any other source, such as hardcoded values, a database, a user interface, etc.
  • The processed information is updated to the MimeMessage object, which is the actual email object. (The MIME, or Multipurpose Internet Mail Extensions, is an Internet standard that specifies how messages must be formatted for interoperability.)
  • MimeMessage takes a session argument, which can either be created or refer to the default session.
  • The session—important in any context and used here for the email—is established using the properties mail.smtp.host and mail.from.
  • Other details such as the date, subject, and the body of the email, which will be used when the email is sent, are being set out.
  • Finally, the email is dispatched using the send() method of the Transport class.
  • The entire email information is made available in a properties file. Here are the contents of the properties file for this application:

smtpAddress=smtp.server.address
fromAddress=myself@me.com
toAddress=someone@you.com
emailSubject=First email
emailBody=My first email

It’s stated previously that the source of the required information can vary, so we can modify the entries at will without recompiling the code to experiment. By the way, holding configurable information in properties files is a standard programming practice.

It seems to be simple right, and now let’s add more advanced features to the application. Don’t you think, you need attachments with email? The following code creates that-

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import java.io.*;

class SendAdvancedEmail
{
    final String emailInfo = "EmailInfo.properties";
    Properties properties = new Properties();

    public static void main(String args[])
    {
        SendAdvancedEmail sendAdvancedEmail = new SendAdvancedEmail ();
        sendAdvancedEmail.sendEmail();
    }

    private void sendEmail()
    {
       try{
            //This is required to load all the properties
           FileInputStream fileInputStream = new FileInputStream(emailInfo);
           properties.load(fileInputStream);
           fileInputStream.close();
        }catch(IOException ioe)
       {
            //throw IOException of your choice.
            //can end here
       }
        System.out.println("Email properties read successfully.");

       String smtpAddress = properties.getProperty("smtpAddress");
       String fromAddress = properties.getProperty("fromAddress");
       String toAddress = properties.getProperty("toAddress");
       String emailSubject = properties.getProperty("emailSubject");
       String emailBody = properties.getProperty("emailBody");

       Properties props = new Properties();
        props.put("mail.smtp.host", smtpAddress);
        props.put("mail.from", fromAddress);
       Session session = Session.getInstance(props, null);

       try
       {
           MimeMessage mimeMessage = new MimeMessage(session);
           mimeMessage.setRecipients(Message.RecipientType.TO,toAddress);
           mimeMessage.setSentDate(new Date());
           mimeMessage.setSubject(emailSubject);
            //The following is required to add attachments
           MimeMultipart multipart = new MimeMultipart();
           //Creating the text part of the email message
           BodyPart bodyPart = new MimeBodyPart();
            //The type is set to "text/plain"
           bodyPart.setText(emailBody);
           //Creating the attachment part of the email message
           BodyPart attachment = new MimeBodyPart();
           DataSource source = new FileDataSource(emailInfo);
           attachment.setDataHandler(new DataHandler(source));
           attachment.setFileName(emailInfo);
           //attachment.setContent(properties, "text/html");
            //Now combining the email message and the attachment
           multipart.addBodyPart(bodyPart);
           multipart.addBodyPart(attachment);
           //Setting the message with the multipart just created
           mimeMessage.setContent(multipart);

           System.out.println("Sending e-mail...");
           Transport.send(mimeMessage);
           System.out.println("e-mail sent.");
       }
        catch(MessagingException me)
       {
           System.out.println("e-mail send failed."+me);
           me.getMessage();
       }
    }
}

The important difference between this code listing and the one for sending email in the previous section is the process of building the message body, which is now capable of adding a file as an attachment. The Multipart class is actually split into two sections, the message part and the attachment. The properties are actually the same as for the previous code listing.

You could use Multipart instead of MimeMultipart, but be aware that Multipart is an abstract class and hence requires you to create an instance of MimeMultipart. It’s a similar case with MimeMessage. You can use Message, which also is an abstract class. Understanding all the classes used here will help you familiarize yourself with the mail package.

The article has provided enough information for you to experiment with sending and receiving emails. But you need to know-

  • The details in the properties file must be accurate to achieve the desired results.
  • Ensure that you have mail.jar and activation.jar in your classpath.

http://www.all1martpro.com
http://www.all1sourcetech.com

Tags: , , , , , , ,
Posted in Editorial, Purely Technical | No Comments »

NaggieHow to build a PHP Link Scraper with cURL?

Thursday, March 4th, 2010

Let’s build a robot, which scrapes links from web pages and dumps them in a database, and then it read those links from the database and follows them, scraping up the links on those pages, and so on ad infinitum.

To begin, let’s have a look at the groundwork.

The cURL Component-

cURL (or “client for URLS”) is a command-line tool for getting or sending files using URL syntax. It was first used in 2007 by Daniel Stenberg as a way to transfer files via protocols such as HTTP, FTP, Gopher, and many others, via a command-line interface. Since then, many more contributors has participated in further developing cURL, and the tool is used widely today.

Using cURL with PHP-

PHP is one of the languages that provide full support for cURL. (Find a listing of all the PHP functions you can use for cURL.) Luckily, PHP also enables you to use cURL without invoking the command line, making it much easier to use cURL while the server is executing. The example below demonstrates how to retrieve a page called example.com using cURL and PHP.

<?php
$ch = curl_init(”http://www.example.com/”);
$fp = fopen(”example_homepage.txt”, “w”);
curl_setopt($ch, cURLOPT_FILE, $fp);
curl_setopt($ch, cURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

<?php


$ch = curl_init(”http://www.example.com/”);
$fp = fopen(”example_homepage.txt”, “w”);


curl_setopt($ch, cURLOPT_FILE, $fp);
curl_setopt($ch, cURLOPT_HEADER, 0);


curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

The Link Scraper-

For the link scraper, you will use cURL to get the content of the page you are looking for, and then you will use some DOM to grab the links and insert them into your database. You can build the database from the information below; it is really simple stuff.

$query = mysql_query(”select URL from links where visited != 1);
if($query)
{

 	while($query = mysql_fetch_array($result))
 	{

$target_url = $query['url'];
$userAgent = ‘ScraperBot’;

Next, grab the URL from the database table inside a simple while loop.

$ch = curl_init();
curl_setopt($ch, cURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, cURLOPT_URL,$target_url);

After instantiating cURL, you use curl_setopt() to set the USER AGENT in the HTTP_REQUEST, and then tell cURL which page you are hoping to retrieve.

curl_setopt($qw, cURLOPT_FAILONERROR, true);
curl_setopt($qw, cURLOPT_FOLLOWLOCATION, true);
curl_setopt($qw, cURLOPT_AUTOREFERER, true);
curl_setopt($qw, cURLOPT_RETURNTRANSFER,true);
curl_setopt($qw, cURLOPT_TIMEOUT, 20);

You’ve set a few more HEADERS with curl_setopt(). This time, you made sure that when an error occurs the script will return a failed result, and you set the timeout of each page followed to 20 seconds. Usually, a standard server will time-out at 30 seconds, but if you run this from your localhost you should be able to set up a no-timeout server.

$html= curl_exec($qw);
if (!$html)
{

 	echo "ERROR NUMBER: ".curl_errno($ch);
 	echo "ERROR: ".curl_error($ch);
 	exit;

}

Grab the actual page by sending the HEADERS along while executing the cURL request using curl_exec(). If an error occurs, it will be reported to PHP by the number and description inside curl_errno() and curl_error, respectively. Obviously, if such an error exists, you exit the script.

$dom = new DOMDocument();
@$dom->loadHTML($html);

Next, you create a document model of your HTML (that you grabbed from the remote server) and set it up as a DOM object.

$xpath = new DOMXPath($dom);
$href = $xpath->evaluate(”/html/body//a”);

Use XPATH to grab all the links on the page.

for ($i = 0; $i < $href->length; $i++) {

 	$data = $href->item($i);
        $url = $data->getAttribute('href');
 	$query = "INSERT INTO links (url, gathered_from) VALUES ('$url', '$gathered_from')";
 	mysql_query($query) or die('Error, insert query failed');
        echo "Successful Link Harvest: ".$url;
 	}

}

Dump all the links into the database, as well as the URL they are gathered from, just so you never go back there again. A more intelligent system might have a separate table for URLs already visited, as well as a normalized relationship between the two.

Going a step further than just grabbing the links enables you to harvest images or entire HTML documents as well. This is kind of where you start when building a search engine. Creating your own search engine may seem naively ambitious, and this little bit of code may inspire you a bit.

Source:- http://www.developer.com

http://www.all1social.com

http://www.all1martpro.com

Tags: , , , , ,
Posted in Editorial, Expert's Opinions, Purely Technical | No Comments »

SuzanneHP debuts Android-based smartbook

Tuesday, February 16th, 2010

The Compaq AirLife 100 has been launched by Hewlett-Packard, it’s a smartbook based on the Android OS and Qualcomm’s Snapdragon processor.

According to HP, the AirLife 100, which was developed for accessing the Web, takes the best of a smartphone and puts it into a netbook design.

The device turns on instantly, has up to 12 hours of battery life and up to 10 days of standby time. It comes with a customized touchscreen user interface, which features a new “tabbed” touch-enabled browser, a mechanism for zooming on Web pages and a touch optimized media suite.

The extended battery life comes courtesy of Qualcomm’s QSD8250 processor, said HP. The Snapdragon processor is used by a growing number of smartphones, including the Android-based Acer Liquid, the Google Nexus One and the upcoming Sony Ericsson Xperia X10.

Like an Android-based smartphones, the AirLife 100 comes with support for GPS and Internet access using 3G and Wi-Fi. The design is standard netbook fare: the device has a 10.1-inch screen and a keyboard that is 92 percent the size of a regular one, according to HP.

Data is stored on a 16GB SSD (solid-state drive) or an SD card.

And the AirLife 100 will start shipping this spring in Europe, via a deal with operator Telefónica, which operates under the O2 brand in the U.K. and Germany. There is no information on pricing or availability in other countries yet.

With the HP’s Android-based smartphones, the additional smartphones, netbooks, and other devices with the OS will be shown at next week’s Mobile World Congress show in Barcelona.

http://get-a-designer.com

http://www.all1sourcetech.com

Tags: , , , , , , ,
Posted in New Product Release, Technical News | No Comments »

NaggieOpera Develops Mini Browser for Apple’s iPhone

Saturday, February 13th, 2010

Opera announced to release it’s newly developed version of its Opera Mini web browser for Apple’s iPhone, which will be demonstrated at the Mobile World Conference in Barcelona, Spain.

The cofounder of Opera Software, Jon von Tetzchner said he is “thrilled” to offer the preview and added that the software provides a “fast, feature-rich” experience for iPhone users. Opera on the iPhone brings the company “one step closer” to its mission of “bringing the web to the world”.

Will Apple Approve?

However, the big question is whether Apple will approve Opera as an iPhone application on its App Store. Apple currently only accepts browsers that run on the WebKit engine — the same engine that powers Apple’s Safari browser.

But Apple has gotten into hot water with the Federal Trade Commission over its initial refusal to certify Voice over Internet Protocol applications, Rob Enderle, principal analyst with the Enderle Group, noted. This time around, Apple will be hard-pressed to exclude competitors.

Enderle said, Given Apple is under FTC review for their application approval process, I don’t think they can refuse this application — but they could break it or limit its capability. “For instance, both the ARM processor and the Opera browser support flash on other platforms, but Apple will likely make sure Flash doesn’t work on Opera any better than it does on Safari on the iPhone.”

“Changing defaults will likely be difficult as well,” Enderle added, “so that it is the Safari browser that opens when you click on a link. If Opera gets on the phone, it is likely that [Google's] Chrome will soon follow, and Apple will do whatever it can to make sure that never happens.”

Advanced Features

Currently, Opera Mini 5 in beta boasts quite a few advanced features over Safari, including tabbed browsing so users can view several sites simultaneously and “easily jump from one to another,” the company said. Another feature is Speed Dial, a display of frequently used pages so users can launch a favorite site with just one click, a feature Safari and Chrome offer in desktop versions but not on smartphones. There’s also Opera Link, which synchronizes bookmarks and Speed Dial between the user’s mobile phone and desktop computer, and Download Manager, which manages downloads from the browser.

According to Opera, its browser offers an intuitive, easy-to-user interface, and is much faster than other browsers because it compresses web pages as much as 90 percent before downloading. Other features include adaptive zoom, in-page searching, landscape mode, and kinetic scrolling.

A senior research analyst at IDC, Raman Llamas said, Opera has a great browser and offers a great experience.

Lucky in 2010?

In 2008, von Tetzchner told The New York Times that Opera had started work on an iPhone version of Opera but stopped development because of Apple’s restrictive developer agreements. Apple initially banned all third-party browsers but finally allowed other browsers based on WebKit.

Around this time, von Tetzchner clearly feels he will have more luck with Apple. Indeed, given that Opera is making a big deal about the availability of Opera Mini 5 for iPhone, Opera may have already received Apple’s blessing.

According to Christen Krogh, Opera’s chief development officer, Opera Mini is compatible with every requirement for the App Store. Krogh also said, there aren’t any identical applications on the iPhone, discounting Safari as an identical application. Opera Mini is a different kind of browser, so we cannot see any conflict with any requirements in the App Store.

Krogh said Opera hasn’t yet submitted the application to the App Store. “We’re comfortable enough [with] where Opera Mini is at to show it to select partners and journalists,” he said. “It won’t take long before we’re ready to submit it to the App Store.”

Tags: , , , , , , , , , , ,
Posted in New Product Release, Opensource, Technical News | No Comments »