// testDlg.cpp : implementation file // #include "stdafx.h" #include "test.h" #include "testDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTestDlg dialog CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CTestDlg::IDD, pParent) { //{{AFX_DATA_INIT(CTestDlg) m_body = _T(""); m_name = _T(""); m_password = _T(""); m_pop = _T(""); m_revad = _T(""); m_sendad = _T(""); m_smtp = _T(""); m_subject = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTestDlg) DDX_Control(pDX, IDC_LIST, m_list); DDX_Text(pDX, IDC_BODY, m_body); DDX_Text(pDX, IDC_NAME, m_name); DDX_Text(pDX, IDC_PASSWORD, m_password); DDX_Text(pDX, IDC_POP, m_pop); DDX_Text(pDX, IDC_REVAD, m_revad); DDX_Text(pDX, IDC_SENDAD, m_sendad); DDX_Text(pDX, IDC_SMTP, m_smtp); DDX_Text(pDX, IDC_SUBJECT, m_subject); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTestDlg, CDialog) //{{AFX_MSG_MAP(CTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_SEND, OnSend) ON_BN_CLICKED(IDC_ADD, OnAdd) ON_BN_DOUBLECLICKED(IDC_ADD, OnDoubleclickedAdd) ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnDblclkList) ON_BN_CLICKED(IDC_REV, OnRev) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTestDlg message handlers BOOL CTestDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CTestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CTestDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CTestDlg::OnSend() { // TODO: Add your control notification handler code here //控件状态的可用性设定 GetDlgItem(IDOK)->EnableWindow(TRUE); GetDlgItem(IDC_NAME)->EnableWindow(TRUE); GetDlgItem(IDC_SENDAD)->EnableWindow(TRUE); GetDlgItem(IDC_PASSWORD)->EnableWindow(TRUE); GetDlgItem(IDC_SMTP)->EnableWindow(TRUE); GetDlgItem(IDC_POP)->EnableWindow(FALSE); GetDlgItem(IDC_REVAD)->EnableWindow(TRUE); GetDlgItem(IDC_SUBJECT)->EnableWindow(TRUE); GetDlgItem(IDC_BODY)->EnableWindow(TRUE); GetDlgItem(IDC_ADD)->EnableWindow(TRUE); GetDlgItem(IDC_LIST)->EnableWindow(FALSE); //清空所有控件内容 m_list.DeleteAllItems(); //清空附件列表 m_body =""; m_name =""; m_password =""; m_pop =""; m_revad =""; m_sendad =""; m_smtp = ""; m_subject =""; UpdateData(false); //更新控件内数据 flag=true; //记录是发信状态 } void CTestDlg::OnAdd() { // TODO: Add your control notification handler code here GetDlgItem(IDC_LIST)->EnableWindow(TRUE); CFileDialog dlg(true,NULL,NULL,OFN_ALLOWMULTISELECT); if(dlg.DoModal() == IDOK) { POSITION pos = dlg.GetStartPosition(); while(pos != NULL) { long i=m_list.GetItemCount(); //得到列表框的项的数目 //AddString(dlg.GetNextPathName(pos)); m_list.InsertItem(m_list.GetItemCount()+1,dlg.GetNextPathName(pos)); //插入项 CString str; str=m_list.GetItemText(i,0); } } } void CTestDlg::OnDoubleclickedAdd() { // TODO: Add your control notification handler code here } void CTestDlg::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here if(flag==true) if(IDOK==MessageBox("确定删除此项吗?","删除提示",MB_OKCANCEL|MB_ICONQUESTION)) { int nItem=m_list.GetNextItem (-1,LVNI_SELECTED); m_list.DeleteItem(nItem); //删除项 UpdateData(FALSE); } *pResult = 0; } void CTestDlg::OnOK() { // TODO: Add extra validation here if(flag==true) { //参数的获取与判断 UpdateData(true); m_sendad.TrimRight(); //去掉后面多余空格 m_password.TrimRight(); m_smtp.TrimRight(); m_revad.TrimRight(); m_subject.TrimRight(); m_body.TrimRight(); m_name.TrimRight(); //判断参数是否合法 if(m_revad.GetLength()<3) { AfxMessageBox("请填写正确的收件人邮箱"); return; } if(m_sendad.GetLength()<3) { AfxMessageBox("请填写正确的发信人邮箱"); return; } if(m_body=="") { AfxMessageBox("请填写正文"); return; } if(m_subject=="") { if(IDCANCEL==MessageBox("确定不填写标题吗?","提示",MB_OKCANCEL|MB_ICONQUESTION)) return; } if(m_name=="") { if(IDCANCEL==MessageBox("确定不填写昵称吗?","提示",MB_OKCANCEL|MB_ICONQUESTION)) return; } //进行发信 try { CoInitialize(NULL); // COM的初始化 jmail::IMessagePtr pMessage("JMail.Message"); pMessage->From = (LPCTSTR)m_sendad; // 发件人邮箱 pMessage->FromName =_bstr_t(m_name); // 发件人姓名 pMessage->AddRecipient((LPCTSTR)m_revad, "", ""); // 添加收件人 pMessage->Priority = 3; // 优先级设置,1~5逐次降低 // 编码方式设置, 默认是iso-8859-1 pMessage->Charset = "GB2312"; pMessage->Subject = (LPCTSTR)m_subject; //主题 pMessage->Body = (LPCTSTR)m_body; //正文 pMessage->MailServerUserName=(LPCTSTR)m_sendad; //开始发送 pMessage->MailServerPassWord=(LPCTSTR)m_password; if(m_list.GetItemCount()!=0) for(int i=0;i<=m_list.GetItemCount()-1;i++) //附件列表 { CString str; str=m_list.GetItemText(i,0); pMessage->AddAttachment(_bstr_t(str), VARIANT_FALSE, "application/octet-stream"); //添加附件 char *p=_bstr_t(str); } pMessage->Send((LPCTSTR)m_smtp, VARIANT_FALSE); pMessage.Release(); AfxMessageBox("发送成功!"); CoUninitialize(); } catch (_com_error e) //捕捉异常 { CString strErr; strErr.Format("错误信息:%s\r\n错误描述:%s", (LPCTSTR)e.ErrorMessage(), (LPCTSTR)e.Description()); AfxMessageBox(strErr); } } else { //参数的获取与判断 UpdateData(true); m_password.TrimRight(); m_revad.TrimRight(); m_pop.TrimRight(); //判断参数是否合法 if(m_sendad.GetLength()<3) { AfxMessageBox("请填写正确的收件人邮箱"); return; } if(m_password=="") { AfxMessageBox("请填写密码"); return; } if(m_pop=="") { AfxMessageBox("请填写POP服务器"); return; } //开始信件获取 CRecDlg dlg; dlg.DoModal(); } // CDialog::OnOK(); } void CTestDlg::OnRev() { // TODO: Add your control notification handler code here //更新控件的状态 GetDlgItem(IDOK)->EnableWindow(TRUE); GetDlgItem(IDC_NAME)->EnableWindow(FALSE); GetDlgItem(IDC_SENDAD)->EnableWindow(TRUE); GetDlgItem(IDC_PASSWORD)->EnableWindow(TRUE); GetDlgItem(IDC_SMTP)->EnableWindow(FALSE); GetDlgItem(IDC_POP)->EnableWindow(TRUE); GetDlgItem(IDC_REVAD)->EnableWindow(FALSE); GetDlgItem(IDC_SUBJECT)->EnableWindow(FALSE); GetDlgItem(IDC_BODY)->EnableWindow(FALSE); GetDlgItem(IDC_ADD)->EnableWindow(FALSE); GetDlgItem(IDC_LIST)->EnableWindow(FALSE); //清空所有控件内容 m_list.DeleteAllItems(); //清空附件列表 m_body =""; m_name =""; m_password =""; m_pop =""; m_revad =""; m_sendad =""; m_smtp = ""; m_subject =""; UpdateData(false); flag=false; //记录是收信状态 }