www.pudn.com > MailAgent.rar > MailAgentDlg.cpp


// MailAgentDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "MailAgent.h" 
#include "MailAgentDlg.h" 
#include "DlgAttachments.h" 
#include "SMTP.h" 
#include "MIMEMessage.h" 
#include "POP3.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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMailAgentDlg dialog 
 
CMailAgentDlg::CMailAgentDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMailAgentDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CMailAgentDlg) 
	m_STMP = _T(""); 
	m_To = _T(""); 
	m_From = _T(""); 
	m_Subject = _T(""); 
	m_POP3 = _T(""); 
	m_User = _T(""); 
	m_Body = _T(""); 
	m_Password = _T(""); 
	m_MN = 0; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CMailAgentDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CMailAgentDlg) 
	DDX_Control(pDX, IDC_TO, m_ctlTo); 
	DDX_Text(pDX, IDC_SMTP, m_STMP); 
	DDX_Text(pDX, IDC_TO, m_To); 
	DDX_Text(pDX, IDC_FROM, m_From); 
	DDX_Text(pDX, IDC_SUBJECT, m_Subject); 
	DDX_Text(pDX, IDC_POP3, m_POP3); 
	DDX_Text(pDX, IDC_USER, m_User); 
	DDX_Text(pDX, IDC_BODY, m_Body); 
	DDX_Text(pDX, IDC_PASSWORD, m_Password); 
	DDX_Text(pDX, IDC_MSGNO, m_MN); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CMailAgentDlg, CDialog) 
	//{{AFX_MSG_MAP(CMailAgentDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_SEND, OnSend) 
	ON_BN_CLICKED(IDC_RETR, OnRetr) 
	ON_BN_CLICKED(IDC_DELE, OnDele) 
	ON_BN_CLICKED(IDC_ATTACH, OnAttach) 
	ON_BN_CLICKED(IDC_STATUS, OnStatus) 
	 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMailAgentDlg message handlers 
 
BOOL CMailAgentDlg::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 CMailAgentDlg::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 CMailAgentDlg::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 CMailAgentDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CMailAgentDlg::OnSend()  
{ 
	// TODO: Add your control notification handler code here 
	CSMTP smtp( "" ); 
	CMIMEMessage msg; 
	 
	UpdateData( TRUE ); 
	smtp.SetServerProperties( m_STMP ); 
	msg.m_sFrom = m_From; 
	msg.AddMultipleRecipients( m_To ); 
	msg.m_sSubject = m_Subject; 
	msg.m_sBody = m_Body; 
	for( int lp = 0; lp <= m_Files.GetUpperBound(); lp++ ) 
	{ 
		msg.AddMIMEPart( m_Files[ lp ] ); 
	} 
	if( !smtp.Connect() ) 
	{ 
		AfxMessageBox( smtp.GetLastError() ); 
		return; 
	} 
	if( !smtp.SendMessage( &msg ) ) 
	{ 
		AfxMessageBox( smtp.GetLastError() ); 
		smtp.Disconnect(); 
		return; 
	} 
	smtp.Disconnect(); 
	AfxMessageBox( _T( "信件成功发送!" ) ); 
	m_To = _T( "" ); 
	m_Subject = _T( "" ); 
	m_Body = _T( "" ); 
	UpdateData( FALSE ); 
	m_ctlTo.SetFocus();	 
} 
 
void CMailAgentDlg::OnRetr()  
{ 
	// TODO: Add your control notification handler code here 
    //_________________HP_BEGIN_________________ 
	//CPOP3 myPOP3(_T("pop.bj.cnuninet.net"),110,_T("zhong@xjwl.cnuninet.net "), 
	//		_T("96047")); 
	CPOP3 myPOP3(_T("pop.21cn.com"),110,_T("helspy"), 
			_T("90132wu")); 
	//连接POP3服务器 
	if(!myPOP3.Connect()){ 
		AfxMessageBox(_T("连接POP3服务器失败!")); 
	}else{ 
		int iMsgNum=myPOP3.GetNumMessages(); 
		if(iMsgNum>0){ 
			for(int i=1;i<=iMsgNum;i++){ 
				if(!myPOP3.DeleteMessage(i))i--; 
			} 
			AfxMessageBox(_T("邮件删除完毕!")); 
		}else{ 
			AfxMessageBox(_T("邮件数目为零或出错,请重试!")); 
		} 
		myPOP3.Disconnect(); 
	} 
	//_________________HP_END____________________ 
} 
 
void CMailAgentDlg::OnDele()  
{ 
	// TODO: Add your control notification handler code here 
	//CString sInf; 
	//sInf.Format("STMP%s POP3%s 用户名%s 密码%s",m_STMP,m_POP3,m_User,m_Password); 
	AfxMessageBox(m_STMP);	 
	 
} 
 
void CMailAgentDlg::OnAttach()  
{ 
	// TODO: Add your control notification handler code here 
	CDlgAttachments dlg; 
	dlg.m_Files.RemoveAll(); 
	dlg.m_Files.Append( m_Files ); 
	if( dlg.DoModal() == IDOK ) 
	{ 
		m_Files.RemoveAll(); 
		m_Files.Append( dlg.m_Files ); 
	}	 
} 
 
void CMailAgentDlg::OnStatus()  
{ 
	// TODO: Add your control notification handler code here 
	//________________HP_BEGIN____________________ 
	//CPOP3 myPOP3(_T("pop.bj.cnuninet.net"),110,_T("zhong@xjwl.cnuninet.net "), 
	//		_T("96047")); 
	CPOP3 myPOP3(_T("pop.21cn.com"),110,_T("helspy"), 
			_T("90132wu")); 
	if(!myPOP3.Connect()){ 
		AfxMessageBox(_T("连接POP3服务器失败!")); 
	}else{ 
		int iMsgNum=myPOP3.GetNumMessages(); 
		CString sMsg; 
		sMsg.Format("邮件数目为%d",iMsgNum); 
		myPOP3.Disconnect(); 
		AfxMessageBox(sMsg); 
	} 
	//________________HP_END_______________________ 
}