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 Program to Check the Connectivity of Graph Using DFS
Java Program to Perform String Matching Using String Library
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Giới thiệu thư viện Apache Commons Chain
Java Program to Find Number of Articulation points in a Graph
Giới thiệu Google Guice – Dependency injection (DI) framework
Read an Outlook MSG file
Hướng dẫn sử dụng String Format trong Java
Spring Cloud AWS – EC2
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Limiting Query Results with JPA and Spring Data JPA
Java Program to Implement LinkedBlockingDeque API
Reading an HTTP Response Body as a String in Java
The Thread.join() Method in Java
Java – Try with Resources
The “final” Keyword in Java
Java Program to Check whether Graph is Biconnected
Stack Memory and Heap Space in Java
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to Implement PrinterStateReasons API
Tính kế thừa (Inheritance) trong java
Recommended Package Structure of a Spring Boot Project
JUnit5 @RunWith
Java Program to Implement String Matching Using Vectors
Lập trình mạng với java
Apache Commons Collections SetUtils
Redirect to Different Pages after Login with Spring Security
Spring Boot - Quick Start
Spring Boot Security Auto-Configuration
Examine the internal DNS cache
Giới thiệu Google Guice – Binding
How to Set TLS Version in Apache HttpClient