using wxSocket for a simple SMTP e-mail client Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Wed Jul 05, 2006 10:36 am

'\n' is the same as the ascii value. so writing \r\n should work fine.

Maybe your inputcontent is to long for the toSend array.
Anyways you should try to avoid working with char buffers,
use wxString instead.

ciammarica
Knows some wx things
Knows some wx things
Posts: 29
Joined: Tue Jul 04, 2006 6:49 am

Post by ciammarica » Wed Jul 05, 2006 6:46 pm

I've already checked, the inputContent isn't too long for the buffer, because the simple echo server i've implemented to test the SMTP client sends back correctly all the strings, from the beginning to the end.
It's a 50 elements array, while the inputContent is never longer than 40.
However I'll try with wxString as you suggested :)
I'll let you know...

ciammarica
Knows some wx things
Knows some wx things
Posts: 29
Joined: Tue Jul 04, 2006 6:49 am

Post by ciammarica » Wed Jul 05, 2006 7:50 pm

Still not working... I don't think the problem depends by the type of variables, I think instead there's something wrong on the last two chars for the Carriage Return and Line Feed, there's no other explaination!

ciammarica
Knows some wx things
Knows some wx things
Posts: 29
Joined: Tue Jul 04, 2006 6:49 am

Post by ciammarica » Wed Jul 05, 2006 8:46 pm

Problem solved!!! :)
Now the program sends correctly the e-mail :)
The problem was that as second parameter of the "Write" method I didn't have to put "sizeof(toSend)", but the length of the string sent to the server ("toSend.Length()").
It was so simple, how couldn't I think about this before???
As suggested, I've changed the variable type from char array to wxString.
So Here I post the final working code (I have still to work a lot on it, and I must still add the "Subject", "From", and "To" commands, but at least for now the socket is working :) )

Code: Select all

#include "wx/wxprec.h"

#include <math.h>
#include <wx/notebook.h>
#include <wx/textfile.h>
#include <wx/textctrl.h>
#include <wx/socket.h>

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#define PORT 25

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

class MyApp : public wxApp
{
 public:

    virtual bool OnInit();
};


class MyFrame : public wxFrame
{
 public:
 
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
            long style = wxDEFAULT_FRAME_STYLE);
 
    void OnClose(wxCommandEvent &event);
    void OnClear(wxCommandEvent &event);
    void OnSend(wxCommandEvent &event); 
    void OnQuit(wxCommandEvent &event);
    void OnIdle(wxIdleEvent &event);
    void OnAbout(wxCommandEvent &event);
    void OnSocketEvent(wxSocketEvent &event);

private:
       
    wxTextCtrl* serverInput;
    wxTextCtrl* mailFromInput;
    wxTextCtrl* rcptToInput;
    wxTextCtrl* dataInput;
    wxTextCtrl* statusInput;
    
    wxButton* sendButton;
    wxButton* cancelButton;
    wxButton* clearButton;
        
    wxMenu* menuFile;
    
    wxPanel* panel;
    
    DECLARE_EVENT_TABLE()
};

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

enum
{
 Minimal_Quit = 1,
 Minimal_About = wxID_ABOUT,
 
 ID_ServerInput = 2,
 ID_MailFromInput = 3,
 ID_RcptToInput = 4,
 ID_DataInput = 5,
 
 ID_SendButton = 6,
 ID_CancelButton = 7,
 ID_ClearButton = 8,
 
 SOCKET_ID = 10,
};

// ----------------------------------------------------------------------------
// event tables and other macros for wxWindows
// ----------------------------------------------------------------------------

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    
    EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
    EVT_MENU(Minimal_About, MyFrame::OnAbout)
    
    EVT_BUTTON(ID_CancelButton, MyFrame::OnClose)
    EVT_BUTTON(ID_SendButton, MyFrame::OnSend)
    EVT_BUTTON(ID_ClearButton, MyFrame::OnClear)
    
    EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
    
    EVT_IDLE(MyFrame::OnIdle)

END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)


