You are on page 1of 11

// Print out DNS Record for an Internet Address import javax.naming.directory.Attributes; import javax.naming.directory.InitialDirContext; import javax.naming.NamingEnumeration; import javax.naming.

NamingException; import java.net.InetAddress; import java.net.UnknownHostException;

public class DNSLookup { public static void main(String args[]) { // explain what program does and how to use it if (args.length != 1) { System.err.println("Print out DNS Record for an Internet Address"); System.err.println("USAGE: java DNSLookup domainName|domainAddress"); System.exit(-1); } try { InetAddress inetAddress; // if first character is a digit then assume is an address if (Character.isDigit(args[0].charAt(0)))

{ // convert address from string representation to byte array byte[] b = new byte[4]; String[] bytes = args[0].split("[.]"); for (int i = 0; i < bytes.length; i++) { b[i] = new Integer(bytes[i]).byteValue(); } // get Internet Address of this host address inetAddress = InetAddress.getByAddress(b); } else { // get Internet Address of this host name inetAddress = InetAddress.getByName(args[0]); } // show the Internet Address as name/address System.out.println(inetAddress.getHostName() + "/" + inetAddress.getHostAddress()); // get the default initial Directory Context InitialDirContext iDirC = new InitialDirContext(); // get the DNS records for inetAddress Attributes attributes = iDirC.getAttributes("dns:/" + inetAddress.getHostName()); // get an enumeration of the attributes and print them out NamingEnumeration attributeEnumeration = attributes.getAll(); System.out.println("-- DNS INFORMATION --"); while (attributeEnumeration.hasMore()) {

System.out.println("" + attributeEnumeration.next()); } attributeEnumeration.close(); } catch (UnknownHostException exception) { System.err.println("ERROR: No Internet Address for '" + args[0] + "'"); } catch (NamingException exception) { System.err.println("ERROR: No DNS record for '" + args[0] + "'"); } } }

Sample Runs

::] javac DNSLookup.java

::] java DNSLookup google.com

google.com/64.233.167.99 -- DNS INFORMATION -A: 64.233.187.99, 72.14.207.99, 64.233.167.99 NS: ns4.google.com., ns1.google.com., ns2.google.com., ns3.google.com. MX: 10 smtp4.google.com., 10 smtp1.google.com., 10 smtp2.google.com., 10 smtp3.google.com.

::] java DNSLookup diablo.cs.fsu.edu diablo.cs.fsu.edu/128.186.120.2 -- DNS INFORMATION -A: 128.186.120.2 MX: 10 mail.cs.fsu.edu.

::] java DNSLookup fsu.edu fsu.edu/128.186.6.14 -- DNS INFORMATION -A: 128.186.6.14 NS: trantor.umd.edu., nsx.lbl.gov., dns1.fsu.edu., dns2.fsu.edu., dnsa.fsu.edu. SOA: dns1.fsu.edu. hostmaster.acns.fsu.edu. 2007020700 3600 1200 604800 86400 MX: 0 ms4.ucs.fsu.edu., 0 ms6.ucs.fsu.edu., 0 ms7.ucs.fsu.edu., 10 ms2.ucs.fsu.edu., 20 ms8.ucs.fsu.edu., 0 ms3.ucs.fsu.edu.

::] java DNSLookup 128.186.6.14 www.fsu.edu/128.186.6.14 -- DNS INFORMATION -A: 128.186.6.14

MX: 0 ms6.ucs.fsu.edu., 0 ms7.ucs.fsu.edu., 10 ms8.ucs.fsu.edu., 0 ms2.ucs.fsu.edu., 0 ms3.ucs.fsu.edu., 0 ms4.ucs.fsu.edu.

