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:
Java – Write an InputStream to a File
Send email with JavaMail
Daemon Threads in Java
Runnable vs. Callable in Java
How to Define a Spring Boot Filter?
String Joiner trong Java 8
Java Program to Represent Graph Using Linked List
How to Round a Number to N Decimal Places in Java
Java Map With Case-Insensitive Keys
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement Sorted Vector
Format ZonedDateTime to String
Compare Two JSON Objects with Jackson
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java – InputStream to Reader
Introduction to Eclipse Collections
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Batch Processing with Spring Cloud Data Flow
An Intro to Spring Cloud Task
A Guide to Concurrent Queues in Java
Java Program to Implement Singly Linked List
Java Program to Find the Connected Components of an UnDirected Graph
Java Program to Represent Graph Using Adjacency List
Introduction to Liquibase Rollback
Debug a HttpURLConnection problem
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Write/Read cookies using HTTP and Read a file from the internet
Spring WebClient vs. RestTemplate
Java Program to Perform Uniform Binary Search
Java Program to Compute Determinant of a Matrix
Java Program to Implement Leftist Heap
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder