POP3SMTPDlg.cpp
资源名称:POP3SMTP.rar [点击查看]
上传用户:pyhyhg
上传日期:2022-08-11
资源大小:56k
文件大小:7k
源码类别:
Email客户端
开发平台:
Visual C++
- // POP3SMTPDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "POP3SMTP.h"
- #include "POP3SMTPDlg.h"
- //smtp
- #include "SMTP.h"
- #include "MailMessage.h"
- //pop
- #include "pop3.h"
- #include "Pop3Base4.h"
- #include "Choose.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CPOP3SMTPDlg dialog
- CPOP3SMTPDlg::CPOP3SMTPDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CPOP3SMTPDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CPOP3SMTPDlg)
- m_popserver = _T("pop3.sohu.com");
- m_pass = _T("870422");
- m_user = _T("testemail2008");
- m_smtpserver = _T("smtp.sohu.com");
- m_sub = _T("这是测试信件!");
- m_to = _T("testemail2009@sohu.com");
- m_body = _T("Hello,world!");
- m_info = _T("");
- m_del = FALSE;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CPOP3SMTPDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CPOP3SMTPDlg)
- DDX_Text(pDX, IDC_EDIT_POP, m_popserver);
- DDV_MaxChars(pDX, m_popserver, 256);
- DDX_Text(pDX, IDC_EDIT_PASS, m_pass);
- DDV_MaxChars(pDX, m_pass, 256);
- DDX_Text(pDX, IDC_EDIT_USER, m_user);
- DDV_MaxChars(pDX, m_user, 256);
- DDX_Text(pDX, IDC_EDIT_SMTP, m_smtpserver);
- DDX_Text(pDX, IDC_EDIT_SUB, m_sub);
- DDX_Text(pDX, IDC_EDIT_TO, m_to);
- DDX_Text(pDX, IDC_EDIT_BODY, m_body);
- DDX_Text(pDX, IDC_EDIT_INFO, m_info);
- DDX_Check(pDX, IDC_CHECK_DEL, m_del);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CPOP3SMTPDlg, CDialog)
- //{{AFX_MSG_MAP(CPOP3SMTPDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
- ON_BN_CLICKED(IDC_BUTTON_RECV, OnButtonRecv)
- ON_BN_CLICKED(IDC_BUTTON_DISCON, OnButtonDiscon)
- ON_BN_CLICKED(IDC_BUTTON_VIEW, OnButtonView)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPOP3SMTPDlg message handlers
- BOOL CPOP3SMTPDlg::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
- CHARFORMAT cf;
- cf.cbSize=sizeof(cf);
- cf.dwMask=CFM_COLOR | CFM_FACE;
- cf.dwEffects=0;
- cf.crTextColor=RGB(255,0,0);
- strcpy(cf.szFaceName,"Verdana");
- CRichEditCtrl* re=(CRichEditCtrl*)GetDlgItem(IDC_EDIT_INFO);
- re->SetDefaultCharFormat(cf);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // 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 CPOP3SMTPDlg::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 CPOP3SMTPDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CPOP3SMTPDlg::OnButtonSend()
- {
- // TODO: Add your control notification handler code here
- // TODO: Add your control notification handler code here
- UpdateData( TRUE );
- CSMTP smtp( m_popserver );
- CMailMessage msg;
- msg.m_sFrom = m_user+"@sohu.com";
- msg.AddMultipleRecipients( m_to );
- msg.m_sSubject = m_sub;
- msg.m_sBody = m_body;
- if( !smtp.Connect() )
- {
- AfxMessageBox( smtp.GetLastError() );
- return;
- }
- if( !smtp.SendMessage( &msg ) )
- {
- AfxMessageBox( smtp.GetLastError() );
- return;
- }
- if( !smtp.Disconnect() )
- {
- AfxMessageBox( smtp.GetLastError() );
- return;
- }
- AfxMessageBox( _T( "信件成功发送!") );
- }
- void CPOP3SMTPDlg::OnButtonRecv()
- {
- // TODO: Add your control notification handler code here
- pop3.Set(this); //set window that would receive messages
- UpdateData(TRUE);
- m_info="";
- pop3.SetProp(m_user,m_pass); //set user and pass
- pop3.DelAfterRead(m_del);
- pop3.Create();
- pop3.Connect((LPCSTR)m_popserver,110); // to a server
- UpdateData(FALSE);
- }
- void CPOP3SMTPDlg::OnButtonDiscon()
- {
- // TODO: Add your control notification handler code here
- pop3.Close();
- }
- void CPOP3SMTPDlg::Dispatch(LONG param)
- {
- CString s;
- switch(param)
- {
- case S_CONNECT: //we are ed
- m_info+="已经连接到服务器:"+m_popserver;
- m_info+="...rn";
- GetDlgItem(IDC_BUTTON_RECV)->EnableWindow(FALSE);
- GetDlgItem(IDC_BUTTON_DISCON)->EnableWindow(TRUE);
- GetDlgItem(IDC_BUTTON_VIEW)->EnableWindow(FALSE);
- break;
- //case S_NOTCON:
- // info+="无法连接到服务器:"+server;
- // info+="...rn";
- // break;
- case S_RECEIVE: //we are receiving some data
- pop3.GetLastMsg(s);
- m_info+=s;
- break;
- case S_CLOSE: //we are closing ion
- m_info+=pop3.GetError();
- m_info+="断开连接rn";
- GetDlgItem(IDC_BUTTON_RECV)->EnableWindow(TRUE);
- GetDlgItem(IDC_BUTTON_DISCON)->EnableWindow(FALSE);
- break;
- case S_GETNUMMSGS: //we can get number of messages
- {
- s.Format("共收到 %d 封信rn",pop3.GetNumMsg());
- m_info+=s;
- break;
- }
- case S_GETSIZEMSGS: //now size of messages
- {
- s.Format("大小为: %d (bytes)rn",pop3.GetSizeMsg());
- m_info+=s;
- break;
- }
- case S_ENDRETR: //we have received all of messages
- {
- s.Format("收到 %d 封信rn",pop3.GetRetrMsgNum());
- m_info+=s;
- if(pop3.GetRetrMsgNum()>0)
- GetDlgItem(IDC_BUTTON_VIEW)->EnableWindow(TRUE);
- break;
- }
- }
- UpdateData(FALSE);
- }
- void CPOP3SMTPDlg::OnButtonView()
- {
- // TODO: Add your control notification handler code here
- CChooseDlg dlg;
- dlg.DoModal();
- }