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

1    package net.mail.swing; 
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     String recipientEmail = null; 
13     String senderEmail = null; 
14     String serverHostName = null; 
15     String message = null; 
16    
17     int smtpPort = 25; 
18    
19     Socket socket; 
20     PrintWriter pw; 
21     BufferedReader br; 
22     InetAddress ia; 
23     InetAddress lina; 
24    
25    
26     Thread thread = new Thread(this); 
27    
28     public void emailLyon(String msg) { 
29       email(msg, 
30             "192.168.1.95", 
31             "lyon@docjava.com", 
32             "lyon@docjava.com"); 
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       thread.start(); 
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       socket = new Socket(serverHostName, smtpPort); 
61       try { 
62         ia = socket.getInetAddress(); 
63         lina = ia.getLocalHost(); 
64    
65         pw = new PrintWriter(socket.getOutputStream()); 
66         br = new BufferedReader( 
67             new InputStreamReader( 
68                 socket.getInputStream() 
69             ) 
70         ); 
71         sendline("HELO " + lina.toString()); 
72         sendline("MAIL FROM:" + senderEmail); 
73         sendline("RCPT TO:" + recipientEmail); 
74         sendline("DATA"); 
75         sendline(message); 
76         sendline("."); 
77    
78    
79       } catch (Exception c) { 
80         socket.close(); 
81         System.out.println( 
82             "Error; message send failed:\n " 
83             + c.getMessage()); 
84       } 
85       socket.close(); 
86     } 
87    
88     void sendline(String data) throws IOException { 
89       pw.println(data); 
90       pw.flush(); 
91       String s = br.readLine(); 
92       System.out.println("sendline in:" + s); 
93     } 
94    
95     public void setMessage(String msg) { 
96       message = msg; 
97     } 
98    
99     public void setSenderEmail(String email) { 
100      senderEmail = email; 
101    } 
102   
103    public void setRecipientEmail(String email) { 
104      recipientEmail = email; 
105    } 
106   
107    public void setMailServerHostName(String host) { 
108      serverHostName = host; 
109    } 
110   
111    public static void main(String args[]) { 
112      Smtp sm = new Smtp(); 
113      sm.emailLyon("This is a test!!"); 
114      System.out.println("Thread was started to send an email"); 
115    } 
116   
117   
118  }