/Users/lyon/j4p/src/net/server/sendmail/SendMailApplet.java

1    package net.server.sendmail; 
2     
3    import java.io.BufferedReader; 
4    import java.io.IOException; 
5    import java.io.InputStreamReader; 
6    import java.io.PrintStream; 
7    import java.net.InetAddress; 
8    import java.net.Socket; 
9     
10    
11   public class SendMailApplet implements Runnable { 
12    
13       String m_sRecipientEmail = null;    // The recipient email address 
14       String m_sSenderEmail = null;       // The sender email address 
15       String m_sServerHostName = null;    // The mail server 
16       String m_sMessage = null;           // The message text that gets mailed 
17    
18       boolean m_bStandalone = false; 
19       int smtpPort = 25;                  // Port 25 is the smtp mail port 
20    
21       Socket socket; 
22       PrintStream ps; 
23       BufferedReader dis; 
24       InetAddress rina; 
25       InetAddress lina; 
26    
27    
28       Thread thread = new Thread(this); 
29    
30       public void emailLyon(String msg) { 
31           email(msg, 
32                   "192.168.1.95", 
33                   "lyon@docjava.com", 
34                   "lyon@docjava.com"); 
35       } 
36    
37       public void run() { 
38           sendNoException(); 
39           System.out.println("email was sent!"); 
40       } 
41    
42       public void email(String msg, 
43                         String m_sServerHostName, 
44                         String m_sRecipientEmail, 
45                         String m_sSenderEmail) { 
46           setSenderEmail(m_sSenderEmail); 
47           setRecipientEmail(m_sRecipientEmail); 
48           setMailServerHostName(m_sServerHostName); 
49           setMessage(msg); 
50    
51    
52           thread.start(); 
53       } 
54    
55    
56       public void sendNoException() { 
57           try { 
58               send(); 
59           } catch (IOException e) { 
60               e.printStackTrace(); 
61           } 
62       } 
63    
64       /************************************************************** 
65        * Send an e-mail message. 
66        **************************************************************/ 
67       public void send() throws IOException { 
68    
69           // Open connection to SMTP server 
70           socket = new Socket(m_sServerHostName, smtpPort); 
71    
72           // Send the form contents as a message 
73           try { 
74    
75               rina = socket.getInetAddress(); 
76               lina = rina.getLocalHost(); 
77    
78               ps = new PrintStream(socket.getOutputStream()); 
79               dis = new BufferedReader( 
80                       new InputStreamReader((socket.getInputStream()))); 
81    
82               // Send message 
83               sendline("HELO " + lina.toString()); 
84               sendline("MAIL FROM:" + m_sSenderEmail); 
85               sendline("RCPT TO:" + m_sRecipientEmail); 
86               sendline("DATA"); 
87               sendline(m_sMessage); 
88               sendline("."); 
89    
90    
91           } catch (Exception ex) { 
92               socket.close(); 
93               //throw ex; 
94               System.out.println("Error; message send failed:\n " + ex.getMessage()); 
95           } 
96    
97           // Close connection 
98           socket.close(); 
99    
100      }//end method 
101   
102   
103      /************************************************************** 
104       * Send a line of data to the server, and retrieve the handshake 
105       **************************************************************/ 
106      void sendline(String data) throws IOException { 
107   
108          //System.out.println("sendline out:" + data); 
109   
110          ps.println(data); 
111          ps.flush(); 
112   
113          String s = dis.readLine(); 
114          System.out.println("sendline in:" + s); 
115      }//end method 
116   
117   
118   
119      /************************************************************** 
120       *  Mutator methods used to set the email properties 
121       *  prior to sending 
122       **************************************************************/ 
123   
124      /************************************************************** 
125       * Sets the message 
126       **************************************************************/ 
127      public void setMessage(String msg) { 
128          this.m_sMessage = msg; 
129      } 
130   
131      /************************************************************** 
132       * Sets the Sender's email address 
133       **************************************************************/ 
134      public void setSenderEmail(String email) { 
135          this.m_sSenderEmail = email; 
136      } 
137   
138      /************************************************************** 
139       * Sets the Recipient's email address 
140       **************************************************************/ 
141      public void setRecipientEmail(String email) { 
142          this.m_sRecipientEmail = email; 
143      } 
144   
145      public static void main(String args[]) { 
146          SendMailApplet sma = new SendMailApplet(); 
147          sma.emailLyon("This is a test!!"); 
148          System.out.println("Thread was started to send an email"); 
149      } 
150   
151      /************************************************************** 
152       * Sets the mail server host name 
153       **************************************************************/ 
154      public void setMailServerHostName(String host) { 
155          this.m_sServerHostName = host; 
156      } 
157   
158  }//end class 
159   
160   
161