Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: SMTP
Expertise: Beginner
Mar 20, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using .net Classes to Log On to Mail Server

How do I use the .net classes to log on to a mail server and request, say, the number of new mail messages? Or send a message? Just POP3/SMTP.

An example would be helpful.

Java provides classes for sending e-mail via the SMTP protocol, but doesn't have any facilities for reading e-mail from a mail server. To create a mail reader, you'd have to implement the appropriate protocol (such as POP or IMAP) yourself using the Socket class. While this is do-able, it may be more work than you think if you insist on implementing the complete protocol. Check to see if there are public domain implementations of these protocols available on the Net before trying to code it yourself.

Sending e-mail, on the other hand, is simple. Among Java's networking classes is an SMTP client, called sun.net.smtp.SmtpClient, that allows you to do it.

Here's a synopsis of sun.net.smtp.SmtpClient as of JDK1.0.2:


               New SMTP client connected to host host.
               Create an uninitialized SMTP client.
               issue the QUIT command to the server and close the connection.
               set the From line of the email
               start sending the body of the email
               set the To line of the email message
In fact, Sun's HotJava browser uses this package to implement the sendto operation for sending mail from the browser. In general, the sun.* packages provide the underlying implementation for the public Java APIs, and are themselves available to Java programmers. However, because they are not part of the published Java API, they may change from release to release.

The following applet demonstrates how to use this package to send e-mail. It creates a couple of TextFields for To: and Subject: and a TextArea for the message body. When the user presses the Send button, it will open an SMTP connection to the server and send the e-mail using the SMTP protocol. Note that because the applet is making connections back to its codebase, it doesn't get security exceptions.

import java.applet.*;
import java.net.*;
import java.io.*;
import sun.net.smtp.*;

public class SendMail extends Applet {
       TextField to, subject;
       TextArea text;

       public void init() {
               setLayout(new BorderLayout());

               Panel p1 = new Panel();
               p1.setLayout(new BorderLayout());
               p1.add("West", new Label("To: "));
               to = new TextField();
               p1.add("Center", to);

               Panel p2 = new Panel();
               p2.setLayout(new BorderLayout());
               p2.add("West", new Label("Subject: "));
               subject = new TextField();
               p2.add("Center", subject);

               Panel headers = new Panel();
               headers.setLayout(new BorderLayout());
               headers.add("North", p1);
               headers.add("South", p2);
               add("North", headers);

               text = new TextArea(10, 30);
               add("Center", text);

               Button send = new Button("Send");
               add("South", send);

       public boolean handleEvent(Event e) {
               if (e.target instanceof Button && e.id == Event.ACTION_EVENT) {
                       try {
                               SmtpClient msg = new SmtpClient(

                               // Set the To: field

                               // Set the From: field to some address

                               // Write the body of the message
                               // according to RFC 733
                               PrintStream out = msg.startMessage();
                               out.println("Subject: " + subject.getText());
                       } catch (Exception err) {
                               System.out.println("Send Failed.");
                       return true;
               return false;
DevX Pro
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date