Using .net Classes to Log On to Mail Server

Using .net Classes to Log On to Mail Server

How do I use the .net classes to log on to a mailserver and request, say, the number of new mailmessages? 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 mailserver. To create a mail reader, you’d have to implement theappropriate protocol (such as POP or IMAP) yourself using theSocket class. While this is do-able, it may be more work thanyou think if you insist on implementing the complete protocol.Check to see if there are public domain implementationsof these protocols available on the Net before trying to codeit yourself.

Sending e-mail, on the other hand, is simple. Among Java’snetworking classes is an SMTP client,,that allows you to do it.

Here’s a synopsis of as of JDK1.0.2:


       SmtpClient(String)               New SMTP client connected to host host.       SmtpClient()               Create an uninitialized SMTP client. 
       closeServer()               issue the QUIT command to the server and close the connection.       from(String)               set the From line of the email       startMessage()               start sending the body of the email       to(String)               set the To line of the email message               
In fact, Sun’s HotJava browser uses this package to implement thesendtooperation for sending mail from the browser. In general,the sun.* packages provide the underlying implementationfor the public Java APIs, and are themselves available to Javaprogrammers. However, because they are not part of the publishedJava 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: anda 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 usingthe SMTP protocol. Note that because the applet is making connectionsback to its codebase, it doesn’t get security exceptions.

importjava.awt.*;import java.applet.*;import*;import*;import*;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 ( instanceof Button && == Event.ACTION_EVENT) {                       try {                               SmtpClient msg = new SmtpClient(                                               getCodeBase().getHost());                               // Set the To: field                     ;                               // Set the From: field to some address                               msg.from(“[email protected]”);                               // Write the body of the message                               // according to RFC 733                               PrintStream out = msg.startMessage();                               out.println(“Subject: ” + subject.getText());                               out.println(text.getText());                               msg.closeServer();                       } catch (Exception err) {                               System.out.println(“Send Failed.”);                               err.printStackTrace();                       }                       return true;               }               return false;       }} 

See also  The Art of AI-Generated Meeting Minutes

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist