esposimi

EmailSender for Netbeans

Nov 6th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. package emailsender;
  2.  
  3. /**
  4.  *
  5.  * @author Michael Esposito
  6.  */
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. public class EmailSender {
  14. private static PrintWriter out;
  15.     private static BufferedReader in;
  16.  
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.  
  22.     public static void main(String[] args) throws Exception{
  23.  
  24.        //Establish a TCP connection with the mail server.
  25.         Socket echoSocket = null;
  26.        
  27.         //Apple's SMTP server works perfectly, you can change it if you find a better one.
  28.         String hostname = "smtp.mail.me.com";
  29.  
  30.  
  31.         try {
  32.             echoSocket = new Socket(hostname, 587); //587 is the port for SMTP
  33.             out = new PrintWriter(echoSocket.getOutputStream(), true);
  34.             in = new BufferedReader(new InputStreamReader(
  35.                                         echoSocket.getInputStream()));
  36.         } catch (UnknownHostException e) {
  37.             System.err.println("Don't know about host: " + hostname);
  38.             System.exit(1);
  39.         } catch (IOException e) {
  40.             System.err.println("Couldn't get I/O for "
  41.                                + "the connection to: smtp.mail.me.com");
  42.             System.exit(1);
  43.         }
  44.  
  45.  
  46. BufferedReader stdIn = new BufferedReader(
  47.                                    new InputStreamReader(System.in));
  48.  
  49.         String response = null;
  50.         response = in.readLine();
  51.  
  52.         System.out.println (response);
  53.               if (!response.startsWith ("220")) {
  54.                   System.err.println("220 reply not received from server.");
  55.                   System.exit(1);
  56.                 }
  57.        //get a reference to the socket's output stream.
  58.        OutputStream os = echoSocket.getOutputStream();
  59.  
  60.  
  61.        //send HELO command and get server response.
  62.             String helocommand = "HELO alice\r\n";
  63.             System.out.print (helocommand);
  64.             os.write (helocommand.getBytes ("US-ASCII"));
  65.             response = in.readLine();
  66.             System.out.println (response);
  67.             if (!response.startsWith("250")) {
  68.                         System.err.println("250 reply not received from server.");
  69.                         System.exit(1);
  70.             }
  71.  
  72.  
  73.         //Send MAIL FROM command.
  74.             String mailfromcommand = "MAIL FROM: [email protected]\r\n"; //The e-mail address you would like to spoof.
  75.             System.out.print (mailfromcommand);
  76.             os.write (mailfromcommand.getBytes ("US-ASCII"));
  77.             response = in.readLine();
  78.             System.out.println (response);
  79.             if (!response.startsWith("250")) {
  80.                         System.err.println("250 reply not received from server.");
  81.                         System.out.print("QUIT\r\n");
  82.                         System.exit(1);
  83.             }
  84.         // Send RCPT TO command.
  85.             String rcpttocommand = "RCPT TO: [email protected]\r\n"; //The e-mail address that will be receiving the message.
  86.             System.out.print (rcpttocommand);
  87.             os.write (rcpttocommand.getBytes ("US-ASCII"));
  88.             response = in.readLine();
  89.             System.out.println (response);
  90.             if (!response.startsWith("250")) {
  91.                         System.err.println("250 reply not received from server.");
  92.                         System.out.print("\r\nQUIT");
  93.                         System.exit(1);
  94.             }
  95.         // Send DATA command.
  96.             String datacommand = "DATA\r\n";
  97.             System.out.print (datacommand);
  98.             os.write (datacommand.getBytes ("US-ASCII"));
  99.             response = in.readLine();
  100.             System.out.println (response);
  101.             if (!response.startsWith("354")) {
  102.                         System.err.println("354 reply not received from server.");
  103.                         System.out.print("QUIT\r\n");
  104.                         System.exit(1);
  105.             }
  106.            
  107.         //Send message data.
  108.              String message = "SUBJECT: Aw yeah!\r\nFROM: [email protected]\r\nTo: Michael\r\nTEXT GOES HERE\r\n";
  109.        
  110.         //The "FROM" address must be the same as "RCPT TO" above, or the message may be blocked by spam filters.
  111.         //"SUBJECT" and "To: " can be anything
  112.         //Replace "TEXT GOES HERE" with the message. The message must be between the \r\n characters for the SMTP server to accept it.
  113.        
  114.              System.out.print (message);
  115.              os.write (message.getBytes ("US-ASCII"));
  116.              
  117.         //End with line with a single period.
  118.             String ender = "\r\n.\r\n";
  119.             System.out.print(ender);
  120.             os.write (ender.getBytes ("US-ASCII"));
  121.             response = in.readLine();
  122.             System.out.println (response);
  123.            if (!response.startsWith("250")) {
  124.                        System.err.println("250 reply not received from server.");
  125.                        System.out.print("QUIT\r\n");
  126.                        System.exit(1);
  127.             }            
  128.         //Send QUIT command
  129.             String quit = "QUIT\r\n";
  130.             System.out.print (quit);
  131.             os.write (quit.getBytes ("US-ASCII"));
  132.             response = in.readLine();
  133.             System.out.println (response);
  134.             System.exit(1);
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment