- Everything You Need To Know about TCP/IP but Failed to Learn in Kindergarten
- A Client Socket in Java
- Sending Email by Java
- A Server Socket in Java
- HTTP and Web Browsing: Retrieving HTTP Pages
- How to Make an Applet Write a File on the Server
- A Multithreaded HTTP Server
- A Mapped I/O HTTP Server
- Further Reading
- Exercises
- Some Light Relief—Using Java to Stuff an Online Poll
Sending Email by Java
As our next example, let's write a Java program to send some email. Email is sent by socketed communication with port 25 on a computer system. All we are going to do is open a socket connected to port 25 on some system that is running a mail server and speak "mail protocol" to the sendmail demon at the other end. If we speak the mail protocol correctly, it will listen to what we say, and send the email for us.
The following below requires an Internet standard mail (SMTP) program running on the server. If your server has some non-standard proprietary mail program on it, you're out of luck. You can check which program you have by telnetting to port 25 on the server, and seeing if you get a mail server to talk to you.
There are two wrinkles to this approach. First, it became common for spammers to steal time on other people's mailservers to relay their spam. As a result, most mail servers are now selective about who they accept a connection from. You won't be able to get mailers around the world to talk to you, just your ISP mail server. Second, Java now has a mail API with a somewhat higher-level interface, so you don't need to understand individual mail commands. But the point here is to show some give and take over a socket connection. Again, this example shows the client end of the socket connection.
The code to send email is:
import java.io.*; import java.net.*; public class email { public static void main(String args[]) throws IOException { Socket sock; DataInputStream dis; PrintStream ps; sock = new Socket("localhost", 25); dis = new DataInputStream( sock.getInputStream()); ps = new PrintStream( sock.getOutputStream()); ps.println("mail from: trelford"); System.out.println( dis.readLine() ); String Addressee= "linden"; ps.println("rcpt to: " + Addressee ); System.out.println( dis.readLine() ); ps.println("data"); System.out.println( dis.readLine() ); ps.println("This is the message\n that Java sent"); ps.println("."); System.out.println( dis.readLine() ); ps.flush(); sock.close(); } }
Running this program will send email to the addressee. Many of the Internet services are like this one. You set up a socket connection and talk a simple protocol to tell the server at the other end what you want.
Note that the main() routine has been declared as throwing an Exception. This is a shortcut, permissible in development, to save the necessity of handling any exceptions that might be raised. It only works because exceptions are not considered part of the signature of a method. In production code, it is important to catch and handle any exceptions.
You can find all the Internet Protocols described in documents in Request For Comments (RFCs), the format in which they were originally issued, available online at: http://www.internic.net/std. The mail RFC is RFC821.txt.
You can find all the WWW protocols described in documents linked to from the URL http://www.w3.org/pub/WWW/Protocols/. A careful study of some of these documents will often answer any protocol questions you have.
You can find more information on the Java mail API at java.sun.com/prod_ucts/javamail/.
If you write a simple Swing GUI around this mail-sending code, you've written a mailer program! It's not that hard to get the RFCs for the POP3 and IMAP1 protocols and write the code to read and display incoming mail, too.