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:
Jackson vs Gson
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Comparing Dates in Java
Jackson Exceptions – Problems and Solutions
Java Program to Implement Singly Linked List
Ép kiểu trong Java (Type casting)
Lập trình hướng đối tượng (OOPs) trong java
Java Program to Implement ConcurrentHashMap API
Java Program to Perform Uniform Binary Search
Ignore Null Fields with Jackson
Login For a Spring Web App – Error Handling and Localization
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Giới thiệu luồng vào ra (I/O) trong Java
Java – Write a Reader to File
Java Program to Generate All Possible Combinations of a Given List of Numbers
Spring Boot - Enabling Swagger2
A Guide to JUnit 5 Extensions
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Concrete Class in Java
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Implement Bucket Sort
Guide to BufferedReader
A Guide to BitSet in Java
Java Program to Implement PrinterStateReasons API
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Extract links from an HTML page
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Implement AttributeList API
Java – File to Reader
Multi Dimensional ArrayList in Java
LIKE Queries in Spring JPA Repositories