⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 speedpostemaildlg.cpp

📁 邮件终端发送程序
💻 CPP
字号:
// SpeedPostEmailDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SpeedPostEmail.h"
#include "SpeedPostEmailDlg.h"
#include "dnsmx.h"
#include "HwSMTP.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()

/////////////////////////////////////////////////////////////////////////////
// CMySpeedPostEmailDlg dialog

CMySpeedPostEmailDlg::CMySpeedPostEmailDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMySpeedPostEmailDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMySpeedPostEmailDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMySpeedPostEmailDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMySpeedPostEmailDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMySpeedPostEmailDlg, CDialog)
	//{{AFX_MSG_MAP(CMySpeedPostEmailDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMBO_NetAdapter, OnSelchangeCOMBONetAdapter)
	ON_BN_CLICKED(IDC_BUTTON_Release, OnBUTTONRelease)
	ON_BN_CLICKED(IDC_BUTTON_Renew, OnBUTTONRenew)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMySpeedPostEmailDlg message handlers

BOOL CMySpeedPostEmailDlg::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

	GetAllNetAdaptersOfPC (0);
	
	SetDlgItemText ( IDC_EDIT_EMailTo, _T("www.skype.org@gmail.com") );
	SetDlgItemText ( IDC_EDIT_EMailFrom, _T("chrys@viction.net") );
	SetDlgItemText ( IDC_EDIT_EMailBody, _T("这封邮件是直接通过“特快专递”传送的。") );

	return TRUE;  // return TRUE  unless you set the focus to a control
}

//
// 获取电脑网卡信息
//
void CMySpeedPostEmailDlg::GetAllNetAdaptersOfPC ( int nCursel )
{
	m_NetAdapterInfo.Refresh ();
	CComboBox *pCombo = ( CComboBox* ) GetDlgItem ( IDC_COMBO_NetAdapter );
	ASSERT ( pCombo );
	pCombo->ResetContent ();
	int nNetAdapterCount = m_NetAdapterInfo.GetNetCardCount();
	TRACE ( _T("nNetAdapterCount = %d\r\n"), nNetAdapterCount );
	
	for ( int i=0; i<nNetAdapterCount; i++ )
	{
		COneNetAdapterInfo *pOneNetAdapterInfo = m_NetAdapterInfo.Get_OneNetAdapterInfo ( i );
		if ( pOneNetAdapterInfo )
		{
			int nItem = pCombo->AddString ( pOneNetAdapterInfo->Get_Desc() );
			if ( nItem >= 0 )
				pCombo->SetItemData ( nItem, (DWORD)i );
		}
	}

	if ( pCombo->GetCount() > 0 )
	{
		if ( nCursel >= 0 && nCursel < pCombo->GetCount() )
		{
			pCombo->SetCurSel (nCursel);
		}
		else
		{
			pCombo->SetCurSel (0);
		}
		OnSelchangeCOMBONetAdapter ();
	}
}

void CMySpeedPostEmailDlg::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 CMySpeedPostEmailDlg::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 CMySpeedPostEmailDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

COneNetAdapterInfo* CMySpeedPostEmailDlg::GetCurselNetAdapter ( int *pnCursel/*=NULL*/ )
{
	CComboBox *pCombo = ( CComboBox* ) GetDlgItem ( IDC_COMBO_NetAdapter );
	ASSERT ( pCombo );
	int nCurSel = pCombo->GetCurSel ();
	if ( pnCursel ) *pnCursel = nCurSel;
	int nNetAdapterCount = m_NetAdapterInfo.GetNetCardCount();
	if ( nCurSel < 0 || nCurSel >= pCombo->GetCount() || nCurSel >= nNetAdapterCount )
		return NULL;
	int nAdapterIndex = pCombo->GetItemData ( nCurSel );
	if ( nAdapterIndex < 0 || nAdapterIndex >= nNetAdapterCount )
		return NULL;
	COneNetAdapterInfo *pOneNetAdapterInfo = m_NetAdapterInfo.Get_OneNetAdapterInfo ( nAdapterIndex );
	return pOneNetAdapterInfo;
}