bool MyApp::OnInit()
{
 MyFrame *frame = new MyFrame(_T("SMTP Client"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE & (wxCAPTION | wxRESIZE_BOX | wxMAXIMIZE_BOX));
                                 
 frame->Centre(wxBOTH);                             
                                 
 frame->Show(TRUE);

 return TRUE;
}


// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
       : wxFrame(NULL, -1, title, pos, size, style)
{
 #if wxUSE_MENUS

 menuFile = new wxMenu;

 wxMenu *helpMenu = new wxMenu;
 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));

 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
    
 menuFile->Enable(wxID_SAVE, FALSE);

 wxMenuBar *menuBar = new wxMenuBar();
 menuBar->Append(menuFile, _T("&File"));
 menuBar->Append(helpMenu, _T("&Help"));

 SetMenuBar(menuBar);
    
 #endif // wxUSE_MENUS
  
 panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize);
 
 serverInput = new wxTextCtrl(panel, ID_ServerInput, "", wxDefaultPosition, wxSize(200, 20));
 wxStaticText* serverLabel = new wxStaticText(panel, -1, "SMTP Server:", wxDefaultPosition, wxDefaultSize);
 
 mailFromInput = new wxTextCtrl(panel, ID_MailFromInput, "", wxDefaultPosition, wxSize(200, 20));
 wxStaticText* mailFromLabel = new wxStaticText(panel, -1, "Mail From:      ", wxDefaultPosition, wxDefaultSize);
 
 rcptToInput = new wxTextCtrl(panel, ID_RcptToInput, "", wxDefaultPosition, wxSize(200, 20));
 wxStaticText* rcptToLabel = new wxStaticText(panel, -1, "Rcpt To:        ", wxDefaultPosition, wxDefaultSize);
 
 dataInput = new wxTextCtrl(panel, ID_DataInput, "", wxDefaultPosition, wxSize(200, 100), wxTE_MULTILINE);
 wxStaticText* dataLabel = new wxStaticText(panel, -1, "E-mail Text:    ", wxDefaultPosition, wxDefaultSize);
 
 statusInput = new wxTextCtrl(panel, -1, "Not connected yet.\nPress \"Send\" when ready...", wxDefaultPosition, wxSize(200, 70), wxTE_MULTILINE);
 statusInput->SetEditable(FALSE);
 
 sendButton = new wxButton(panel, ID_SendButton, "Send", wxDefaultPosition, wxDefaultSize);
 cancelButton = new wxButton(panel, ID_CancelButton, "Exit", wxDefaultPosition, wxDefaultSize);
 clearButton = new wxButton(panel, ID_ClearButton, "Clear All", wxDefaultPosition, wxDefaultSize);
 
 wxFlexGridSizer* frameSizer = new wxFlexGridSizer(3, 1, 20, 20);
 
 wxFlexGridSizer* commandsSizer = new wxFlexGridSizer(6, 1, 10, 10);
 
 wxBoxSizer* serverSizer = new wxBoxSizer(wxHORIZONTAL);
 
 serverSizer->Add(10, 10);
 serverSizer->Add(serverLabel, 0, wxALIGN_CENTER);
 serverSizer->Add(10, 10);
 serverSizer->Add(serverInput, 0, wxALIGN_CENTER);
 serverSizer->Add(10, 10);
 
 wxBoxSizer* mailFromSizer = new wxBoxSizer(wxHORIZONTAL);
 
 mailFromSizer->Add(10, 10);
 mailFromSizer->Add(mailFromLabel, 0, wxALIGN_CENTER);
 mailFromSizer->Add(10, 10);
 mailFromSizer->Add(mailFromInput, 0, wxALIGN_CENTER);
 mailFromSizer->Add(10, 10);
 
 wxBoxSizer* rcptToSizer = new wxBoxSizer(wxHORIZONTAL);
 
 rcptToSizer->Add(10, 10);
 rcptToSizer->Add(rcptToLabel, 0, wxALIGN_CENTER);
 rcptToSizer->Add(10, 10);
 rcptToSizer->Add(rcptToInput, 0, wxALIGN_CENTER);
 rcptToSizer->Add(10, 10);
 
 wxBoxSizer* dataSizer = new wxBoxSizer(wxHORIZONTAL);
 
 dataSizer->Add(10, 10);
 dataSizer->Add(dataLabel, 0, wxALIGN_CENTER);
 dataSizer->Add(10, 10);
 dataSizer->Add(dataInput, 0, wxALIGN_CENTER);
 dataSizer->Add(10, 10);
 
 wxStaticBox* statusBox = new wxStaticBox(panel, -1, "Status", wxDefaultPosition, wxDefaultSize);
 
 wxStaticBoxSizer* statusBoxSizer = new wxStaticBoxSizer(statusBox, wxHORIZONTAL);
 
 wxBoxSizer* statusSizer = new wxBoxSizer(wxHORIZONTAL);
 
 statusSizer->Add(10, 10);
 statusSizer->Add(statusInput, 1, wxGROW | wxALIGN_CENTER);
 statusSizer->Add(10, 10);
 
 statusBoxSizer->Add(statusSizer, 1, wxALIGN_CENTER);
 
 commandsSizer->Add(10, 10);
 commandsSizer->Add(serverSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(mailFromSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(rcptToSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(dataSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(statusBoxSizer, 1, wxGROW | wxALIGN_CENTER);
 
 wxBoxSizer* buttonsSizer = new wxBoxSizer(wxHORIZONTAL);
 
 buttonsSizer->Add(cancelButton, 0, wxALIGN_CENTER);
 buttonsSizer->Add(clearButton, 0, wxALIGN_CENTER);
 buttonsSizer->Add(sendButton, 0, wxALIGN_CENTER);
 
 frameSizer->Add(commandsSizer, 1, wxGROW | wxALIGN_CENTER);
 frameSizer->Add(buttonsSizer, 0, wxALIGN_CENTER);
 frameSizer->Add(10, 10);
 
 frameSizer->Fit(this);
 
 CenterOnParent(wxBOTH);
 
 SetSizer(frameSizer);
 SetAutoLayout(TRUE);
 Layout();
 
 SetSize(300, 430);

 #if wxUSE_STATUSBAR
 
 CreateStatusBar(2);
 SetStatusText(_T("Ready..."));
    
 #endif
}


void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
 Close(TRUE);
}


void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxString msg;
    msg.Printf(_T("\"SMTP Client\"\n\n")
               _T("- Author: Stefano Pace\n\n")
               _T("- Compiled with:\n\n")
               _T("Dev-C++ v.4.9.9.2\n\n")
               _T("- GUI toolkit:\n\n")
               _T("%s"), wxVERSION_STRING);

    wxMessageBox(msg, _T("About SMTP Client"), wxOK | wxICON_INFORMATION, this);
}


void MyFrame::OnIdle(wxIdleEvent &event)
{
 //SetStatusText("Ready...");
}     


void MyFrame::OnClose(wxCommandEvent &event)
{
 this->Destroy();
}     


void MyFrame::OnClear(wxCommandEvent &event)
{
 serverInput->SetValue("");
 mailFromInput->SetValue("");
 rcptToInput->SetValue("");
 dataInput->SetValue("");
 statusInput->SetValue("Not connected yet.\nPress \"Send\" when ready...");
}    


void MyFrame::OnSend(wxCommandEvent& event)
{    
 wxString serverAddress;    

 serverAddress = serverInput->GetValue();

 wxIPV4address addr;
 addr.Hostname(serverAddress);
 addr.Service(PORT);

 // Create the socket
 wxSocketClient* Socket = new wxSocketClient();

 // Set up the event handler and subscribe to most events
 Socket->SetEventHandler(*this, SOCKET_ID);
 Socket->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG | wxSOCKET_OUTPUT_FLAG);
 Socket->Notify(TRUE);

 // Wait for the connection event
 Socket->Connect(addr, FALSE);
 
 SetStatusText("Connecting...");
 statusInput->AppendText("\nTrying to connect to");
 statusInput->AppendText(serverAddress);
 statusInput->AppendText("...");
 
 Socket->WaitOnConnect(10);
 
 if (Socket->IsConnected())
         {
          wxString toSend = ""; 
          wxString inputContent; 
                                      
          Socket->Write("HELO SMTP Client v1.0 beta\r\n", 28); 
          
          statusInput->AppendText("\nClient: HELO SMTP Client v1.0 beta");
          
          Socket->WaitForRead(2); 
          
          inputContent = mailFromInput->GetValue(); 
          
          toSend += ("MAIL FROM:<");
          toSend += (inputContent);
          toSend += (">\r\n");
          
          Socket->Write(toSend, toSend.Length());
          
          statusInput->AppendText("\nClient: ");
          statusInput->AppendText(toSend);
          
          Socket->WaitForRead(2); 
          
          inputContent = rcptToInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("RCPT TO:<");
          toSend += (inputContent);
          toSend += (">\r\n");
          
          Socket->Write(toSend, toSend.Length()); 
          
          statusInput->AppendText("\nClient: ");
          statusInput->AppendText(toSend);
          
          Socket->WaitForRead(2); 
          
          Socket->Write("DATA\r\n", 6);
          
          statusInput->AppendText("\nClient: DATA");
          
          Socket->WaitForRead(2); 
          
          inputContent = dataInput->GetValue(); 
          
          toSend = "";
          
          toSend += (inputContent);
          toSend += ("\r\n.\r\n");
          
          statusInput->AppendText("\nClient: Sending e-mail text to the server...");
          
          Socket->Write(toSend, toSend.Length()); 
                    
          Socket->WaitForRead(2); 
          
          Socket->Write("QUIT\r\n", 6); 
          
          statusInput->AppendText("\nClient: QUIT");
          
          Socket->WaitForRead(2); 
          
          Socket->Close(); 
         } 
 else
         {         
          Socket->Close();
         }
}


