Send an email using the SMTP protocol

SMTP is the protocol used to send an email.

import java.net.*;
import java.io.*;

public class SendVietMXMail {
 public static void main(String s[]) {
   //
   //  Send fake mail from Elvis Presley
   //
   //  SendVietMXMail [mail server] [recipient address]
   //               mail server can be hostname or IP address
   //
   //   ex.  SendVietMXMail mail.company.com myFriend@somewhere.vn
   //
   SendVietMXMail t = new SendVietMXMail();
   t.sendMail(s[0], s[1]);
 }

 public void sendMail(String mailServer, String recipient) {
   try {
      Socket s = new Socket(mailServer, 25);
      BufferedReader in = new BufferedReader
          (new InputStreamReader(s.getInputStream(), "8859_1"));
      BufferedWriter out = new BufferedWriter
          (new OutputStreamWriter(s.getOutputStream(), "8859_1"));

      send(in, out, "HELO theWorld");
      // warning : some mail server validate the sender address
      //           in the MAIL FROm command, put your real address here
      send(in, out, "MAIL FROM: <viet.mai@maixuanviet.com>");
      send(in, out, "RCPT TO: " + recipient);
      send(in, out, "DATA");
      send(out, "Subject: In the ghetto");
      send(out, "From: Viet Mai <viet.mai@maixuanviet.com>");
      send (out, "\n");
      // message body
      send(out, "I'm alive. Help me!");
      send(out, "\n.\n");
      send(in, out, "QUIT");
      s.close();
   }
   catch (Exception e) {
      e.printStackTrace();
   }
 }

 public void send(BufferedReader in, BufferedWriter out, String s) {
   try {
      out.write(s + "\n");
      out.flush();
      System.out.println(s);
      s = in.readLine();
      System.out.println(s);
   }
   catch (Exception e) {
      e.printStackTrace();
   }
 }

 public void send(BufferedWriter out, String s) {
   try {
      out.write(s + "\n");
      out.flush();
      System.out.println(s);
   }
   catch (Exception e) {
      e.printStackTrace();
   }
 }
}

Done! Happy Coding!

Related posts:

Spring Security 5 – OAuth2 Login
Java Program to Implement Park-Miller Random Number Generation Algorithm
Java Program to Implement Stack
Java Program to Find Nearest Neighbor Using Linear Search
Configure a Spring Boot Web Application
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Find a Good Feedback Vertex Set
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Describe the Representation of Graph using Adjacency List
Getting a File’s Mime Type in Java
Java Program to Implement the One Time Pad Algorithm
Java InputStream to String
Server-Sent Events in Spring
Introduction to Spring Security Expressions
Spring Boot - Interceptor
Java Streams vs Vavr Streams
Spring 5 Functional Bean Registration
Java Program to Implement Uniform-Cost Search
Introduction to Using FreeMarker in Spring MVC
Create a Custom Exception in Java
Using a Mutex Object in Java
Guide to Java 8’s Collectors
Java Program to Implement Hopcroft Algorithm
Uploading MultipartFile with Spring RestTemplate
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Ép kiểu trong Java (Type casting)
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Java Program to subtract two large numbers using Linked Lists
Hướng dẫn Java Design Pattern – Memento
Anonymous Classes in Java
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu