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:

REST Web service: Upload và Download file với Jersey 2.x
Java – Reader to String
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Hướng dẫn Java Design Pattern – Singleton
Java – Write an InputStream to a File
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Java Program to Perform Right Rotation on a Binary Search Tree
Uploading MultipartFile with Spring RestTemplate
Java CyclicBarrier vs CountDownLatch
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Custom Error Pages with Spring MVC
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Implement Suffix Array
Java Program to Perform Searching Based on Locality of Reference
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Solve a Matching Problem for a Given Specific Case
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
HashMap trong Java hoạt động như thế nào?
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Stream Filter with Lambda Expression
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Introduction to Spring Data REST
Guide to BufferedReader
@Lookup Annotation in Spring
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Implement Graph Structured Stack
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Implement Bloom Filter
Java Program to Implement Gauss Jordan Elimination
Java Program to Print only Odd Numbered Levels of a Tree
Converting a Stack Trace to a String in Java