00001 /* 00002 * Purpose: private wxWindows mail transport implementation 00003 * Author: Frank Buß 00004 * Created: 2002 00005 */ 00006 00007 #include <wx/sckstrm.h> 00008 00009 #include "smtp.h" 00010 #include "states.h" 00011 00012 #define SOCKET_ID 1 00013 00017 wxSMTPListener g_nullListener; 00018 00019 00020 wxSMTP::wxSMTP(wxSMTPListener* pListener) : 00021 m_pMailState(&g_initialState) 00022 { 00023 if (pListener) { 00024 m_pListener = pListener; 00025 } else { 00026 m_pListener = &g_nullListener; 00027 } 00028 m_pMessage = NULL; 00029 } 00030 00031 wxSMTP::~wxSMTP() 00032 { 00033 } 00034 00035 void wxSMTP::EvaluateLine(const wxString& line) 00036 { 00037 // TODO: implementing response timeout somewhere 00038 // TODO: implementing multiline response 00039 00040 // get command 00041 unsigned long cmd = 0; 00042 line.ToULong(&cmd); 00043 m_pMailState->onResponse(*this, cmd); 00044 } 00045 00046 void wxSMTP::OnConnect(wxSocketEvent& event) 00047 { 00048 m_pMailState->onConnect(*this, event); 00049 } 00050 00051 void wxSMTP::Send(wxEmailMessage* pMessage) 00052 { 00053 // no message means nothing to do 00054 if (!pMessage) return; 00055 m_pMessage = pMessage; 00056 00057 // add recipients from message 00058 wxRecipientsIterator ri = m_pMessage->GetRecipientsIterator(); 00059 while (ri.HasNext()) m_recipients.Add(ri.GetNext()); 00060 m_count = 0; 00061 00062 // init new socket connection 00063 m_pMailState = &g_initialState; 00064 Connect(); 00065 } 00066 00067 void wxSMTP::ChangeState(const MailState& mailState) 00068 { 00069 m_pMailState = &mailState; 00070 } 00071 00072 void wxSMTP::SendNextRecipient() 00073 { 00074 if (m_count < m_recipients.GetCount()) { 00075 m_currentRecipient = m_recipients[m_count++]; 00076 Write("RCPT TO:<" + m_currentRecipient + ">\x00d\x00a"); 00077 } else { 00078 ChangeState(g_startDataState); 00079 Write("DATA\x00d\x00a"); 00080 } 00081 } 00082 00083 void wxSMTP::OnRecipientError() 00084 { 00085 m_pListener->OnRecipientAdded(m_currentRecipient, 0); 00086 } 00087 00088 void wxSMTP::OnRecipientSuccess() 00089 { 00090 m_pListener->OnRecipientAdded(m_currentRecipient, 1); 00091 } 00092 00093 void wxSMTP::OnDataSuccess() 00094 { 00095 m_pListener->OnDataSent(1); 00096 } 00097 00098 void wxSMTP::SendFrom() 00099 { 00100 Write("MAIL FROM:<" + m_pMessage->GetFrom() + ">\x00d\x00a"); 00101 } 00102 00103 void wxSMTP::SendData() 00104 { 00105 wxSocketOutputStream out(*this); 00106 // wxFileOutputStream out("mail.txt"); 00107 m_pMessage->Encode(out); 00108 } 00109