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:
Các kiểu dữ liệu trong java
Guide to @ConfigurationProperties in Spring Boot
Java Program to implement Priority Queue
Java Program to Compute Determinant of a Matrix
Hashtable trong java
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Thao tác với tập tin và thư mục trong Java
Test a REST API with Java
Java Program to Implement PriorityQueue API
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Using a Mutex Object in Java
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Implement IdentityHashMap API
Java Program to Perform Cryptography Using Transposition Technique
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Find Inverse of a Matrix
Java Program to Implement RoleUnresolvedList API
Converting a List to String in Java
Java Program to Implement Fermat Factorization Algorithm
Java Program to Find kth Largest Element in a Sequence
Java Program to Perform Arithmetic Operations on Numbers of Size
HashMap trong Java hoạt động như thế nào?
Java Program to Implement HashSet API
Logging a Reactive Sequence
Concurrent Test Execution in Spring 5
ExecutorService – Waiting for Threads to Finish
Java Program to Implement Disjoint Sets
New Features in Java 12
Adding Shutdown Hooks for JVM Applications
Hướng dẫn Java Design Pattern – Null Object
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Represent Graph Using Adjacency List