void CMySpeedPostEmailDlg::OnSelchangeCOMBONetAdapter() 
{
	COneNetAdapterInfo *pOneNetAdapterInfo = GetCurselNetAdapter ();

	CString csNetAdapterInfos;
	if ( pOneNetAdapterInfo )
	{
		CString csOneAdapterInfo;
		int j = 0;
		csOneAdapterInfo.Format ( _T("Adapter Index.%d Net adapter ---------------------------------------------\r\n%s\r\n"),
			pOneNetAdapterInfo->Get_AdapterIndex(), pOneNetAdapterInfo->Get_Desc() );
		csNetAdapterInfos += csOneAdapterInfo;
		
		csOneAdapterInfo.Format ( _T("Adapter Name : %s\r\n"),
			pOneNetAdapterInfo->Get_Name() );
		csNetAdapterInfos += csOneAdapterInfo;
		
		csOneAdapterInfo.Format ( _T("Type : %s - %s\r\n"),
			pOneNetAdapterInfo->GetAdapterTypeString(), pOneNetAdapterInfo->Is_DHCP_Used()?_T("Dynamic Address (DHCP)"):_T("Static IP") );
		csNetAdapterInfos += csOneAdapterInfo;
		
		for ( j=0; j<pOneNetAdapterInfo->Get_IPCount(); j++ )
		{
			csOneAdapterInfo.Format ( _T("IP.%d : %s\tSubnet : %s\r\n"),
				j+1, pOneNetAdapterInfo->Get_IPAddr (j), pOneNetAdapterInfo->Get_Subnet (j));
			csNetAdapterInfos += csOneAdapterInfo;
		}
		
		for ( j=0; j<pOneNetAdapterInfo->Get_DNSCount(); j++ )
		{
			csOneAdapterInfo.Format ( _T("DNS.%d : %s\r\n"),
				j+1, pOneNetAdapterInfo->Get_DNSAddr (j));
			csNetAdapterInfos += csOneAdapterInfo;
		}
		
		for ( j=0; j<pOneNetAdapterInfo->Get_GatewayCount(); j++ )
		{
			csOneAdapterInfo.Format ( _T("Gateway.%d : %s\r\n"),
				j+1, pOneNetAdapterInfo->Get_GatewayAddr (j));
			csNetAdapterInfos += csOneAdapterInfo;
		}
		
		CTime cTime;
		if( pOneNetAdapterInfo->Is_DHCP_Used() )
		{
			csOneAdapterInfo.Format ( _T("DHCP Server : %s\r\n"),
				pOneNetAdapterInfo->Get_DHCPAddr ());
			csNetAdapterInfos += csOneAdapterInfo;
			
			cTime = pOneNetAdapterInfo->Get_LeaseObtained ();
			csOneAdapterInfo.Format ( _T("Lease Obtained : %s\r\n"),
				cTime.Format ( _T("%Y-%m-%d %H:%M:%S") ) );
			csNetAdapterInfos += csOneAdapterInfo;
			
			cTime = pOneNetAdapterInfo->Get_LeaseExpired ();
			csOneAdapterInfo.Format ( _T("Lease Expires : %s\r\n"),
				cTime.Format ( _T("%Y-%m-%d %H:%M:%S") ) );
			csNetAdapterInfos += csOneAdapterInfo;
		}
		
		csOneAdapterInfo.Format ( _T("WINS Enabled : %s\r\n"),
			pOneNetAdapterInfo->Is_Wins_Used()?_T("Yes"):_T("No") );
		csNetAdapterInfos += csOneAdapterInfo;
		
		if ( pOneNetAdapterInfo->Is_Wins_Used() )
		{
			csOneAdapterInfo.Format ( _T("Primary WINS Server : %s\r\n"),
				pOneNetAdapterInfo->Get_PrimaryWinsServer() );
			csNetAdapterInfos += csOneAdapterInfo;
			
			csOneAdapterInfo.Format ( _T("Secondary WINS Server : %s\r\n"),
				pOneNetAdapterInfo->Get_SecondaryWinsServer() );
			csNetAdapterInfos += csOneAdapterInfo;
		}
		
		csOneAdapterInfo.Format ( _T("Current IP : %s\r\n"),
			pOneNetAdapterInfo->Get_CurrentIP() );
		csNetAdapterInfos += csOneAdapterInfo;

		csOneAdapterInfo.Format ( _T("MAC Address : %s\r\n"),
			pOneNetAdapterInfo->Get_PhysicalAddressStr() );
		csNetAdapterInfos += csOneAdapterInfo;
		
		csNetAdapterInfos += _T("\r\n");
	}
	
	csNetAdapterInfos += _T("\r\n");
	csNetAdapterInfos += NOTE_SpeedPostMail;
	SetDlgItemText ( IDC_EDIT_NetAdapterInfo, csNetAdapterInfos );
	
}

void CMySpeedPostEmailDlg::OnBUTTONRelease() 
{
	int nCurSel = 0;
	COneNetAdapterInfo *pOneNetAdapterInfo = GetCurselNetAdapter ( &nCurSel );
	if ( !pOneNetAdapterInfo ) return;

	CString sMsg;
	if( pOneNetAdapterInfo->ReleaseIP() )
	{
		sMsg.Format( _T("All address information for [ %s ] has been released."), pOneNetAdapterInfo->Get_Desc() );
		GetAllNetAdaptersOfPC (nCurSel);
		AfxMessageBox( sMsg, MB_OK | MB_ICONINFORMATION );
	}
	else
	{
		AfxMessageBox( _T("Failed to release adapater addresses."), MB_OK | MB_ICONERROR );
	}
	return;
}

