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 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. If your ISP uses a different port for mail (mine uses port 5190) use that instead. 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.

Here's how you use telnet to say helo to the SMTP program on port 25 (the bold lines are what you type, the other lines are responses to you):

telnet yourisp.com 25
220 yourisp.com SMTP
HELO
250 yourISP.com
QUIT
221 yourisp.com

You could feed email to the server by hand, if you memorized the protocol and had the patience. There are two wrinkles to connecting to SMTP servers. 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;
         BufferedReader bis;
         PrintStream ps;

         sock = new Socket("localhost", 25);
         bis = new BufferedReader(
                         new InputStreamReader(sock.getInputStream()));
         ps = new PrintStream(sock.getOutputStream());

         ps.println("mail from: trelford");
         //System.out.println( dis.readLine() );
         // Exercise for student:
         // check all responses from the SMTP server
         // They should all start with "2nn" or "3nn"
         // Any different reply code means a failure to send mail.
         System.out.println(bis.readLine());

         String Addressee= "linden";
         ps.println("rcpt to: " + Addressee);
         //System.out.println( dis.readLine() );
         System.out.println(bis.readLine());

         ps.println("data");
         //System.out.println( dis.readLine() );
         System.out.println(bis.readLine());

         ps.println("This is the message  that Java sent");
         ps.println(".");
         System.out.println(bis.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 text request-and-response 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. Do a web search to find a mirror near you. The mail RFC is RFC821.txt. 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/products/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 IMAP[1] protocols and write the code to read and display incoming mail, too.


[1] POP3 is “Post Office Protocol 3,” and IMAP is “Internet Mail Access Protocol,” different standards for the client end of mail systems. POP3 downloads mail and keeps it on the client. IMAP keeps the mail on the server, which is more convenient if you read it from several different computers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.234