/Users/lyon/j4p/src/net/mail/Smtp.java

1    package net.mail; 
2     
3    import java.io.BufferedReader; 
4    import java.io.IOException; 
5    import java.io.InputStreamReader; 
6    import java.io.PrintWriter; 
7    import java.net.InetAddress; 
8    import java.net.Socket; 
9     
10   public class Smtp implements Runnable { 
11    
12       private String recipientEmail = null; 
13       private String senderEmail = null; 
14       private String serverHostName = null; 
15       private String message = null; 
16    
17       private int smtpPort = 25; 
18    
19       private Socket emailClientSocket; 
20       private PrintWriter pw; 
21       private BufferedReader br; 
22       private InetAddress internetAddressOfSocketToTheSMTPServer; 
23       private InetAddress myLocalInternetAddress; 
24    
25    
26       Thread thread = new Thread(this); 
27    
28       public void emailLyon(String msg) { 
29           email(msg, 
30                   "localhost", 
31                   "lyon@company.mail", 
32                   "lyon@company.mail"); 
33       } 
34    
35       public void email(String msg, 
36                         String serverHostName, 
37                         String recipientEmail, 
38                         String senderEmail) { 
39           setSenderEmail(senderEmail); 
40           setRecipientEmail(recipientEmail); 
41           setMailServerHostName(serverHostName); 
42           setMessage(msg); 
43           run(); 
44       } 
45    
46       public void start() { 
47           thread.start(); 
48       } 
49    
50    
51       public void run() { 
52           try { 
53               send(); 
54           } catch (IOException e) { 
55               e.printStackTrace(); 
56           } 
57       } 
58    
59       public void send() throws IOException { 
60    
61           emailClientSocket = new Socket(serverHostName, smtpPort); 
62           try { 
63               internetAddressOfSocketToTheSMTPServer = emailClientSocket.getInetAddress(); 
64               // check permission of incomming host 
65               // to see if we should allow it access to 
66               // the smtp services. 
67               myLocalInternetAddress = InetAddress.getLocalHost(); 
68    
69               pw = new PrintWriter(emailClientSocket.getOutputStream()); 
70               br = new BufferedReader( 
71                       new InputStreamReader( 
72                               emailClientSocket.getInputStream() 
73                       ) 
74               ); 
75               sendline("HELO " + myLocalInternetAddress.toString()); 
76               sendline("MAIL FROM:" + senderEmail); 
77               sendline("RCPT TO:" + recipientEmail); 
78               sendline("DATA"); 
79               sendline(message); 
80               sendline("."); 
81    
82    
83           } catch (Exception c) { 
84               emailClientSocket.close(); 
85               System.out.println( 
86                       "Error; message send failed:\n " 
87                       + c.getMessage()); 
88           } 
89           emailClientSocket.close(); 
90       } 
91    
92       void sendline(String data) throws IOException { 
93           pw.println(data); 
94           pw.flush(); 
95           String s = br.readLine(); 
96           System.out.println("sendline in:" + s); 
97       } 
98    
99       public void setMessage(String msg) { 
100          message = msg; 
101      } 
102   
103      public void setSenderEmail(String email) { 
104          senderEmail = email; 
105      } 
106   
107      public void setRecipientEmail(String email) { 
108          recipientEmail = email; 
109      } 
110   
111      public void setMailServerHostName(String host) { 
112          serverHostName = host; 
113      } 
114   
115      public static void main(String args[]) { 
116          Smtp sm = new Smtp(); 
117          for (int i = 0; i < 1000; i++) { 
118              sm.emailLyon("This is a test!!"); 
119              System.out.println("Thread was started to send an email"); 
120          } 
121      } 
122   
123   
124  }