void CMySpeedPostEmailDlg::OnBUTTONRenew() 
{
	int nCurSel = 0;
	COneNetAdapterInfo *pOneNetAdapterInfo = GetCurselNetAdapter ( &nCurSel );
	if ( !pOneNetAdapterInfo ) return;
	
	CString sMsg;
	if( pOneNetAdapterInfo->RenewIP() )
	{
		sMsg.Format( _T("All address information for [ %s ] has been renewed."), pOneNetAdapterInfo->Get_Desc() );
		GetAllNetAdaptersOfPC (nCurSel);
		AfxMessageBox( sMsg, MB_OK | MB_ICONINFORMATION );
	}
	else
	{
		AfxMessageBox( _T("Failed to renewed adapater addresses."), MB_OK | MB_ICONERROR );
	}
	return;
}

BOOL CMySpeedPostEmailDlg::SendEMail ( CString csReceiverServer )
{
	CString csEMailTo, csEMailFrom, csEMailBody;
	GetDlgItemText ( IDC_EDIT_EMailTo, csEMailTo );
	GetDlgItemText ( IDC_EDIT_EMailFrom, csEMailFrom );
	GetDlgItemText ( IDC_EDIT_EMailBody, csEMailBody );

	TCHAR szComputeName[128] = {0};
	DWORD dwSize = sizeof(szComputeName)/sizeof(szComputeName[0]);
	GetComputerName ( szComputeName, &dwSize );
	if ( lstrlen(szComputeName) < 1 ) lstrcpy ( szComputeName, _T("Error Computer Name") );

	CString csSMTPServer, csUserName, csPasswd, csSenderEmail, csReceiverEmail, csSenderName, csReceiverName, csSubject, csBody;
	csSMTPServer = csReceiverServer;
	csSenderEmail = csEMailFrom;
	csReceiverEmail = csEMailTo;
	csSenderName.Format ( _T("\"%s\" <%s>"), szComputeName, csSenderEmail );
	csReceiverName.Format ( _T("\"%s\" <%s>"), _T("谢红伟"), csReceiverEmail );
	csSubject.Format ( _T("[%s] - Test [%s]"), szComputeName, theApp.m_pszAppName );
	csBody = csEMailBody;
	
	CWaitCursor WaitCursor;
	BOOL bRet = ::SendEmail (
		FALSE,
		csSMTPServer,				// 发送邮件的服务器地址
		csUserName,					// 邮箱的用户名
		csPasswd,					// 邮箱的密码
		FALSE,
		csSenderEmail,				// 发送者邮箱地址
		csReceiverEmail,			// 接收者邮箱地址
		csSenderName,				// 发送者姓名
		csReceiverName,				// 接收者名字
		csSubject,					// 邮件主题
		csBody						// 邮件正文
		);
	WaitCursor.Restore ();

	return bRet;
}

int FindFasterMXHost ( t_Ary_MXHostInfos &Ary_MXHostInfos )
{
	if ( Ary_MXHostInfos.GetSize() < 1 ) return -1;

	t_MXHostInfo &MXHostInfo = Ary_MXHostInfos.GetAt(0);
	int nMinN = MXHostInfo.N;
	int nIndex = 0;
	for ( int i=1; i<Ary_MXHostInfos.GetSize(); i++ )
	{
		t_MXHostInfo &MXHostInfo = Ary_MXHostInfos.GetAt(i);
		if ( MXHostInfo.N < nMinN )
		{
			nIndex = i;
			nMinN = MXHostInfo.N;
		}
	}

	return nIndex;
}

void CMySpeedPostEmailDlg::OnOK()
{
	CString csEMailTo;
	GetDlgItemText ( IDC_EDIT_EMailTo, csEMailTo );
	int nFindPos = csEMailTo.Find ( _T("@"), 0 );
	if ( nFindPos <= 0 || nFindPos >= csEMailTo.GetLength()-1 )
	{
		AfxMessageBox ( _T("E-Mail address is illegal.") );
		GetDlgItem(IDC_EDIT_EMailTo)->SetFocus ();
		return;
	}
	CString csReceiverServer = csEMailTo.Mid ( nFindPos+1 );

	t_Ary_MXHostInfos Ary_MXHostInfos;
	if ( !GetMX ( csReceiverServer.GetBuffer(0), Ary_MXHostInfos ) ||
		Ary_MXHostInfos.GetSize() < 1 )
	{
		AfxMessageBox ( FormatString(_T("Parse email (%s) MX host failed."), csReceiverServer) );
		return;
	}

	BOOL bSendEMailSuccess = FALSE;
	while ( Ary_MXHostInfos.GetSize() > 0 )
	{
		nFindPos = FindFasterMXHost ( Ary_MXHostInfos );
		if ( nFindPos < 0 ) break;
		t_MXHostInfo &MXHostInfo = Ary_MXHostInfos.GetAt(nFindPos);
		Ary_MXHostInfos.RemoveAt (nFindPos);
		if ( SendEMail ( MXHostInfo.szMXHost ) )
		{
			bSendEMailSuccess = TRUE;
			break;
		}
	}

	if ( bSendEMailSuccess )
		AfxMessageBox ( _T("Send E-Mail successfully."), MB_ICONINFORMATION );
	else
		AfxMessageBox ( _T("Send E-Mail failed."), MB_ICONWARNING );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -