Source Code Cross Referenced for EasySendMail.java in  » IDE » tIDE » snow » mail » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE » tIDE » snow.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package snow.mail;
002:
003:        import snow.utils.storage.FileUtils;
004:        import java.net.*;
005:        import java.io.*;
006:        import java.util.*;
007:        import java.text.*;
008:
009:        /** A very easy Quick SMTP send message mechanism.
010:         ( this class don't resolve dns information, it directly send messages to the given host,
011:         do please use it only if you explicitely know the server )
012:         */
013:        public class EasySendMail {
014:            static final boolean debug = true;
015:
016:            static final String EOL = "\r\n"; // network end of line
017:
018:            protected BufferedReader reply = null;
019:            protected PrintStream send = null;
020:            protected Socket sock = null;
021:
022:            protected List<String> logLines = new Vector<String>();
023:
024:            /**
025:             *   @param hostid The host to connect to (mail.gmx.ch).
026:             *   @param port is normally 25
027:             */
028:            public EasySendMail(String hostid, int port) throws Exception {
029:                sock = new Socket(hostid, port);
030:                sock.setSoTimeout(1000 * 60 * 1); // 1 min timeout
031:
032:                reply = new BufferedReader(new InputStreamReader(sock
033:                        .getInputStream()));
034:                send = new PrintStream(sock.getOutputStream());
035:
036:                // greetings
037:                //
038:                String rstr = readLine();
039:                if (!rstr.startsWith("220")) {
040:                    throw new Exception("I want 220 I get: " + rstr);
041:                }
042:
043:                while (rstr.indexOf('-') == 3) // ### 4?
044:                {
045:                    rstr = readLine();
046:                    if (!rstr.startsWith("220")) {
047:                        throw new Exception("I want 220 I get: " + rstr);
048:                    }
049:                }
050:            }
051:
052:            /** Send the message content to the given server.
053:                [Sept2005] may throw a GreyListException with the time to wait until next attempt.
054:             */
055:            public void sendmsg(String from_address, String to_address,
056:                    @edu.umd.cs.findbugs.annotations.CheckForNull
057:                    String fileName, // null if none
058:                    String content, String domainName) throws Exception,
059:                    Exception {
060:
061:                sendLine("HELO " + domainName, true);
062:                String rstr = readLine();
063:                if (!rstr.startsWith("250"))
064:                    throw new Exception("I say HELO " + domainName
065:                            + ", server says " + rstr);
066:
067:                sendLine("MAIL FROM:<" + from_address + ">", true);
068:                rstr = readLine();
069:                if (!rstr.startsWith("250"))
070:                    throw new Exception("I say Mail From:<" + from_address
071:                            + ">" + " server says " + rstr);
072:
073:                sendLine("RCPT TO:<" + to_address + ">", true);
074:                rstr = readLine();
075:                if (rstr.startsWith("450")) {
076:                    throw new Exception(rstr); // Greylist !!
077:                }
078:
079:                if (!rstr.startsWith("250"))
080:                    throw new Exception("I say RCPT TO:<" + to_address + ">"
081:                            + " server says " + rstr);
082:
083:                sendLine("DATA", true);
084:                rstr = readLine();
085:                if (!rstr.startsWith("354"))
086:                    throw new Exception("I say DATA server says " + rstr);
087:
088:                // send
089:                //
090:
091:                if (fileName != null) {
092:                    try {
093:                        sendFileContent(send, fileName);
094:                    } catch (Exception e) {
095:                        send.write("### FILE READ ERROR ###".getBytes());
096:                        throw e;
097:                    }
098:                }
099:
100:                if (content != null) {
101:                    send.write(content.getBytes());
102:                }
103:
104:                // all data has been sent - wrap it up
105:                sendLine("", true);
106:                sendLine(".", true);
107:
108:                rstr = readLine();
109:                if (!rstr.startsWith("250")) {
110:                    throw new Exception("I say .(end of message) server says "
111:                            + rstr);
112:                }
113:
114:                close();
115:            }
116:
117:            private void close() {
118:                try {
119:                    sendLine("QUIT", true);
120:                    sock.close();
121:                } catch (Exception ioe) {
122:                }
123:            }
124:
125:            /** all the traffic
126:             */
127:            public List<String> getTrafficLogLines() {
128:                return logLines;
129:            }
130:
131:            private void sendLine(String line, boolean doLog) throws Exception {
132:                if (doLog) {
133:                    logLines.add("S> " + line);
134:                    if (debug)
135:                        System.out.println("EasySendMail S> " + line);
136:                }
137:                send.print(line);
138:                send.print(EOL);
139:                send.flush();
140:            }
141:
142:            private String readLine() throws Exception {
143:                String line = reply.readLine();
144:                logLines.add("R> " + line);
145:                if (debug)
146:                    System.out.println("EasySendMail R> " + line);
147:                return line;
148:            }
149:
150:            public static void main(String[] a) {
151:                testGMX();
152:                //testLocalhost();
153:            }
154:
155:            public static void testGMX() {
156:                try {
157:                    InetAddress local = null;
158:                    try {
159:                        local = InetAddress.getLocalHost();
160:                    } catch (UnknownHostException ioe) {
161:                        System.err
162:                                .println("No local IP address found - is your network up?");
163:                        throw ioe;
164:                    }
165:                    System.out.println("LOCAL: " + local.getHostAddress()
166:                            + ", " + local.getHostName());
167:
168:                    EasySendMail con = new EasySendMail("mx0.gmx.de", 25);
169:                    con
170:                            .sendmsg(
171:                                    "akimo@www.snowraver.org",
172:                                    "akimo@gmx.ch",
173:                                    null,
174:                                    "subject: you should log out\n"
175:                                            + "from: akimo@www.snowraver.org\n\n"
176:                                            + "You work time of 8:32 hours today is reached\r\nlog out now.",
177:                                    //local.getHostAddress()
178:                                    "www.snowraver.org");
179:                    //con.close();
180:                } catch (Exception e) {
181:                    e.printStackTrace();
182:                }
183:            }
184:
185:            /** get the content of the file and send it.
186:             */
187:            private void sendFileContent(OutputStream os, String fileName)
188:                    throws Exception {
189:                InputStream fis = null;
190:
191:                try {
192:                    String strCurrentLine;
193:                    // the file is chiphered. NonLocal received a file and stored it here
194:                    fis = new FileInputStream(fileName);
195:                    BufferedReader lineReader = new BufferedReader(
196:                            new InputStreamReader(fis));
197:
198:                    // send the file one line at a time
199:                    String line = lineReader.readLine();
200:                    while (line != null) {
201:                        os.write((line + "\r\n").getBytes());
202:
203:                        line = lineReader.readLine();
204:                    }
205:                } catch (Exception e) {
206:                    throw e;
207:                } finally {
208:                    FileUtils.closeIgnoringExceptions(fis);
209:                }
210:            }
211:
212:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.