/* Main Entry */ int main(int argc, char **argv) { // init connection object Connection conn; memset(&conn, 0, sizeof(conn)); conn.port = PORT; // assign default port conn.host = HOST; // Parse arguments parseArguments(argc, argv, &conn); // init SSL library conn.sslContext = initSSLContext(CLIENT_CERTIFICATE, CA_CERTIFICATE); SSL_CTX_set_options(conn.sslContext, SSL_OP_NO_SSLv2); SSL_CTX_set_cipher_list(conn.sslContext, "SHA1"); // Connect if (tcpConnect(&conn) < 0){ tcpDisconnect(&conn); exit(0); } SSL * ssl = SSL_new(conn.sslContext); BIO * sbio = BIO_new_socket(conn.socket, BIO_NOCLOSE); SSL_set_bio(ssl, sbio, sbio); int ret; ret = SSL_connect(ssl); if (ret <= 0){ printf(FMT_CONNECT_ERR); handleError(ssl, ret); } else{ // Process Message if (checkServerCertification(ssl) == OK){ processMessage(ssl); } } // close ssl connection if (!SSL_shutdown(ssl)){ tcpDisconnect(&conn); SSL_shutdown(ssl); } SSL_free(ssl); // Disconnect tcpDisconnect(&conn); destroySSLContext(conn.sslContext); return 1; }
bool SSMTP::send(MIMEmessage *message) { log.clear(); int mSize = message->getMIMEText().toLatin1().size(); /* calc timeout for slowly 56 kbps connection */ messageRespTimeout = (mSize / (56000/8)) * 1000; if (!tcpConnect()) return false; if (!waitForResponse()) return false; if (!checkRespCode(220)) return false; if (!sendCmd("EHLO " + smtp_name,250)) return false; if (!smtp_user.isEmpty() || smtp_pass.isEmpty()) { if (!sendCmd("AUTH LOGIN",334)) return false; if (!sendCmd(smtp_user.toUtf8().toBase64(),334)) return false; if (!sendCmd(smtp_pass.toUtf8().toBase64(),235)) return false; } if (!sendCmd("MAIL FROM: " + message->getFromEmail(),250)) return false; for (int i=0; i < message->rcptList()->count(); ++i) { if (!sendCmd("RCPT TO: " + message->rcptList()->at(i),250)) return false; } if (!sendCmd("DATA",354)) return false; sendText(message->getMIMEText(),true); sendText("."); if (!waitForResponse(true)) return false; if (!checkRespCode(250)) return false; if (!sendCmd("QUIT",221)) return false; tcpDisconnect(); return true; }
bool SSMTP::checkRespCode(int code) { if (code != responseCode) { writeLog("Error: bad response code"); tcpDisconnect(); return false; } return true; }
bool SSMTP::waitForResponse(bool isMessage) { int tm = isMessage ? messageRespTimeout : commandRespTimeout; if (!socket->waitForReadyRead(tm)) { if (isMessage) writeLog("Error: Message response timeout"); else writeLog("Error: Command response timeout"); tcpDisconnect(); return false; } while (socket->canReadLine()) { responseText = socket->readLine(); responseCode = responseText.left(3).toInt(); writeLog("S <= " + responseText); } return true; }