Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package emailsender;
- /**
- *
- * @author Michael Esposito
- */
- import java.io.*;
- import java.net.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class EmailSender {
- private static PrintWriter out;
- private static BufferedReader in;
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws Exception{
- //Establish a TCP connection with the mail server.
- Socket echoSocket = null;
- //Apple's SMTP server works perfectly, you can change it if you find a better one.
- String hostname = "smtp.mail.me.com";
- try {
- echoSocket = new Socket(hostname, 587); //587 is the port for SMTP
- out = new PrintWriter(echoSocket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader(
- echoSocket.getInputStream()));
- } catch (UnknownHostException e) {
- System.err.println("Don't know about host: " + hostname);
- System.exit(1);
- } catch (IOException e) {
- System.err.println("Couldn't get I/O for "
- + "the connection to: smtp.mail.me.com");
- System.exit(1);
- }
- BufferedReader stdIn = new BufferedReader(
- new InputStreamReader(System.in));
- String response = null;
- response = in.readLine();
- System.out.println (response);
- if (!response.startsWith ("220")) {
- System.err.println("220 reply not received from server.");
- System.exit(1);
- }
- //get a reference to the socket's output stream.
- OutputStream os = echoSocket.getOutputStream();
- //send HELO command and get server response.
- String helocommand = "HELO alice\r\n";
- System.out.print (helocommand);
- os.write (helocommand.getBytes ("US-ASCII"));
- response = in.readLine();
- System.out.println (response);
- if (!response.startsWith("250")) {
- System.err.println("250 reply not received from server.");
- System.exit(1);
- }
- //Send MAIL FROM command.
- String mailfromcommand = "MAIL FROM: [email protected]\r\n"; //The e-mail address you would like to spoof.
- System.out.print (mailfromcommand);
- os.write (mailfromcommand.getBytes ("US-ASCII"));
- response = in.readLine();
- System.out.println (response);
- if (!response.startsWith("250")) {
- System.err.println("250 reply not received from server.");
- System.out.print("QUIT\r\n");
- System.exit(1);
- }
- // Send RCPT TO command.
- String rcpttocommand = "RCPT TO: [email protected]\r\n"; //The e-mail address that will be receiving the message.
- System.out.print (rcpttocommand);
- os.write (rcpttocommand.getBytes ("US-ASCII"));
- response = in.readLine();
- System.out.println (response);
- if (!response.startsWith("250")) {
- System.err.println("250 reply not received from server.");
- System.out.print("\r\nQUIT");
- System.exit(1);
- }
- // Send DATA command.
- String datacommand = "DATA\r\n";
- System.out.print (datacommand);
- os.write (datacommand.getBytes ("US-ASCII"));
- response = in.readLine();
- System.out.println (response);
- if (!response.startsWith("354")) {
- System.err.println("354 reply not received from server.");
- System.out.print("QUIT\r\n");
- System.exit(1);
- }
- //Send message data.
- String message = "SUBJECT: Aw yeah!\r\nFROM: [email protected]\r\nTo: Michael\r\nTEXT GOES HERE\r\n";
- //The "FROM" address must be the same as "RCPT TO" above, or the message may be blocked by spam filters.
- //"SUBJECT" and "To: " can be anything
- //Replace "TEXT GOES HERE" with the message. The message must be between the \r\n characters for the SMTP server to accept it.
- System.out.print (message);
- os.write (message.getBytes ("US-ASCII"));
- //End with line with a single period.
- String ender = "\r\n.\r\n";
- System.out.print(ender);
- os.write (ender.getBytes ("US-ASCII"));
- response = in.readLine();
- System.out.println (response);
- if (!response.startsWith("250")) {
- System.err.println("250 reply not received from server.");
- System.out.print("QUIT\r\n");
- System.exit(1);
- }
- //Send QUIT command
- String quit = "QUIT\r\n";
- System.out.print (quit);
- os.write (quit.getBytes ("US-ASCII"));
- response = in.readLine();
- System.out.println (response);
- System.exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment