www.pudn.com > 113172215vcsendemail.zip > MailDlg.cpp


// MailDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Mail.h" 
#include "MailDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMailDlg dialog 
 
CMailDlg::CMailDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMailDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CMailDlg) 
	m_Srvr = _T(""); 
	m_Port = 0; 
	m_UserName = _T(""); 
	m_PassWord = _T(""); 
	m_MailFrom = _T(""); 
	m_RcptTo = _T(""); 
	m_Title = _T(""); 
	m_Content = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CMailDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CMailDlg) 
	DDX_Control(pDX, IDC_BUTTON1, m_Button1); 
	DDX_Control(pDX, IDC_LIST1, m_List1); 
	DDX_Text(pDX, IDC_EDIT1, m_Srvr); 
	DDX_Text(pDX, IDC_EDIT2, m_Port); 
	DDX_Text(pDX, IDC_EDIT3, m_UserName); 
	DDX_Text(pDX, IDC_EDIT4, m_PassWord); 
	DDX_Text(pDX, IDC_EDIT5, m_MailFrom); 
	DDX_Text(pDX, IDC_EDIT6, m_RcptTo); 
	DDX_Text(pDX, IDC_EDIT7, m_Title); 
	DDX_Text(pDX, IDC_EDIT8, m_Content); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CMailDlg, CDialog) 
	//{{AFX_MSG_MAP(CMailDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMailDlg message handlers 
 
BOOL CMailDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// 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 
	m_Srvr="smtp.X263.net"; 
	m_Port=25; 
	m_UserName="Whg0001"; 
	m_MailFrom="Whg0001@263.net"; 
	m_RcptTo="Whg0001@263.net"; 
	m_Title="这里是邮件标题"; 
	m_Content="这里是邮件内容"; 
	m_List1.AddString("状态信息"); 
	UpdateData(false); 
	if (HINSTANCE hLib=LoadLibrary("MyDll.Dll")) 
	{ 
	    (FARPROC&)pAnsiToBase64=GetProcAddress(hLib,"AnsiToBase64"); 
	} 
	else 
	{ 
		AfxMessageBox("不能找到MyDll.DLL,内含Ansi到Base64代码转换程序"); 
		OnOK(); 
	} 
	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 CMailDlg::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 CMailDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CMailDlg::OnButton1()  
{ 
	// TODO: Add your control notification handler code here 
	m_Button1.EnableWindow(false); 
	UpdateData(); 
	CSocket Sock; 
	Sock.Create(); 
	CString Msg; 
	Msg="***连接邮件服务器"; 
	m_List1.AddString(Msg); 
	m_List1.SetCurSel(m_List1.GetCount()-1); 
	if (Sock.Connect(m_Srvr,m_Port)) 
	{ 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="220")  
		{ 
			m_List1.AddString("***邮件服务器忙"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			return; 
		} 
		//Send HELO; 
		Msg="HELO "+m_Srvr+"\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="250")  
		{ 
			m_List1.AddString("***邮件服务器拒绝HELO"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			return; 
		} 
		//Send AUTH LOGIN; 
		Msg="AUTH LOGIN\r\n"; 
	    m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
	    Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength());		 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="334")  
		{ 
			m_List1.AddString("***邮件服务器不支持验证"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			return; 
		} 
		int nCX=this->pAnsiToBase64(Msg.GetBuffer(0x100),m_UserName.GetBuffer(0x100),m_UserName.GetLength());			 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),nCX); 
		Msg="\r\n"; 
		Sock.Send(Msg.GetBuffer(0x100),2); 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="334")  
		{ 
			m_List1.AddString("***邮件服务器不支持验证"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			return; 
		} 
        nCX=this->pAnsiToBase64(Msg.GetBuffer(0x100),m_PassWord.GetBuffer(0x100),m_PassWord.GetLength()); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),nCX); 
		Msg="\r\n"; 
		Sock.Send(Msg.GetBuffer(0x100),2); 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="235")  
		{		 
			m_List1.AddString("***验证失败"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			m_Button1.EnableWindow(); 
			return; 
		} 
		else 
		{ 
			m_List1.AddString("***验证通过"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
		} 
		//Send MAIL FROM; 
		Msg="MAIL FROM: "+m_MailFrom+"\r\n";		 
        m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="250")  
		{ 
			m_List1.AddString("***服务器不接受源信箱地址"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			m_Button1.EnableWindow(); 
			return; 
		} 
		//Send RCPT TO; 
		Msg="RCPT TO: "+m_RcptTo+"\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength());		 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="250")  
		{ 
			m_List1.AddString("***服务器不接受目的信箱地址"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			m_Button1.EnableWindow(); 
			return; 
		} 
		//Send DATA; 
		Msg="DATA\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="354")  
		{ 
			m_List1.AddString("***服务器不能接受DATA命令"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			m_Button1.EnableWindow(); 
			return; 
		} 
		//Send FROM; 
		Msg="FROM: "+m_MailFrom+"\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		//Send TO; 
		Msg="TO: "+m_RcptTo+"\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		//Send SUBJECT; 
		Msg="SUBJECT: "+m_Title+"\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		//Send Content; 
		Msg=m_Content+"\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		//Send End; 
		Msg=".\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="250")  
		{ 
			m_List1.AddString("***服务器不能接受邮件内容"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			Sock.Close(); 
			m_Button1.EnableWindow(); 
			return; 
		} 
		//Send QUIT 
		Msg="QUIT\r\n"; 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Sock.Send(Msg.GetBuffer(0x100),Msg.GetLength()); 
		Sock.Receive(Msg.GetBuffer(0x100),0x100); 
		m_List1.AddString(Msg); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
		Msg=Msg.GetBuffer(3); 
		if (Msg.Left(3)!="221")  
		{ 
			m_List1.AddString("***发送失败"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
			 
		} 
		else 
		{ 
			m_List1.AddString("***发送成功"); 
			m_List1.SetCurSel(m_List1.GetCount()-1); 
		}		 
		Sock.Close(); 
	}	 
	else 
	{ 
		m_List1.AddString("***连接邮件服务器失败"); 
		m_List1.SetCurSel(m_List1.GetCount()-1); 
	} 
	m_Button1.EnableWindow(); 
}