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 Find Transpose of a Graph Matrix
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Generate N Number of Passwords of Length M Each
Checked and Unchecked Exceptions in Java
Java Program to Find the GCD and LCM of two Numbers
How to Change the Default Port in Spring Boot
CharSequence vs. String in Java
Hướng dẫn sử dụng Java Generics
Spring Boot - Runners
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Implement Sorted Circularly Singly Linked List
Convert XML to JSON Using Jackson
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Guide to Java 8’s Collectors
Java – Combine Multiple Collections
Java Program to Implement Skew Heap
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Sort a HashMap in Java
Spring MVC Tutorial
Hướng dẫn Java Design Pattern – Command
Zipping Collections in Java
REST Web service: Basic Authentication trong Jersey 2.x
Performance Difference Between save() and saveAll() in Spring Data
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Program to Implement Circular Singly Linked List
Tiêu chuẩn coding trong Java (Coding Standards)
Guide to Mustache with Spring Boot
HandlerAdapters in Spring MVC
Easy Ways to Write a Java InputStream to an OutputStream
RegEx for matching Date Pattern in Java
Java Program to Implement Binary Tree
Jackson – Marshall String to JsonNode