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

📄 sendmaildlg.cpp

📁 本程序是用来发送电子邮件的源代码
💻 CPP
字号:
// SendMailDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SendMail.h"
#include "SendMailDlg.h"
#include "SMTP.h"
#include "MAILVIEW.h"
#include "MailModi.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()

/////////////////////////////////////////////////////////////////////////////
// CSendMailDlg dialog

CSendMailDlg::CSendMailDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSendMailDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSendMailDlg)
	m_SMTP = _T("");
	m_To = _T("");
	m_Subject = _T("");
	m_From = _T("");
	m_Body = _T("");
	m_Flag=CString("未发送");
	t_Date=_T("");
	bSend=FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSendMailDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSendMailDlg)
	DDX_Text(pDX, IDC_SMTP, m_SMTP);
	DDX_Text(pDX, IDC_TO, m_To);
	DDX_Text(pDX, IDC_SUBJECT, m_Subject);
	DDX_Text(pDX, IDC_FROM, m_From);
	DDX_Text(pDX, IDC_BODY, m_Body);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSendMailDlg, CDialog)
	//{{AFX_MSG_MAP(CSendMailDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_QUERY, OnQuery)
	ON_BN_CLICKED(IDC_OK, OnOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendMailDlg message handlers

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

void CSendMailDlg::OnSend() 
{
	CSMTP smtp( "" );
	CMailMessage msg;
	
	UpdateData( TRUE );
	smtp.SetServerProperties( m_SMTP );
	msg.m_sFrom = m_From;
	msg.AddMultipleRecipients( m_To );
	msg.m_sSubject = m_Subject;
	msg.m_sBody = m_Body;

	if( !smtp.Connect() )
	{
		AfxMessageBox( smtp.GetLastError() );
		return;
	}
	if( !smtp.SendMessage( &msg ) )
	{
		AfxMessageBox( smtp.GetLastError() );
		smtp.Disconnect();
		return;
	}
	smtp.Disconnect();
	AfxMessageBox( _T( "信件成功发送!" ) );
	m_Flag=_T("已发送");
	if (bSend==TRUE)
	{
	try
	{
//		if (db.Open("mail"))
//		{		    
			CString strSQL=CString("update mail set flag='已发送' where time='");
        	strSQL+=_T(t_Date)+CString("'");
			db.ExecuteSQL(strSQL);
			AfxMessageBox( _T( "已改变信件发送状态!" ) );
//		}
	}
	catch(CDBException* pe)
    {
		AfxMessageBox(pe->m_strError);
		if (db.IsOpen())
		{
			db.Close();
		}
		pe->Delete();
	}
	}
//	m_To = _T( "" );
	m_Subject = _T( "" );
	m_Body = _T( "" );
	UpdateData( FALSE );
}
//通过CDatabase对象连接数据库,并插入数据到sql server表中!
BOOL CSendMailDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
    BOOL bSuccess=FALSE;
	bSend=TRUE;
	UpdateData( TRUE );
	m_Date = m_Date.GetCurrentTime();
	t_Date= m_Date.Format( "%a, %d %b %y %H:%M:%S" );
    if ((m_From != _T("")) && (m_To != _T("")))
	{
	try
	{
		if (db.Open("mail"))
		{		    
			CString strSQL=CString("insert into mail values (");
			strSQL+=CString("'")+_T(m_To)+CString("',");
			strSQL+=CString("'")+_T(m_From)+CString("',");
			strSQL+=CString("'")+_T(t_Date)+CString("',");
			strSQL+=CString("'")+_T(m_Flag)+CString("',");	
			strSQL+=CString("'")+_T(m_Body)+CString("')");	
			db.ExecuteSQL(strSQL);
			AfxMessageBox( _T( "已将信件信息插入表中!" ) );
			bSuccess=TRUE;
		}
	}
	catch(CDBException* pe)
    {
		AfxMessageBox(pe->m_strError);
		if (db.IsOpen())
		{
			db.Close();
		}
		pe->Delete();
	}
	}
	else
	AfxMessageBox( _T( "收发地址不能为空!" ) );
	return bSuccess;
}

void CSendMailDlg::OnQuery() 
{
	// TODO: Add your control notification handler code here
	CMAILVIEW dlg;
	dlg.DoModal();
}

void CSendMailDlg::OnOk() 
{
   exit( 1 );
}

int CSendMailDlg::DoModal() 
{
	// TODO: Add your specialized code here and/or call the base class
	return CDialog::DoModal();
}

⌨️ 快捷键说明

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