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:
How to Count Duplicate Elements in Arraylist
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Streams vs Vavr Streams
Java Program to Implement Graph Coloring Algorithm
Spring Boot - Google OAuth2 Sign-In
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Immutable Objects in Java
What is Thread-Safety and How to Achieve it?
Server-Sent Events in Spring
Java Program to Implement Find all Back Edges in a Graph
The DAO with JPA and Spring
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Implement Shunting Yard Algorithm
Migrating from JUnit 4 to JUnit 5
Java Program to implement Bit Matrix
Getting a File’s Mime Type in Java
Converting Between an Array and a Set in Java
Spring Data Reactive Repositories with MongoDB
Java Program to Evaluate an Expression using Stacks
Receive email by java client
Comparing Arrays in Java
Java Program for Topological Sorting in Graphs
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Ignore Null Fields with Jackson
Java Program to Check Multiplicability of Two Matrices
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement Extended Euclid Algorithm
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Implement Sorted Vector
Guide to DelayQueue
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Find Path Between Two Nodes in a Graph