PageMailSmtp.cpp
资源名称:Netmanag.zip [点击查看]
上传用户:geanq888
上传日期:2007-01-03
资源大小:316k
文件大小:11k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // PageMailSmtp.cpp : implementation file
- //
- #include "stdafx.h"
- #include "netmanager.h"
- #include "PageMailSmtp.h"
- #include "GlobalsExtern.h"
- #include "SMTP.h"
- #include "PageMailConfig.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CPageMailSmtp property page
- IMPLEMENT_DYNCREATE(CPageMailSmtp, CPropertyPage)
- CPageMailSmtp::CPageMailSmtp() : CPropertyPage(CPageMailSmtp::IDD)
- {
- //{{AFX_DATA_INIT(CPageMailSmtp)
- m_sFrom = _T("");
- m_sSubject = _T("");
- m_sText = _T("");
- m_sTo = _T("");
- m_bClear = FALSE;
- m_bWithDate = FALSE;
- m_bWithMailer = FALSE;
- m_bWithSignature = FALSE;
- //}}AFX_DATA_INIT
- }
- CPageMailSmtp::~CPageMailSmtp()
- {
- }
- void CPageMailSmtp::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CPageMailSmtp)
- DDX_Control(pDX, IDC_ATTACHMENTS, m_Attachments);
- DDX_Control(pDX, IDC_NUMBER_OF_CHARS, m_NumberOfChars);
- DDX_Control(pDX, IDC_TEXT, m_Text);
- DDX_CBString(pDX, IDC_FROM, m_sFrom);
- DDV_MaxChars(pDX, m_sFrom, 256);
- DDX_CBString(pDX, IDC_SUBJECT, m_sSubject);
- DDV_MaxChars(pDX, m_sSubject, 256);
- DDX_Text(pDX, IDC_TEXT, m_sText);
- DDV_MaxChars(pDX, m_sText, 64000);
- DDX_CBString(pDX, IDC_TO, m_sTo);
- DDV_MaxChars(pDX, m_sTo, 256);
- DDX_Check(pDX, IDC_CLEAR, m_bClear);
- DDX_Check(pDX, IDC_WITHDATE, m_bWithDate);
- DDX_Check(pDX, IDC_WITHMAILER, m_bWithMailer);
- DDX_Check(pDX, IDC_WITHSIGNATURE, m_bWithSignature);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CPageMailSmtp, CPropertyPage)
- //{{AFX_MSG_MAP(CPageMailSmtp)
- ON_BN_CLICKED(IDB_SEND, OnSend)
- ON_BN_CLICKED(IDB_SETTINGS, OnSettings)
- ON_EN_CHANGE(IDC_TEXT, OnChangeText)
- ON_BN_CLICKED(IDC_WITHSIGNATURE, OnWithSignature)
- ON_BN_CLICKED(IDB_ATTACHMENTS, OnAttachments)
- ON_CBN_SETFOCUS(IDC_ATTACHMENTS, OnSetfocusAttachments)
- ON_CBN_EDITUPDATE(IDC_ATTACHMENTS, OnEditupdateAttachments)
- ON_CBN_SELCHANGE(IDC_ATTACHMENTS, OnSelchangeAttachments)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPageMailSmtp message handlers
- BOOL CPageMailSmtp::OnInitDialog()
- {
- CPropertyPage::OnInitDialog();
- m_ToolTip.Create(this);
- m_ToolTip.Activate(TRUE);
- CWnd* pWnd = GetWindow(GW_CHILD);
- while(pWnd)
- {
- int nID = pWnd->GetDlgCtrlID();
- if (nID != -1)
- m_ToolTip.AddTool(pWnd, pWnd->GetDlgCtrlID());
- pWnd = pWnd->GetWindow(GW_HWNDNEXT);
- }
- m_Subject.SubclassDlgItem(IDC_SUBJECT, this);
- m_From.SubclassDlgItem(IDC_FROM, this);
- m_To.SubclassDlgItem(IDC_TO, this);
- m_DelCurrent.SubclassDlgItem(IDB_DELCURRENT, this);
- OnChangeText();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::OnSend()
- {
- UpdateData();
- g_AnimateWait->Play(0, -1, -1);
- CString sFromName;
- CString sFromEmail;
- CString sToName;
- CString sToEmail;
- int nFrom = m_sFrom.Find(_T('<'));
- if(nFrom != -1)
- {
- sFromName = m_sFrom.Left(nFrom);
- sFromEmail = m_sFrom.Right(m_sFrom.GetLength() - nFrom);
- sFromEmail = sFromEmail.Mid(1, sFromEmail.GetLength() - 2);
- }
- else
- {
- sFromEmail = m_sFrom;
- }
- int nTo = m_sTo.Find(_T('<'));
- if(nTo != -1)
- {
- sToName = m_sTo.Left(nTo);
- sToEmail = m_sTo.Right(m_sTo.GetLength() - nTo);
- sToEmail = sToEmail.Mid(1, sToEmail.GetLength() - 2);
- }
- else
- {
- sToEmail = m_sTo;
- }
- CSMTPConnection Smtp;
- if(!Smtp.Connect(m_sServer))
- {
- DWORD dwError = ::GetLastError();
- CString sResponse = Smtp.GetLastCommandResponse();
- g_WriteToOutput(TRUE, "[Mail] Failed to connect to SMTP server" + sResponse);
- return;
- }
- CSMTPMessage Message(m_sXMailer, m_bWithDate, m_bWithMailer);
- CSMTPAddress From(sFromName, sFromEmail);
- Message.m_From = From;
- CSMTPAddress To(sToName, sToEmail);
- Message.AddRecipient(To);
- Message.m_sSubject = m_sSubject;
- Message.AddBody(m_sText);
- // Message.m_ReplyTo = CSMTPAddress(_T(""), _T("aa@aaa.com"));
- CArray<CSMTPAttachment*, CSMTPAttachment*&> apAttachments;
- CString sAttachment;
- CSMTPAttachment* pAttachment;
- int nAttachments = m_Attachments.GetCount();
- if(nAttachments != CB_ERR && nAttachments != 0)
- {
- for(int i = 0; i < nAttachments; i++)
- {
- pAttachment = new CSMTPAttachment;
- apAttachments.Add(pAttachment);
- m_Attachments.GetLBText(i, sAttachment);
- pAttachment->Attach(sAttachment);
- Message.AddAttachment(pAttachment);
- }
- }
- if(!Smtp.SendMessage(Message))
- {
- DWORD dwError = ::GetLastError();
- CString sResponse = Smtp.GetLastCommandResponse();
- g_WriteToOutput(TRUE, "[Mail] Failed to send the SMTP message" + sResponse);
- return;
- }
- else
- {
- g_WriteToHistory(TRUE, "[Mail] Sent mail to " + m_sTo);
- if(m_bClear)
- {
- m_Text.SetSel(0, -1);
- m_Text.Clear();
- m_To.SetEditSel(0, -1);
- m_To.Clear();
- m_Subject.SetEditSel(0, -1);
- m_Subject.Clear();
- m_Attachments.ResetContent();
- }
- }
- Smtp.Disconnect();
- nAttachments = apAttachments.GetSize();
- for(int i = 0; i < nAttachments; i++)
- delete apAttachments.GetAt(i);
- apAttachments.RemoveAll();
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::OnAttachments()
- {
- CFileDialog BrowseDialog(TRUE);
- if(BrowseDialog.DoModal() == IDOK)
- {
- CString sPath = BrowseDialog.GetPathName();
- if(!sPath.IsEmpty())
- {
- m_Attachments.EnableWindow(TRUE);
- m_Attachments.SetWindowText(sPath);
- m_Attachments.AddString(sPath);
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::OnSettings()
- {
- CPageMailConfig MailConfig;
- MailConfig.m_sSignature = m_sSignature;
- MailConfig.m_sServer = m_sServer;
- MailConfig.m_sXMailer = m_sXMailer;
- if(MailConfig.DoModal() == IDOK)
- {
- m_sSignature = MailConfig.m_sSignature;
- m_sServer = MailConfig.m_sServer;
- m_sXMailer = MailConfig.m_sXMailer;
- OnChangeText();
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::OnChangeText()
- {
- // TODO: If this is a RICHEDIT control, the control will not
- // send this notification unless you override the CPropertyPage::OnInitDialog()
- // function to send the EM_SETEVENTMASK message to the control
- // with the ENM_CHANGE flag ORed into the lParam mask.
- // TODO: Add your control notification handler code here
- UpdateData();
- int nLength = m_sText.GetLength();
- if(m_bWithSignature)
- nLength += m_sSignature.GetLength();
- CString sLength;
- sLength.Format("%d", nLength);
- m_NumberOfChars.SetWindowText(sLength);
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::OnWithSignature()
- {
- OnChangeText();
- }
- /////////////////////////////////////////////////////////////////////////////
- BOOL CPageMailSmtp::PreTranslateMessage(MSG* pMsg)
- {
- // transate the message based on TTM_WINDOWFROMPOINT
- MSG msg = *pMsg;
- msg.hwnd = (HWND)m_ToolTip.SendMessage(TTM_WINDOWFROMPOINT, 0, (LPARAM)&msg.pt);
- CPoint pt = pMsg->pt;
- if (msg.message >= WM_MOUSEFIRST && msg.message <= WM_MOUSELAST)
- ::ScreenToClient(msg.hwnd, &pt);
- msg.lParam = MAKELONG(pt.x, pt.y);
- // Let the ToolTip process this message.
- m_ToolTip.RelayEvent(&msg);
- if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
- {
- CWnd* pCurrent = GetFocus()->GetOwner();
- int nCurrent = pCurrent->GetDlgCtrlID();
- if(nCurrent == IDC_FROM
- || nCurrent == IDC_TO
- //|| nCurrent == IDC_SERVER
- || nCurrent == IDC_SUBJECT
- //|| nCurrent == IDC_XMAILER
- )
- {
- CString sCurrent;
- pCurrent->GetWindowText(sCurrent);
- if(sCurrent != "")
- {
- ((CComboBox*)pCurrent)->InsertString(-1, sCurrent);
- g_WriteToHistory(TRUE, "[Mail] Added " + sCurrent);
- ((CEdit*)GetFocus())->SetSel(0, -1);
- }
- }
- if(GetKeyState(VK_CONTROL) & 0x8000)
- {
- pMsg->message ^= WM_KEYDOWN;
- int nResult = MessageBox("Send mail?", NULL, MB_ICONQUESTION | MB_YESNO);
- if(nResult != IDNO)
- OnSend();
- }
- }
- return CPropertyPage::PreTranslateMessage(pMsg);
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::OnSetfocusAttachments()
- {
- CString sText;
- m_Attachments.GetWindowText(sText);
- if(sText.IsEmpty() && m_Attachments.GetCount() == 0)
- m_Attachments.EnableWindow(FALSE);
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::OnEditupdateAttachments()
- {
- OnSetfocusAttachments();
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::OnSelchangeAttachments()
- {
- OnSetfocusAttachments();
- }
- /////////////////////////////////////////////////////////////////////////////
- void CPageMailSmtp::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- { // storing code
- UpdateData();
- ar << m_sSignature;
- ar << m_sServer;
- ar << m_sXMailer;
- ar << m_bClear;
- ar << m_bWithDate;
- ar << m_bWithSignature;
- ar << m_bWithMailer;
- ar << m_sFrom;
- int i, j;
- CString sText;
- j = m_From.GetCount();
- ar << j;
- for(i = 0; i < j; i++)
- {
- m_From.GetLBText(i, sText);
- ar << sText;
- }
- j = m_To.GetCount();
- ar << j;
- for(i = 0; i < j; i++)
- {
- m_To.GetLBText(i, sText);
- ar << sText;
- }
- j = m_Subject.GetCount();
- ar << j;
- for(i = 0; i < j; i++)
- {
- m_Subject.GetLBText(i, sText);
- ar << sText;
- }
- }
- else
- { // loading code
- ar >> m_sSignature;
- ar >> m_sServer;
- ar >> m_sXMailer;
- ar >> m_bClear;
- ar >> m_bWithDate;
- ar >> m_bWithSignature;
- ar >> m_bWithMailer;
- ar >> m_sFrom;
- int i, j;
- CString sText;
- ar >> j;
- for(i = 0; i < j; i++)
- {
- ar >> sText;
- m_From.AddString(sText);
- }
- ar >> j;
- for(i = 0; i < j; i++)
- {
- ar >> sText;
- m_To.AddString(sText);
- }
- ar >> j;
- for(i = 0; i < j; i++)
- {
- ar >> sText;
- m_Subject.AddString(sText);
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////