Using .net Classes to Log On to Mail Server

Question:
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.

Answer:
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, calledsun.net.smtp.SmtpClient,that allows you to do it.

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

Constructors:

       SmtpClient(String)               New SMTP client connected to host host.       SmtpClient()               Create an uninitialized SMTP client. 
Methods:
       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 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(                                               getCodeBase().getHost());                               // Set the To: field                               msg.to(to.getText());                               // 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;       }} 

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may