void MyFrame::OnSocketEvent(wxSocketEvent& event)
{
 // The socket that had the event
 wxSocketBase* sock = event.GetSocket();
 
 // Common buffer shared by the events
 wxChar buf[100] = "";
 
 switch(event.GetSocketEvent())
          {
           case wxSOCKET_CONNECTION:
                   {                    
                    SetStatusText("Connected to SMTP server...");
                    statusInput->AppendText("\n\nConnected to SMTP server...");

                    break;
                   }
                   
           case wxSOCKET_INPUT:
                   {         
                    sock->Read(buf, sizeof(buf));
                    
                    if(sock->Error()) 
                              { 
                               statusInput->AppendText("\nSocket error!");
                               
                               break; 
                              } 
                    
                    statusInput->AppendText("\nServer: ");
                    statusInput->AppendText(buf);
                    
                    break;
                   }
                   
           // The server hangs up after sending the data

           case wxSOCKET_LOST:
                   {
                    sock->Destroy();
                    
                    statusInput->AppendText("\nConnection closed.");
                    SetStatusText("Connection closed.");
                                        
                    wxString msg;
                    msg.Printf(_T("Connection closed."));
                    wxMessageBox(msg, _T("SMTP Client Info"), wxOK | wxICON_INFORMATION);
                    
                    break;
                   }
                   
           case wxSOCKET_OUTPUT:
                   {                        
                    break;
                   }
          }
}

