Using .net Classes to Log On to Mail Server

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing