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 Boot Tutorial – Bootstrap a Simple Application
Java Program to Implement Hash Tables with Linear Probing
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement the Vigenere Cypher
Quick Guide to java.lang.System
Java Program to Perform String Matching Using String Library
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Implement Warshall Algorithm
A Guide to HashSet in Java
Java Program to Implement Dijkstra’s Algorithm using Set
How to Replace Many if Statements in Java
Java Program to Find Transitive Closure of a Graph
Reversing a Linked List in Java
Java Program to Implement Stack using Linked List
Documenting a Spring REST API Using OpenAPI 3.0
Getting Started with Forms in Spring MVC
Java Program to Perform Finite State Automaton based Search
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java Program to Generate a Random Subset by Coin Flipping
Base64 encoding và decoding trong Java 8
Cơ chế Upcasting và Downcasting trong java
A Quick Guide to Using Keycloak with Spring Boot
Spring Boot - Introduction
Java Collections Interview Questions
Weak References in Java
Java – Delete a File
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Implement Repeated Squaring Algorithm
Zipping Collections in Java
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Implement Leftist Heap
Assertions in JUnit 4 and JUnit 5