ciammarica
Knows some wx things
Knows some wx things
Posts: 29
Joined: Tue Jul 04, 2006 6:49 am

Post by ciammarica » Thu Jul 06, 2006 10:19 am

This is the final 1.0 version of the program.
In version 2 i'll try to implement attachments and a tool for dns query, to find the SMTP server address automatically.

Code: Select all

#include "wx/wxprec.h"

#include <math.h>
#include <wx/notebook.h>
#include <wx/textfile.h>
#include <wx/textctrl.h>
#include <wx/socket.h>

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#define PORT 25

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

class MyApp : public wxApp
{
 public:

    virtual bool OnInit();
};


class MyFrame : public wxFrame
{
 public:
 
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
            long style = wxDEFAULT_FRAME_STYLE);
 
    void OnClose(wxCommandEvent &event);
    void OnClear(wxCommandEvent &event);
    void OnSend(wxCommandEvent &event); 
    void OnQuit(wxCommandEvent &event);
    void OnIdle(wxIdleEvent &event);
    void OnAbout(wxCommandEvent &event);
    void OnSocketEvent(wxSocketEvent &event);

private:
       
    wxTextCtrl* serverInput;
    wxTextCtrl* mailFromInput;
    wxTextCtrl* rcptToInput;
    wxTextCtrl* dataInput;
    wxTextCtrl* statusInput;
    wxTextCtrl* dateInput;
    wxTextCtrl* xMailerInput;
    wxTextCtrl* replyToInput;
    wxTextCtrl* subjectInput;
    wxTextCtrl* fromInput;
    wxTextCtrl* toInput;
    wxTextCtrl* ccInput;
    wxTextCtrl* bccInput;
    
    wxButton* sendButton;
    wxButton* cancelButton;
    wxButton* clearButton;
        
    wxMenu* menuFile;
    
    wxPanel* panel;
    
    DECLARE_EVENT_TABLE()
};

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

enum
{
 Minimal_Quit = 1,
 Minimal_About = wxID_ABOUT,
 
 ID_ServerInput = 2,
 ID_MailFromInput = 3,
 ID_RcptToInput = 4,
 ID_SubjectInput = 5,
 ID_FromInput = 6,
 ID_ToInput = 7,
 ID_DateInput =8,
 ID_XMailerInput =9,
 ID_ReplyToInput = 10,
 ID_CcInput = 11,
 ID_BccInput = 12,
 ID_DataInput = 13,
 
 ID_SendButton = 14,
 ID_CancelButton = 15,
 ID_ClearButton = 16,
 
 SOCKET_ID = 17,
};

// ----------------------------------------------------------------------------
// event tables and other macros for wxWindows
// ----------------------------------------------------------------------------

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    
    EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
    EVT_MENU(Minimal_About, MyFrame::OnAbout)
    
    EVT_BUTTON(ID_CancelButton, MyFrame::OnClose)
    EVT_BUTTON(ID_SendButton, MyFrame::OnSend)
    EVT_BUTTON(ID_ClearButton, MyFrame::OnClear)
    
    EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
    
    EVT_IDLE(MyFrame::OnIdle)

END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

// ============================================================================
// implementation
// ============================================================================