::] java DNSLookup yahoo.com yahoo.com/216.109.112.135 -- DNS INFORMATION -A: 66.94.234.13, 216.109.112.135 NS: ns9.yahoo.com., ns1.yahoo.com., ns2.yahoo.com., ns3.yahoo.com., ns4.yahoo.com., ns5.yahoo.com., ns8.yahoo.com. MX: 1 c.mx.mail.yahoo.com., 1 d.mx.mail.yahoo.com., 1 e.mx.mail.yahoo.com., 1 f.mx.mail.yahoo.com., 1 g.mx.mail.yahoo.com., 1 a.mx.mail.yahoo.com., 1 b.mx.mail.yahoo.com.

::] java DNSLookup 199.232.41.10 www.gnu.org/199.232.41.10 -- DNS INFORMATION -CNAME: gnu.org.

::] java DNSLookup 199.232.41 199.232.41.0/199.232.41.0 ERROR: No DNS record for '199.232.41'

::] java DNSLookup ghthytf.com ERROR: No Internet Address for 'ghthytf.com'

// Demonstrate URLConnection. import java.net.*; import java.io.*; import java.util.Date; class UCDemo { public static void main(String args[]) throws Exception { int c; URL hp = new URL("http://www.java-samples.com/j2me/"); URLConnection hpCon = hp.openConnection(); System.out.println("Date: " + new Date(hpCon.getDate())); System.out.println("Content-Type: " + hpCon.getContentType()); System.out.println("Expires: " + hpCon.getExpiration()); System.out.println("Last-Modified: " + new Date(hpCon.getLastModified())); int len = hpCon.getContentLength(); System.out.println("Content-Length: " + len); if (len > 0) { System.out.println("=== Content ===");

InputStream input = hpCon.getInputStream(); int i = len; while (((c = input.read()) != -1) && (i > 0)) { System.out.print((char) c); } input.close(); } else { System.out.println("No Content Available"); } } }

EMAIL

import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SimpleSendEmail { public static void main(String[] args) { // Collect the necessary information to send a simple message

// Make sure to replace the values for host, to, and from with // valid information. // host - must be a valid smtp server that you currently have // access to. // to - whoever is going to get your email // from - whoever you want to be. Just remember that many smtp // servers will validate the domain of the from address // before allowing the mail to be sent. String host = "server.myhost.com"; String to = "YourFriend@someemail.com"; String from = "Me@myhost.com"; String subject = "My First Email"; String messageText = "I am sending a message using the" + " JavaMail API.\n" + "Here type your message."; boolean sessionDebug = false; // Create some properties and get the default Session. Properties props = System.getProperties(); props.put("mail.host", host); props.put("mail.transport.protocol", "smtp"); Session session = Session.getDefaultInstance(props, null); // Set debug on the Session so we can see what is going on // Passing false will not echo debug info, and passing true // will. session.setDebug(sessionDebug);

try { // Instantiate a new MimeMessage and fill it with the // required information. Message msg = new MimeMessage(session); 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(messageText); // Hand the message to the default transport service // for delivery. Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); } } }

public class Mail { String d_email = "from@...", d_password = "***", d_host = "smtp.gmail.com", d_port = "465", m_to = "to@...", m_subject = "subject", m_text = "this is just a test mail";

public Mail() { Properties props = new Properties(); props.put("mail.smtp.user", d_email); props.put("mail.smtp.host", d_host); props.put("mail.smtp.port", d_port); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.auth", "true"); //props.put("mail.smtp.debug", "true"); props.put("mail.smtp.socketFactory.port", d_port); props.put("mail.smtp.socketFactory.class", " javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try {

Authenticator auth = new SMTPAuthenticator(); Session session = Session.getInstance(props, auth); //session.setDebug(true);

MimeMessage msg = new MimeMessage(session); msg.setText(m_text); msg.setSubject(m_subject); msg.setFrom(new InternetAddress(d_email)); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to)); Transport.send(msg); } catch (Exception mex) { mex.printStackTrace(); } } private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(d_email, d_password); } }

public static void main(String[] args){ Mail sendmail=new Mail(); } }

You might also like