home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09961.iso / java / mojo1-2e.exe / MOJODISK / DATA.4 / plugins / smtp.def < prev    next >
Encoding:
Text File  |  1996-07-02  |  13.1 KB  |  469 lines

  1. DEF_COMPONENTNAME
  2. Smtp
  3. DEF_SUPERCLASS
  4. Object
  5. DEF_SUPERCOMPONENT
  6.  
  7. DEF_PACKAGE
  8. plugins
  9. internet
  10. DEF_ENDLIST
  11. DEF_SUBCOMPONENTLIST
  12. DEF_ENDLIST
  13. DEF_SUBCOMPONENTCLASSLIST
  14. DEF_ENDLIST
  15. DEF_BITMAP
  16.  
  17. DEF_THUMBNAIL_UP
  18.  
  19. DEF_THUMBNAIL_DOWN
  20.  
  21. DEF_IMPORTS
  22. java.util.*
  23. java.net.*
  24. DEF_ENDLIST
  25. DEF_REQUIRES
  26. DEF_ENDLIST
  27. DEF_IMPLEMENTS
  28. DEF_ENDLIST
  29. DEF_DECLARATION
  30. // A class that lays the groundwork for a SMTP component.
  31. //  Variable Declarations
  32.     static final int DEFAULT_PORT = 25;
  33.     static final boolean DEBUG = false;
  34.  
  35.     protected DataInputStream reply = null;
  36.     protected PrintStream send = null;
  37.     protected Socket sock = null;
  38. DEF_ENDLIST
  39. DEF_METHOD
  40.     public static void main( String[] args) {
  41.     }
  42. DEF_ENDLIST
  43. DEF_METHOD
  44.     public Smtp( String hostid) throws UnknownHostException, IOException {
  45.         this(hostid, DEFAULT_PORT);
  46.     }
  47. DEF_ENDLIST
  48. DEF_METHOD
  49.     public Smtp( String hostid, int port) throws UnknownHostException, IOException {
  50.         sock = new Socket( hostid, port );
  51.         reply = new DataInputStream( sock.getInputStream() );
  52.         send = new PrintStream( sock.getOutputStream() );
  53.         String rstr = reply.readLine();
  54.         if (DEBUG == true) System.out.println(rstr);
  55.         if (!rstr.startsWith("220")) throw new ProtocolException(rstr);
  56.         while (rstr.indexOf('-') == 3) {
  57.             rstr = reply.readLine();
  58.             if (DEBUG) System.out.println(rstr);
  59.             if (!rstr.startsWith("220")) throw new ProtocolException(rstr);
  60.         }
  61.     }
  62. DEF_ENDLIST
  63. DEF_METHOD
  64.     public Smtp( String hostid, int port, String to_address, String from_address, String message ) throws UnknownHostException, IOException {
  65.     }
  66. DEF_ENDLIST
  67. DEF_METHOD
  68.     public Smtp( InetAddress address ) throws IOException {
  69.         this(address, DEFAULT_PORT);
  70.     }
  71. DEF_ENDLIST
  72. DEF_METHOD
  73.     public Smtp( InetAddress address, int port ) throws IOException {
  74.         sock = new Socket( address, port );
  75.         reply = new DataInputStream( sock.getInputStream() );
  76.         send = new PrintStream( sock.getOutputStream() );
  77.         String rstr = reply.readLine();
  78.         if (DEBUG) System.out.println(rstr);
  79.         if (!rstr.startsWith("220")) throw new ProtocolException(rstr);
  80.         while (rstr.indexOf('-') == 3) {
  81.             rstr = reply.readLine();
  82.             if (DEBUG) System.out.println(rstr);
  83.             if (!rstr.startsWith("220")) throw new ProtocolException(rstr);
  84.         }
  85.     }
  86. DEF_ENDLIST
  87. DEF_METHOD
  88.     public void sendmsg( String from_address, String to_address, String subject, String message ) throws IOException, ProtocolException {
  89.  
  90.         String rstr;
  91.         String sstr;
  92.  
  93.         InetAddress local = null;
  94.         local = local.getLocalHost();
  95.         String host = local.getHostName();
  96.         send.println("HELO " + host);
  97. //        send.println("HELO smtp");
  98.         if (DEBUG) System.out.println("HELO " + host);
  99.         rstr = reply.readLine();
  100.         if (DEBUG) System.out.println(rstr);
  101.         if (!rstr.startsWith("250")) throw new ProtocolException(rstr);
  102.         sstr = "MAIL FROM: " + from_address ;
  103.         send.println(sstr);
  104.         if (DEBUG) System.out.println(sstr);
  105.         rstr = reply.readLine();
  106.         if (DEBUG) System.out.println(rstr);
  107.         if (!rstr.startsWith("250")) throw new ProtocolException(rstr);
  108.         sstr = "RCPT TO: " + to_address;
  109.         send.println(sstr);
  110.         if (DEBUG) System.out.println(sstr);
  111.         rstr = reply.readLine();
  112.         if (DEBUG) System.out.println(rstr);
  113.         if (!rstr.startsWith("250")) throw new ProtocolException(rstr);
  114.         send.println("DATA");
  115.         if (DEBUG) System.out.println("DATA");
  116.         rstr = reply.readLine();
  117.         if (DEBUG) System.out.println(rstr);
  118.         if (!rstr.startsWith("354")) throw new ProtocolException(rstr);
  119.         send.println("From: " + from_address);
  120.         if (DEBUG) System.out.println("From: " + from_address);
  121.         send.println("To: " + to_address);
  122.         if (DEBUG) System.out.println("To: " + to_address);
  123.         send.println("Subject: " + subject);
  124.         if (DEBUG) System.out.println("Subject: " + subject);
  125.  
  126.         // Create Date - we'll cheat by assuming that local clock is right
  127.         // and that GMT String is formatted correctly.
  128.  
  129.         Date today_date = new Date();
  130. //        send.println("Date: " + today_date.toGMTString());
  131.         send.println("Date: " + msgDateFormat(today_date));
  132.         if (DEBUG) System.out.println("Date: " + msgDateFormat(today_date));
  133.  
  134.         // Warn the world that we are on the loose - with the comments header:
  135.         send.println("Comment: Unauthenticated sender");
  136.         send.println("X-Mailer: JNet Smtp");
  137.  
  138.         // Sending a blank line ends the header part.
  139.         send.println("");
  140.         if (DEBUG) System.out.println("");
  141.         // Now send the message proper
  142.         send.println(message);
  143.         if (DEBUG) System.out.println(message);
  144.         send.println(".");
  145.         if (DEBUG) System.out.println(".");
  146.         rstr = reply.readLine();
  147.         if (DEBUG) System.out.println(rstr);
  148.         if (!rstr.startsWith("250")) throw new ProtocolException(rstr);
  149.     }
  150. DEF_ENDLIST
  151. DEF_METHOD
  152.     protected void finalize() throws Throwable {
  153.         send.println("QUIT");
  154.         sock.close();
  155.         super.finalize();
  156.     }
  157. DEF_ENDLIST
  158. DEF_METHOD
  159.     private String msgDateFormat( Date senddate) {
  160.         String formatted = "hold";
  161.  
  162.         String Day[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  163.         String Month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  164.  
  165.         formatted = Day[senddate.getDay()] + ", ";
  166.         formatted = formatted + String.valueOf(senddate.getDate()) + " ";
  167.         formatted = formatted + Month[senddate.getMonth()] + " ";
  168.         if (senddate.getYear() > 99)
  169.             formatted = formatted + String.valueOf(senddate.getYear() + 1900) + " ";
  170.         else
  171.             formatted = formatted + String.valueOf(senddate.getYear()) + " ";
  172.         if (senddate.getHours() < 10) formatted = formatted + "0";
  173.         formatted = formatted + String.valueOf(senddate.getHours()) + ":";
  174.         if (senddate.getMinutes() < 10) formatted = formatted + "0";
  175.         formatted = formatted + String.valueOf(senddate.getMinutes()) + ":";
  176.         if (senddate.getSeconds() < 10) formatted = formatted + "0";
  177.         formatted = formatted + String.valueOf(senddate.getSeconds()) + " ";
  178.         if (senddate.getTimezoneOffset() < 0)
  179.             formatted = formatted + "+";
  180.         else
  181.             formatted = formatted + "-";
  182.         if (senddate.getTimezoneOffset()/60 < 10) formatted = formatted + "0";
  183.         formatted = formatted + String.valueOf(senddate.getTimezoneOffset()/60);
  184.         if (senddate.getTimezoneOffset()%60 < 10) formatted = formatted + "0";
  185.         formatted = formatted + String.valueOf(senddate.getTimezoneOffset()%60);
  186.  
  187.         return formatted;
  188.     }
  189. DEF_ENDLIST
  190. DEF_ENDCOMPONENT
  191. DEF_COMPONENTNAME
  192. SmtpWrapper
  193. DEF_SUPERCLASS
  194. Panel
  195. DEF_SUPERCOMPONENT
  196.  
  197. DEF_PACKAGE
  198. plugins
  199. internet
  200. DEF_ENDLIST
  201. DEF_SUBCOMPONENTLIST
  202. DEF_ENDLIST
  203. DEF_SUBCOMPONENTCLASSLIST
  204. DEF_ENDLIST
  205. DEF_CATEGORY
  206. Internet
  207. DEF_BITMAP
  208.  
  209. DEF_THUMBNAIL_UP
  210. but1.bmp
  211. DEF_THUMBNAIL_DOWN
  212. d-but1.bmp
  213. DEF_VISUAL
  214. DEF_PANEL
  215. DEF_IMPORTS
  216. java.io.IOException
  217. java.net.ProtocolException
  218. java.net.UnknownHostException
  219. DEF_ENDLIST
  220. DEF_REQUIRES
  221. DEF_ENDLIST
  222. DEF_IMPLEMENTS
  223. DEF_ENDLIST
  224. DEF_DECLARATION
  225. // A class that is a wrapper for the smtp class.
  226. //  Variable Declarations
  227.     private String from;
  228.     private String to;
  229.     private String subject = "Uninitialized";
  230.     private String message = "Uninitialized";
  231.     private String mailhost = "unknown";
  232.     private boolean editTo;
  233.     private boolean editFrom;
  234. //    Button sendit;
  235. //    Label fromLabel;
  236. //    Label toLabel;
  237. //    TextField fromText, toText;
  238. //    TextField subjText;
  239. //    TextArea mesgText;
  240. DEF_ENDLIST
  241. DEF_EVENT
  242.     public boolean action(Event e, Object o) {
  243.         if (e.target instanceof Button)  {
  244.             if ("Send".equals( (String) o ) ) {
  245.                 try {
  246. /*  Nice idea - but for some reason it is locking up... I'll fix it for v1.1 :-)
  247. //                  showStatus("Getting Mailhost");
  248.                     String mailhost = getCodeBase().getHost();
  249. */
  250. //                  showStatus("Connecting to mailhost ");
  251.                     Smtp connect = new Smtp(mailhost);
  252. //                    showStatus("Connected to mailhost... sending...");
  253.  
  254. //      temporarily commented...
  255. //                    subject = subjText.getText();
  256. //                    message = mesgText.getText();
  257.  
  258.                     connect.sendmsg(from,to,subject,message);
  259. //                    showStatus("Message sent");
  260.  
  261. //   temporarily commented...
  262. //                    if (fromText.isEditable())
  263. //                        fromText.setText(from);
  264. //                    if (toText.isEditable())
  265. //                        toText.setText(to);
  266. //
  267. //                    subjText.setText("Send from Applet");
  268. //                    mesgText.setText("Sample Message");
  269.                 }
  270.                 catch (UnknownHostException x1) {
  271. //                    showStatus("Failed to find host - " + mailhost);
  272.                     System.out.println(x1.getMessage());
  273.                 }
  274.                 catch (ProtocolException x2) {
  275. //                    showStatus("Some sort of protocol exception");
  276.                     System.out.println(x2.getMessage());
  277.                 }
  278.                 catch (IOException x3) {
  279. //                    showStatus("Error reading/writing to socket on " + mailhost);
  280.                     System.out.println(x3.getMessage());
  281.                 }
  282.                 finally {
  283.                 }
  284.                 return true;
  285.             }
  286.         }
  287.         if (e.target instanceof TextField) {
  288.         return true;
  289.         }
  290.         return super.action(e,o);
  291.     }
  292. DEF_ENDLIST
  293. DEF_METHOD
  294. void initialize()
  295. {
  296.  //        resize(300, 200);
  297. //        from = getParameter("From");
  298. //        to = getParameter("To");
  299. //        mailhost = getParameter("Mailhost");
  300. //        fromLabel = new Label("From: ");
  301. //        toLabel = new Label("To: ");
  302. //        fromText = new TextField(from,10);
  303. //        toText = new TextField(to,10);
  304.  
  305. //        System.out.println("mark1");
  306. //        if (getParameter("EditTo") != null && getParameter("EditTo").equals("false"))
  307. //            toText.setEditable(false);
  308. //        else
  309. //            toText.setEditable(true);
  310.  
  311. //   temporarily commented...
  312. //        if (editTo == false)
  313. //          toText.setEditable(false);
  314. //        else
  315. //          toText.setEditable(true);
  316.  
  317.  
  318. //        if (getParameter("EditFrom") != null && getParameter("EditFrom").equals("true"))
  319. //            fromText.setEditable(true);
  320. //        else
  321. //            fromText.setEditable(false);
  322.  
  323. //   temporarily commented...
  324. //        if (editFrom == true)
  325. //            fromText.setEditable(true);
  326. //        else
  327. //            fromText.setEditable(false);
  328.  
  329. //        add(fromLabel);
  330. //        add(fromText);
  331. //        add(toLabel);
  332. //        add(toText);
  333. //        subjText = new TextField("Send from Applet", 40);
  334. //        add(subjText);
  335. //        mesgText = new TextArea("Sample Message",5,40);
  336. //        add(mesgText);
  337. //        sendit = new Button("Send");
  338. //        add(sendit);
  339. }
  340. DEF_ENDLIST
  341. DEF_METHOD
  342.     public void paint(Graphics g) {
  343.     }
  344. DEF_ENDLIST
  345. DEF_METHOD
  346. public void setSmtpTo(String aParam)
  347. {
  348.   to = aParam;
  349. }
  350. DEF_ENDLIST
  351. DEF_METHOD
  352. public String getSmtpTo()
  353. {
  354.   return to;
  355. }
  356. DEF_ENDLIST
  357. DEF_METHOD
  358. public void setSmtpFrom(String aParam)
  359. {
  360.   from = aParam;
  361. }
  362. DEF_ENDLIST
  363. DEF_METHOD
  364. public String getSmtpFrom()
  365. {
  366.   return from;
  367. }
  368. DEF_ENDLIST
  369. DEF_METHOD
  370. public void setSmtpMailHost(String aParam)
  371. {
  372.   mailhost = aParam;
  373. }
  374. DEF_ENDLIST
  375. DEF_METHOD
  376. public String getSmtpMailHost()
  377. {
  378.   return mailhost;
  379. }
  380. DEF_ENDLIST
  381. DEF_METHOD
  382. public void setSmtpEditTo(boolean aParam)
  383. {
  384.   editTo = aParam;  
  385. }
  386. DEF_ENDLIST
  387. DEF_METHOD
  388. public boolean getSmtpEditTo()
  389. {
  390.   return editTo;
  391. }
  392. DEF_ENDLIST
  393. DEF_METHOD
  394. public void setSmtpEditFrom(boolean aParam)
  395. {
  396.   editFrom = aParam;
  397. }
  398. DEF_ENDLIST
  399. DEF_METHOD
  400. public boolean getSmtpEditFrom()
  401. {
  402.   return editFrom;  
  403. }
  404. DEF_ENDLIST
  405. DEF_PROPERTY
  406. To:
  407. String
  408. setSmtpTo(AVALUE);
  409. AVALUE=getSmtpTo();
  410.  
  411. DEF_ENDLIST
  412. DEF_PROPERTY
  413. From:
  414. String
  415. setSmtpFrom(AVALUE);
  416. AVALUE=getSmtpFrom();
  417.  
  418. DEF_ENDLIST
  419. DEF_PROPERTY
  420. MailHost
  421. String
  422. setSmtpMailHost(AVALUE);
  423. AVALUE=getSmtpMailHost();
  424.  
  425. DEF_ENDLIST
  426. DEF_PROPERTY
  427. Edit To
  428. boolean
  429. setSmtpEditTo(AVALUE);
  430. AVALUE=getSmtpEditTo();
  431. true
  432. DEF_ENDLIST
  433. DEF_PROPERTY
  434. Edit From
  435. boolean
  436. setSmtpEditFrom(AVALUE);
  437. AVALUE=getSmtpEditFrom();
  438. true
  439. DEF_ENDLIST
  440. DEF_PROPERTY
  441. Top
  442. int
  443. move(bounds().x, AVALUE);
  444. AVALUE = bounds().y;
  445. 0
  446. DEF_ENDLIST
  447. DEF_PROPERTY
  448. Left
  449. int
  450. move(AVALUE, bounds().y);
  451. AVALUE = bounds().x;
  452. 0
  453. DEF_ENDLIST
  454. DEF_PROPERTY
  455. Height
  456. int
  457. resize(bounds().width, AVALUE);
  458. AVALUE = bounds().height;
  459. 150
  460. DEF_ENDLIST
  461. DEF_PROPERTY
  462. Width
  463. int
  464. resize(AVALUE, bounds().height);
  465. AVALUE = bounds().width;
  466. 150
  467. DEF_ENDLIST
  468. DEF_ENDCOMPONENT
  469.