bool MyApp::OnInit()
{
 MyFrame *frame = new MyFrame(_T("SMTP Client"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE & (wxCAPTION | wxRESIZE_BOX | wxMAXIMIZE_BOX));
                                 
 frame->Centre(wxBOTH);                             
                                 
 frame->Show(TRUE);

 return TRUE;
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
       : wxFrame(NULL, -1, title, pos, size, style)
{
 #if wxUSE_MENUS

 menuFile = new wxMenu;

 wxMenu *helpMenu = new wxMenu;
 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));

 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
    
 menuFile->Enable(wxID_SAVE, FALSE);

 wxMenuBar *menuBar = new wxMenuBar();
 menuBar->Append(menuFile, _T("&File"));
 menuBar->Append(helpMenu, _T("&Help"));

 SetMenuBar(menuBar);
    
 #endif // wxUSE_MENUS
  
 panel = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize);
 
 serverInput = new wxTextCtrl(panel, ID_ServerInput, "", wxDefaultPosition, wxSize(300, 18));
 wxStaticText* serverLabel = new wxStaticText(panel, -1, "SMTP Server:", wxDefaultPosition, wxDefaultSize);
 
 mailFromInput = new wxTextCtrl(panel, ID_MailFromInput, "", wxDefaultPosition, wxSize(300, 18));
 wxStaticText* mailFromLabel = new wxStaticText(panel, -1, "Mail From:      ", wxDefaultPosition, wxDefaultSize);
 
 rcptToInput = new wxTextCtrl(panel, ID_RcptToInput, "", wxDefaultPosition, wxSize(300, 18));
 wxStaticText* rcptToLabel = new wxStaticText(panel, -1, "Rcpt To:        ", wxDefaultPosition, wxDefaultSize);
 
 ccInput = new wxTextCtrl(panel, ID_CcInput, "", wxDefaultPosition, wxSize(300, 18));
 wxStaticText* ccLabel = new wxStaticText(panel, -1, "Cc:                 ", wxDefaultPosition, wxDefaultSize);
 
 bccInput = new wxTextCtrl(panel, ID_BccInput, "", wxDefaultPosition, wxSize(300, 18));
 wxStaticText* bccLabel = new wxStaticText(panel, -1, "Bcc:               ", wxDefaultPosition, wxDefaultSize);
 
 replyToInput = new wxTextCtrl(panel, ID_ReplyToInput, "", wxDefaultPosition, wxSize(300, 18));
 wxStaticText* replyToLabel = new wxStaticText(panel, -1, "Reply-To:       ", wxDefaultPosition, wxDefaultSize);
 
 dateInput = new wxTextCtrl(panel, ID_DateInput, "", wxDefaultPosition, wxSize(117, 18));
 wxStaticText* dateLabel = new wxStaticText(panel, -1, "Date:             ", wxDefaultPosition, wxDefaultSize);
 
 xMailerInput = new wxTextCtrl(panel, ID_XMailerInput, "", wxDefaultPosition, wxSize(119, 18));
 wxStaticText* xMailerLabel = new wxStaticText(panel, -1, "X-Mailer: ", wxDefaultPosition, wxDefaultSize);

 subjectInput = new wxTextCtrl(panel, ID_SubjectInput, "", wxDefaultPosition, wxSize(300, 18));
 wxStaticText* subjectLabel = new wxStaticText(panel, -1, "Subject:         ", wxDefaultPosition, wxDefaultSize);
 
 fromInput = new wxTextCtrl(panel, ID_FromInput, "", wxDefaultPosition, wxSize(117, 18));
 wxStaticText* fromLabel = new wxStaticText(panel, -1, "From:             ", wxDefaultPosition, wxDefaultSize);
 
 toInput = new wxTextCtrl(panel, ID_ToInput, "", wxDefaultPosition, wxSize(117, 18));
 wxStaticText* toLabel = new wxStaticText(panel, -1, "   To:       ", wxDefaultPosition, wxDefaultSize);
 
 dataInput = new wxTextCtrl(panel, ID_DataInput, "", wxDefaultPosition, wxSize(300, 100), wxTE_MULTILINE);
 wxStaticText* dataLabel = new wxStaticText(panel, -1, "E-mail Text:    ", wxDefaultPosition, wxDefaultSize);
 
 statusInput = new wxTextCtrl(panel, -1, "Not connected yet.\nPress \"Send\" when ready...", wxDefaultPosition, wxSize(200, 70), wxTE_MULTILINE);
 statusInput->SetEditable(FALSE);
 
 sendButton = new wxButton(panel, ID_SendButton, "Send", wxDefaultPosition, wxDefaultSize);
 cancelButton = new wxButton(panel, ID_CancelButton, "Exit", wxDefaultPosition, wxDefaultSize);
 clearButton = new wxButton(panel, ID_ClearButton, "Clear All", wxDefaultPosition, wxDefaultSize);
 
 wxFlexGridSizer* frameSizer = new wxFlexGridSizer(3, 1, 10, 10);
 
 wxFlexGridSizer* commandsSizer = new wxFlexGridSizer(13, 1, 5, 5);
 
 wxBoxSizer* serverSizer = new wxBoxSizer(wxHORIZONTAL);
 
 serverSizer->Add(10, 10);
 serverSizer->Add(serverLabel, 0, wxALIGN_CENTER);
 serverSizer->Add(10, 10);
 serverSizer->Add(serverInput, 0, wxALIGN_CENTER);
 serverSizer->Add(10, 10);
 
 wxBoxSizer* mailFromSizer = new wxBoxSizer(wxHORIZONTAL);
 
 mailFromSizer->Add(10, 10);
 mailFromSizer->Add(mailFromLabel, 0, wxALIGN_CENTER);
 mailFromSizer->Add(10, 10);
 mailFromSizer->Add(mailFromInput, 0, wxALIGN_CENTER);
 mailFromSizer->Add(10, 10);
 
 wxBoxSizer* rcptToSizer = new wxBoxSizer(wxHORIZONTAL);
 
 rcptToSizer->Add(10, 10);
 rcptToSizer->Add(rcptToLabel, 0, wxALIGN_CENTER);
 rcptToSizer->Add(10, 10);
 rcptToSizer->Add(rcptToInput, 0, wxALIGN_CENTER);
 rcptToSizer->Add(10, 10);
 
 wxBoxSizer* ccSizer = new wxBoxSizer(wxHORIZONTAL);
 
 ccSizer->Add(10, 10);
 ccSizer->Add(ccLabel, 0, wxALIGN_CENTER);
 ccSizer->Add(10, 10);
 ccSizer->Add(ccInput, 0, wxALIGN_CENTER);
 ccSizer->Add(10, 10);
 
 wxBoxSizer* bccSizer = new wxBoxSizer(wxHORIZONTAL);
 
 bccSizer->Add(10, 10);
 bccSizer->Add(bccLabel, 0, wxALIGN_CENTER);
 bccSizer->Add(10, 10);
 bccSizer->Add(bccInput, 0, wxALIGN_CENTER);
 bccSizer->Add(10, 10);
 
 wxBoxSizer* replyToSizer = new wxBoxSizer(wxHORIZONTAL);
 
 replyToSizer->Add(10, 10);
 replyToSizer->Add(replyToLabel, 0, wxALIGN_CENTER);
 replyToSizer->Add(10, 10);
 replyToSizer->Add(replyToInput, 0, wxALIGN_CENTER);
 replyToSizer->Add(10, 10);
 
 wxBoxSizer* xMailerAndDateSizer = new wxBoxSizer(wxHORIZONTAL);
 
 xMailerAndDateSizer->Add(10, 10);
 xMailerAndDateSizer->Add(dateLabel, 0, wxALIGN_CENTER);
 xMailerAndDateSizer->Add(10, 10);
 xMailerAndDateSizer->Add(dateInput, 0, wxALIGN_CENTER);
 xMailerAndDateSizer->Add(10, 10);
 xMailerAndDateSizer->Add(xMailerLabel, 0, wxALIGN_CENTER);
 xMailerAndDateSizer->Add(10, 10);
 xMailerAndDateSizer->Add(xMailerInput, 0, wxALIGN_CENTER);
 xMailerAndDateSizer->Add(10, 10);
 
 wxBoxSizer* dataSizer = new wxBoxSizer(wxHORIZONTAL);
 
 dataSizer->Add(10, 10);
 dataSizer->Add(dataLabel, 0, wxALIGN_CENTER);
 dataSizer->Add(10, 10);
 dataSizer->Add(dataInput, 0, wxALIGN_CENTER);
 dataSizer->Add(10, 10);
 
 wxBoxSizer* subjectSizer = new wxBoxSizer(wxHORIZONTAL);
 
 subjectSizer->Add(10, 10);
 subjectSizer->Add(subjectLabel, 0, wxALIGN_CENTER);
 subjectSizer->Add(10, 10);
 subjectSizer->Add(subjectInput, 0, wxALIGN_CENTER);
 subjectSizer->Add(10, 10);
 
 wxBoxSizer* fromAndToSizer = new wxBoxSizer(wxHORIZONTAL);
 
 fromAndToSizer->Add(10, 10);
 fromAndToSizer->Add(fromLabel, 0, wxALIGN_CENTER);
 fromAndToSizer->Add(10, 10);
 fromAndToSizer->Add(fromInput, 0, wxALIGN_CENTER);
 fromAndToSizer->Add(10, 10);
 fromAndToSizer->Add(toLabel, 0, wxALIGN_CENTER);
 fromAndToSizer->Add(10, 10);
 fromAndToSizer->Add(toInput, 0, wxALIGN_CENTER);
 fromAndToSizer->Add(10, 10);
 
 wxStaticBox* statusBox = new wxStaticBox(panel, -1, "Status", wxDefaultPosition, wxDefaultSize);
 
 wxStaticBoxSizer* statusBoxSizer = new wxStaticBoxSizer(statusBox, wxHORIZONTAL);
 
 wxBoxSizer* statusSizer = new wxBoxSizer(wxHORIZONTAL);
 
 statusSizer->Add(10, 10);
 statusSizer->Add(statusInput, 1, wxGROW | wxALIGN_CENTER);
 statusSizer->Add(10, 10);
 
 statusBoxSizer->Add(statusSizer, 1, wxALIGN_CENTER);
 
 commandsSizer->Add(10, 10);
 commandsSizer->Add(serverSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(mailFromSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(rcptToSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(ccSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(bccSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(replyToSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(xMailerAndDateSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(subjectSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(fromAndToSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(dataSizer, 1, wxGROW | wxALIGN_CENTER);
 commandsSizer->Add(statusBoxSizer, 1, wxGROW | wxALIGN_CENTER);
 
 wxBoxSizer* buttonsSizer = new wxBoxSizer(wxHORIZONTAL);
 
 buttonsSizer->Add(cancelButton, 0, wxALIGN_CENTER);
 buttonsSizer->Add(clearButton, 0, wxALIGN_CENTER);
 buttonsSizer->Add(sendButton, 0, wxALIGN_CENTER);
 
 frameSizer->Add(commandsSizer, 1, wxGROW | wxALIGN_CENTER);
 frameSizer->Add(buttonsSizer, 0, wxALIGN_CENTER);
 frameSizer->Add(10, 10);
 
 frameSizer->Fit(this);
 
 CenterOnParent(wxBOTH);
 
 SetSizer(frameSizer);
 SetAutoLayout(TRUE);
 Layout();
 
 SetSize(400, 520);

 #if wxUSE_STATUSBAR
 
 CreateStatusBar(2);
 SetStatusText(_T("Ready..."));
    
 #endif
}


void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
 Close(TRUE);
}


void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxString msg;
    msg.Printf(_T("\"SMTP Client\" v1.0\n\n\n")
               _T("- Author: Stefano Pace\n\n")
               _T("- Compiled with: Dev-C++ v.4.9.9.2\n\n")
               _T("- GUI toolkit: %s"), wxVERSION_STRING);

    wxMessageBox(msg, _T("About SMTP Client"), wxOK | wxICON_INFORMATION, this);
}


void MyFrame::OnIdle(wxIdleEvent &event)
{
 //SetStatusText("Ready...");
}     


void MyFrame::OnClose(wxCommandEvent &event)
{
 this->Destroy();
}     


void MyFrame::OnClear(wxCommandEvent &event)
{
 serverInput->SetValue("");
 mailFromInput->SetValue("");
 rcptToInput->SetValue("");
 ccInput->SetValue("");
 bccInput->SetValue("");
 dateInput->SetValue("");
 xMailerInput->SetValue("");
 replyToInput->SetValue("");
 subjectInput->SetValue("");
 fromInput->SetValue("");
 toInput->SetValue("");
 dataInput->SetValue("");
 statusInput->SetValue("Not connected yet.\nPress \"Send\" when ready...");
}    


void MyFrame::OnSend(wxCommandEvent& event)
{    
 wxString serverAddress;    

 serverAddress = serverInput->GetValue();

 wxIPV4address addr;
 addr.Hostname(serverAddress);
 addr.Service(PORT);

 // Create the socket
 wxSocketClient* Socket = new wxSocketClient();

 // Set up the event handler and subscribe to most events
 Socket->SetEventHandler(*this, SOCKET_ID);
 Socket->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG | wxSOCKET_OUTPUT_FLAG);
 Socket->Notify(TRUE);

 // Wait for the connection event
 Socket->Connect(addr, FALSE);
 
 SetStatusText("Connecting...");
 statusInput->AppendText("\nTrying to connect to");
 statusInput->AppendText(serverAddress);
 statusInput->AppendText("...");
 
 Socket->WaitOnConnect(10);
 
 if (Socket->IsConnected())
         {
          wxString toSend = ""; 
          wxString inputContent; 
                                      
          Socket->Write("HELO SMTP Client v1.0 beta\r\n", 28); 
          
          statusInput->AppendText("\nClient: HELO SMTP Client v1.0 beta");
          
          Socket->WaitForRead(2); 
          
          inputContent = mailFromInput->GetValue(); 
          
          toSend += ("MAIL FROM:<");
          toSend += (inputContent);
          toSend += (">\r\n");
          
          Socket->Write(toSend, toSend.Length());
          
          statusInput->AppendText("\nClient: ");
          statusInput->AppendText(toSend);
          
          Socket->WaitForRead(2); 
          
          inputContent = rcptToInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("RCPT TO:<");
          toSend += (inputContent);
          toSend += (">\r\n");
          
          Socket->Write(toSend, toSend.Length()); 
          
          statusInput->AppendText("\nClient: ");
          statusInput->AppendText(toSend);
          
          Socket->WaitForRead(2); 
          
          Socket->Write("DATA\r\n", 6);
          
          statusInput->AppendText("\nClient: DATA");
          
          Socket->WaitForRead(2);
          
          inputContent = dateInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("Date: ");
          toSend += (inputContent);
          toSend += ("\r\n");
          
          statusInput->AppendText(toSend);
          
          Socket->Write(toSend, toSend.Length()); 
                    
          inputContent = xMailerInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("X-Mailer: ");
          toSend += (inputContent);
          toSend += ("\r\n");
          
          statusInput->AppendText(toSend);
          
          Socket->Write(toSend, toSend.Length());
          
          inputContent = replyToInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("Reply-To: ");
          toSend += (inputContent);
          toSend += ("\r\n");
          
          statusInput->AppendText(toSend);
          
          Socket->Write(toSend, toSend.Length());
          
          inputContent = ccInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("Cc: ");
          toSend += (inputContent);
          toSend += ("\r\n");
          
          statusInput->AppendText(toSend);
          
          Socket->Write(toSend, toSend.Length());
          
          inputContent = bccInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("Bcc: ");
          toSend += (inputContent);
          toSend += ("\r\n");
          
          statusInput->AppendText(toSend);
          
          Socket->Write(toSend, toSend.Length());
          
          inputContent = fromInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("From: ");
          toSend += (inputContent);
          toSend += ("\r\n");
          
          statusInput->AppendText(toSend);
          
          Socket->Write(toSend, toSend.Length());
          
          inputContent = toInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("To: ");
          toSend += (inputContent);
          toSend += ("\r\n");
          
          statusInput->AppendText(toSend);
          
          Socket->Write(toSend, toSend.Length());
          
          inputContent = subjectInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("Subject: ");
          toSend += (inputContent);
          toSend += ("\r\n");
          
          statusInput->AppendText(toSend);
          
          Socket->Write(toSend, toSend.Length());
          
          inputContent = dataInput->GetValue(); 
          
          toSend = "";
          
          toSend += ("\n");
          toSend += (inputContent);
          toSend += ("\r\n.\r\n");
          
          statusInput->AppendText("\nClient: Sending e-mail text to the server...");
          
          Socket->Write(toSend, toSend.Length()); 
                    
          Socket->WaitForRead(2); 
          
          Socket->Write("QUIT\r\n", 6); 
          
          statusInput->AppendText("\nClient: QUIT");
          
          Socket->WaitForRead(2); 
          
          Socket->Close(); 
         } 
 else
         {         
          Socket->Close();
         }
}


void MyFrame::OnSocketEvent(wxSocketEvent& event)
{
 // The socket that had the event
 wxSocketBase* sock = event.GetSocket();
 
 // Common buffer shared by the events
 wxChar buf[100] = "";
 
 switch(event.GetSocketEvent())
          {
           case wxSOCKET_CONNECTION:
                   {                    
                    SetStatusText("Connected to SMTP server...");
                    statusInput->AppendText("\n\nConnected to SMTP server...");

                    break;
                   }
                   
           case wxSOCKET_INPUT:
                   {         
                    sock->Read(buf, sizeof(buf));
                    
                    if(sock->Error()) 
                              { 
                               statusInput->AppendText("\nSocket error!");
                               
                               break; 
                              } 
                    
                    statusInput->AppendText("\nServer: ");
                    statusInput->AppendText(buf);
                    
                    break;
                   }
                   
           // The server hangs up after sending the data

           case wxSOCKET_LOST:
                   {
                    sock->Destroy();
                    
                    statusInput->AppendText("\nConnection closed.");
                    SetStatusText("Connection closed.");
                                        
                    wxString msg;
                    msg.Printf(_T("Connection closed."));
                    wxMessageBox(msg, _T("SMTP Client Info"), wxOK | wxICON_INFORMATION);
                    
                    break;
                   }
                   
           case wxSOCKET_OUTPUT:
                   {                        
                    break;
                   }
          }
}

fronda1
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Aug 30, 2006 11:35 am

Where is source code with make

Post by fronda1 » Wed Aug 30, 2006 6:53 pm

to download?

aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

Post by aquawicket » Wed Mar 12, 2008 10:01 pm

:D :D :D

This is a great peace of code.. I messed around with wxSMTP for hours today with no results. Once I found this, I knew what was going wrong. Very nice work. Thanks

ciammarica
Knows some wx things
Knows some wx things
Posts: 29
Joined: Tue Jul 04, 2006 6:49 am

Post by ciammarica » Sun Apr 13, 2008 11:10 am

Hi! I've put a Win32 executable here: http://www.stefanopace.net/downloads.php,
(compiled with Dev-C++ under Windows XP).
but source code is not available yet to download.

00061205
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Jun 16, 2008 3:43 am
Location: Beijing, China

Post by 00061205 » Mon Jul 14, 2008 3:01 am

Very nice work.
Regards,

00061205

Post Reply