// RecDlg.cpp : implementation file // #include "stdafx.h" #include "test.h" #include "RecDlg.h" #include "testDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CRecDlg dialog CRecDlg::CRecDlg(CWnd* pParent /*=NULL*/) : CDialog(CRecDlg::IDD, pParent) { //{{AFX_DATA_INIT(CRecDlg) m_body = _T(""); //}}AFX_DATA_INIT } void CRecDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CRecDlg) DDX_Control(pDX, IDC_LETTER, m_llist); DDX_Control(pDX, IDC_ATT, m_list); DDX_Text(pDX, IDC_BODY, m_body); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CRecDlg, CDialog) //{{AFX_MSG_MAP(CRecDlg) ON_BN_CLICKED(IDC_START, OnStart) ON_NOTIFY(LVN_ITEMCHANGED, IDC_LETTER, OnItemchangedLetter) ON_NOTIFY(NM_DBLCLK, IDC_ATT, OnDblclkAtt) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CRecDlg message handlers BOOL CRecDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_llist.InsertColumn(0, "发件人"); m_llist.InsertColumn(1, "主题"); m_llist.InsertColumn(2, "日期"); //获取列表的宽度 RECT rect; m_llist.GetWindowRect(&rect); int wid=rect.right-rect.left; //为每列分宽度 m_llist.SetColumnWidth(0,wid/3); m_llist.SetColumnWidth(1,wid/3); m_llist.SetColumnWidth(2,wid/3); m_list.InsertColumn(0, "附件名"); m_list.InsertColumn(1, "大小"); //获取列表的宽度 m_list.GetWindowRect(&rect); wid=rect.right-rect.left; //为每列分宽度 m_list.SetColumnWidth(0,wid/2); m_list.SetColumnWidth(1,wid/2); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CRecDlg::OnStart() { // TODO: Add your control notification handler code here CTestApp*pApp=(CTestApp*)AfxGetApp(); CTestDlg*pDlg=(CTestDlg*)pApp->m_pMainWnd; m_add=pDlg->m_sendad; m_password=pDlg->m_password; m_pop=pDlg->m_pop; try { CoInitialize(NULL); jmail::IPOP3Ptr pPOP3("JMail.POP3"); // 超时 60S //Connect=true; // pPOP3->Timeout = 60; // 连接邮件服务器 pPOP3->Connect((LPCTSTR)m_add,(LPCTSTR)m_password,(LPCTSTR)m_pop,110); pMessages = pPOP3->Messages; long lCount = pMessages->Count - 1; if(lCount == 0) AfxMessageBox("信箱为空"); else { for(long i= 1; i <= lCount; i++) //信件的数目 { pMessage=pMessages->Item[i]; _bstr_t bstrFrom = pMessage->From; //插入信息的来源信息 _bstr_t bstrSubject = pMessage->Subject; //插入主题 _bstr_t bstrBody = pMessage->Body; //正文 COleDateTime oleDate = pMessage->Date; //日期 int nListItem = m_llist.InsertItem(i, (const char*)bstrFrom); m_llist.SetItem(nListItem, 1, LVIF_TEXT, (const char*)bstrSubject, 0, 0, 0, NULL); // m_llist.SetItem(nListItem, 2, LVIF_TEXT, (const char*)oleDate.Format("%Y-%m-%d"), 0, 0, 0, NULL); m_BodyArray.Add((const char*)bstrBody); pMessage.Release(); } } } catch(_com_error e) //捕捉异常 { CString strErr; strErr.Format("程序运行错误\r\n错误描述:%s",(LPCTSTR)e.Description()); AfxMessageBox(strErr); } } void CRecDlg::OnItemchangedLetter(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; // TODO: Add your control notification handler code here //得到第一个列表项 POSITION pos = m_llist.GetFirstSelectedItemPosition(); if(pos) { m_list.DeleteAllItems( ); int nItem = m_llist.GetNextSelectedItem(pos); m_body = m_BodyArray.GetAt(nItem); jmail::IPOP3Ptr pPOP3("JMail.POP3"); pMessage=pMessages->Item[nItem+1]; for(int i=0;i<=pMessage->Attachments->Count-1;i++) //得到附件数目 { pAttachment=pMessage->Attachments->Item[i]; //插入附件的信息 _bstr_t bstrname=pAttachment->Name; _bstr_t bstrsize=pAttachment->Size; int nListItem = m_list.InsertItem(m_list.GetItemCount()+1,(const char*)bstrname); m_list.SetItem(nListItem, 1, LVIF_TEXT, (const char*)bstrsize, 0,0, 0, NULL); } UpdateData(FALSE); pMessage.Release(); } *pResult = 0; } void CRecDlg::OnDblclkAtt(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here int i=m_llist.GetNextItem (-1, LVNI_SELECTED); int nItem =m_list.GetNextItem (-1, LVNI_SELECTED); jmail::IPOP3Ptr pPOP3("JMail.POP3"); CString str; str=m_list.GetItemText(nItem,0); CFileDialog dlg(false,NULL,str,OFN_ALLOWMULTISELECT); //弹出文件保存对话框 if(dlg.DoModal()==IDOK) { POSITION pos=dlg.GetStartPosition(); while(pos!=NULL) { pMessage=pMessages->Item[i+1]; pAttachment=pMessage->Attachments->Item[nItem]; pAttachment->SaveToFile(_bstr_t(dlg.GetNextPathName(pos)));//下载附件 MessageBox("下载完成!",NULL,MB_OK); pMessage.Release(); } } *pResult = 0; }