Example #1
0
 // ON SEND MAIL
 int onSendMail() {
   String inBuffer;
   String outBuffer;
   Socket sock;
   SMTPInputStream fromServer;
   SMTPOutputStream toServer;
   int intPort;
   // TEST RECIPIENT ADDRESS it must be at once
   if (reciverMail.getText().length() < 1) {
     infoArea.Println("enter recipient please ...");
     return -1;
   }
   // TEST PORT NUMBER FORMAT and ALERT WHEN NOT 25
   intPort = -1;
   infoArea.Clear();
   infoArea.Println("check port ...");
   try {
     intPort = Integer.valueOf(smtpPort.getText()).intValue();
     if ((intPort < 0) || (intPort > 65356)) {
       infoArea.Println("invalid port ...");
       return -1;
     }
     if (intPort != 25) infoArea.Println("attention port is not 25 ...");
     infoArea.Println("port number is " + smtpPort.getText());
   } catch (NumberFormatException e) {
     infoArea.Println("invalid port ...");
     return -1;
   }
   // CREATE SMTP CONNECTION
   infoArea.Println("connect to server ...");
   try {
     sock = new Socket(smtpServer.getText(), intPort);
   } catch (IOException e) {
     infoArea.Println("unable to connect(SK) ... " + e);
     return -1;
   }
   // CREATE SERVER STREAMS
   // INPUT STREAM
   try {
     fromServer = new SMTPInputStream(sock);
   } catch (IOException e) {
     infoArea.Println("unable to connect(IS) ...");
     return -1;
   }
   // OUTPUT STREAM
   try {
     toServer = new SMTPOutputStream(sock);
   } catch (IOException e) {
     infoArea.Println("unable to connect(OS) ...");
     return -1;
   }
   infoArea.Println("connected ...");
   // Mail Sending
   infoArea.Println("sending mail ...");
   // *******************************************************************
   try {
     int i = doSMTPTransaction(fromServer, toServer);
     if (i == 0) infoArea.Println("complete ...");
     else infoArea.Println("error ...");
   } catch (IOException e) {
     infoArea.Println("error ...");
   }
   // *******************************************************************
   // CLOSE SERVER STREAMS
   // OUTPUT STREAM
   infoArea.Println("disconnect from server ...");
   try {
     toServer.Close();
   } catch (IOException e) {
     infoArea.Println("alert OUTPUT STREAM ...");
   }
   // INPUT STREAM
   try {
     fromServer.Close();
   } catch (IOException e) {
     infoArea.Println("alert INPUT STREAM ...");
   }
   // CLOSE CONNECTION
   try {
     sock.close();
     infoArea.Println("disconnected ...");
   } catch (IOException e) {
     infoArea.Println("unable to disconnect ...");
     return -1;
   }
   return 0;
 }
