Send E-Mails Using the Class

Send E-Mails Using the Class

Recently, I found I needed to send e-mail from my program. Because I did not want to create any external dependencies, I decided not to use JavaMail. However, I wanted something really simple to send just plain text. The solution I found uses only the classes from standard Java distribution, making it quite limited in its capabilities. It does not support attachments, signatures, etc., but it does the job. The only thing needed to make it work is a valid and running SMTP host.

  /**   * Actually sends email using URLConnection class.   * This method is made static, so it can be called all    * by itself. It actually   * has all the info it needs passed in thru parameters.   * 

* One important piece of the information that this method * needs in order to work * is a reference to a valid SMTP host. This reference * is picked up from the system * property . That's why it is * important that this property is * either set before the program runs, or the valid * host name is entered in the code * instead of a placeholder [VALID SMTP HOST]. * * @param from The sender of the message. * @param to The adressee. Should be standard * email address, like [email protected]. * @param subject The subject of the message. * @param message The text of the message. */ public static void sendEmail(String from, String to, String subject, String message) { // valid SMTP has to be set up and running, // and system property should point to it; // let's see if we have anything set up for //this property, and if not, // set it up to our mail server String mailHost = System.getProperty(""); if (mailHost == null) { System.setProperty("", [VALID_SMTP_HOST]); } try { // open connection using internal "mailto" protocol handler URL url = new URL("mailto:" + to); URLConnection conn = url.openConnection(); conn.setDoInput(false); conn.setDoOutput(true); conn.connect(); // get writer into the stream PrintWriter out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream() ) ); // write out mail headers // From header in the form From: "alias" out.println("From: "" + from + "" <" + from + ">"); out.println("To: " + to); out.println("Subject: " + subject); out.println(); // blank line to end the list of headers // write out the message out.println(message); // close the stream to terminate the message out.close(); } catch(Exception err) { System.err.println(err); err.printStackTrace(System.err); } }

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