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:
@Order in Spring
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement Range Tree
Understanding Memory Leaks in Java
Most commonly used String methods in Java
The DAO with JPA and Spring
Finding the Differences Between Two Lists in Java
Apache Camel with Spring Boot
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
A Guide to TreeMap in Java
Stack Memory and Heap Space in Java
OAuth2 Remember Me with Refresh Token
Creating a Web Application with Spring 5
New Features in Java 10
Programmatic Transaction Management in Spring
Hướng dẫn Java Design Pattern – Intercepting Filter
Spring Boot Annotations
Spring Security – Reset Your Password
Base64 encoding và decoding trong Java 8
Guide to the Synchronized Keyword in Java
Java Program to Implement Uniform-Cost Search
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Consuming RESTful Web Services
Working With Maps Using Streams
Java Program to Implement Borwein Algorithm
So sánh Array và ArrayList trong Java
Removing all Nulls from a List in Java
Hướng dẫn Java Design Pattern – Iterator
Using the Not Operator in If Conditions in Java
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Spring Data JPA and Null Parameters