Example #2
0
  int doSMTPTransaction(SMTPInputStream in, SMTPOutputStream out) throws IOException {
    int replyCode;
    Date today = new Date();
    // GET SERVER RESPONSE on CONNECTION ESTABLISHMENT
    // WAIT FOR RESPONSE (WAIT_TIMEOUT) millis
    //      NOT RESPONSE ON CONNECT ERROR CODE 1
    replyCode = waitForTimeout(in);
    if (replyCode == 0) {
      infoArea.Println("NOT RESPONSE ON CONNECT ERROR CODE 1");
      return 1;
    }
    if (replyCode == -1) {
      infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
      return 4;
    }

    if (replyCode != SMTP_RCODE_READY) {
      infoArea.Println("UNSUCCESS CONNECTION ");
      return replyCode;
    }

    infoArea.Println("connection response is OK");
    infoArea.Println("request HELO");

    // SEND HELO CRLF
    out.WriteToStream(SMTPCommand[SMTP_CMD_HELO]);
    out.WriteToStream(SMTPCommand[SMTP_CMD_SPC]);
    out.WriteToStream("127.0.0.1");
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);

    // WAIT FOR RESPONSE ON HELO
    replyCode = waitForTimeout(in);
    if (replyCode == 0) {
      infoArea.Println("NOT RESPONSE TO HELO ERROR CODE 2");
      return 2;
    }
    if (replyCode == -1) {
      infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
      return 4;
    }
    if (replyCode != SMTP_RCODE_COMPLETED) {
      infoArea.Println("UNABLE TO COMPLETE REPLY");
      return replyCode;
    }
    infoArea.Println("HELO response is OK");
    infoArea.Println("request MAIL FROM");

    // SEND MAIL FROM: SPC <sender> CRLF
    out.WriteToStream(SMTPCommand[SMTP_CMD_MAIL_FROM]);
    out.WriteToStream(SMTPCommand[SMTP_CMD_SPC]);
    out.WriteToStream(senderMail.getText());
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);

    // WAIT FOR RESPONSE ON MAIL FROM
    replyCode = waitForTimeout(in);
    if (replyCode == 0) {
      infoArea.Println("NOT RESPONSE TO HELO ERROR CODE 2");
      return 2;
    }
    if (replyCode == -1) {
      infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
      return 4;
    }
    if (replyCode != SMTP_RCODE_COMPLETED) {
      infoArea.Println("UNABLE TO COMPLETE REPLY");
      return replyCode;
    }
    infoArea.Println("MAIL FROM response is OK");
    infoArea.Println("request RCPT TO");

    // SEND RCPT TO: SPC <sender> CRLF
    out.WriteToStream(SMTPCommand[SMTP_CMD_RCPT_TO]);
    out.WriteToStream(SMTPCommand[SMTP_CMD_SPC]);
    out.WriteToStream(reciverMail.getText());
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);

    // WAIT FOR RESPONSE ON RCPT TO
    replyCode = waitForTimeout(in);
    if (replyCode == 0) {
      infoArea.Println("NOT RESPONSE TO HELO ERROR CODE 2");
      return 2;
    }
    if (replyCode == -1) {
      infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
      return 4;
    }
    if ((replyCode != SMTP_RCODE_COMPLETED) && (replyCode != SMTP_RCODE_FORWARD)) {
      infoArea.Println("UNABLE TO COMPLETE REPLY");
      return replyCode;
    }
    infoArea.Println("RCPT TO response is OK");
    // TRY TO SEND TO CC
    if ((ccMail.getText().length()) > 0) {
      infoArea.Println("request CC RCPT TO");
      // SEND RCPT TO: SPC <sender> CRLF
      out.WriteToStream(SMTPCommand[SMTP_CMD_RCPT_TO]);
      out.WriteToStream(SMTPCommand[SMTP_CMD_SPC]);
      out.WriteToStream(ccMail.getText());
      out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);

      // WAIT FOR RESPONSE ON RCPT TO
      replyCode = waitForTimeout(in);
      if (replyCode == 0) {
        infoArea.Println("NOT RESPONSE TO HELO ERROR CODE 2");
        return 2;
      }
      if (replyCode == -1) {
        infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
        return 4;
      }
      if ((replyCode != SMTP_RCODE_COMPLETED) && (replyCode != SMTP_RCODE_FORWARD)) {
        infoArea.Println("UNABLE TO COMPLETE CC");
      } else {
        infoArea.Println("CC.RCPT TO response is OK");
      }
    }
    // TRY TO SEND TO BCC
    if ((bccMail.getText().length()) > 0) {
      // SEND RCPT TO: SPC <sender> CRLF
      out.WriteToStream(SMTPCommand[SMTP_CMD_RCPT_TO]);
      out.WriteToStream(SMTPCommand[SMTP_CMD_SPC]);
      out.WriteToStream(bccMail.getText());
      out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);

      // WAIT FOR RESPONSE ON RCPT TO
      replyCode = waitForTimeout(in);
      if (replyCode == 0) {
        infoArea.Println("NOT RESPONSE TO HELO ERROR CODE 2");
        return 2;
      }
      if (replyCode == -1) {
        infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
        return 4;
      }
      if ((replyCode != SMTP_RCODE_COMPLETED) && (replyCode != SMTP_RCODE_FORWARD)) {
        infoArea.Println("UNABLE TO COMPLETE BCC");
      } else {
        infoArea.Println("BCC.RCPT TO response is OK");
      }
    }
    infoArea.Println("request DATA");

    // SEND DATA CRLF
    out.WriteToStream(SMTPCommand[SMTP_CMD_DATA]);
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);

    // WAIT FOR RESPONSE ON DATA
    replyCode = waitForTimeout(in);
    if (replyCode == 0) {
      infoArea.Println("NOT RESPONSE TO HELO ERROR CODE 2");
      return 2;
    }
    if (replyCode == -1) {
      infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
      return 4;
    }
    if (replyCode != SMTP_RCODE_MAIL_START) {
      infoArea.Println("UNABLE TO COMPLETE REPLY");
      return replyCode;
    }
    infoArea.Println("DATA response is OK");
    infoArea.Println("request DATA");

    // SEND mail content CRLF.CRLF
    out.WriteToStream("Subject: " + subjectMail.getText());
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);
    out.WriteToStream("From: " + senderMail.getText());
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);
    out.WriteToStream("To: " + reciverMail.getText());
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);
    out.WriteToStream("Date: " + today.toString());
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);
    out.WriteToStream(bodyMail.getText());
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF_CRLF]);

    // WAIT FOR RESPONSE ON mail content CRLF.CRLF
    replyCode = waitForTimeout(in);
    if (replyCode == 0) {
      infoArea.Println("NOT RESPONSE TO HELO ERROR CODE 2");
      return 2;
    }
    if (replyCode == -1) {
      infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
      return 4;
    }
    if (replyCode != SMTP_RCODE_COMPLETED) {
      infoArea.Println("UNABLE TO COMPLETE REPLY");
      return replyCode;
    }
    infoArea.Println("mail content response is OK");
    infoArea.Println("request QUIT");

    // SEND QUIT CRLF
    out.WriteToStream(SMTPCommand[SMTP_CMD_QUIT]);
    out.WriteToStream(SMTPCommand[SMTP_CMD_CRLF]);

    // WAIT FOR RESPONSE (WAIT_TIMEOUT) millis
    //      NOT RESPONSE TO QUIT ERROR CODE 3
    replyCode = waitForTimeout(in);
    if (replyCode == 0) {
      infoArea.Println("NOT RESPONSE TO QUIT ERROR CODE 3");
      return 3;
    }
    if (replyCode == -1) {
      infoArea.Println("UNKNOWN REPLY CODE ERROR CODE 4");
      return 4;
    }
    if (replyCode != SMTP_RCODE_CLOSING) {
      infoArea.Println("UNCLOSED CONNECTION REPLY");
      return replyCode;
    }
    infoArea.Println("QUIT response is BYE");
    infoArea.Println("end of SMTP transaction");

    //  TRANSACTION OK ERROR CODE 0
    return 0;
  }