📄 simmaildlg.cpp
字号:
// SimMailDlg.cpp : implementation file
//
#include "stdafx.h"
#include "process.h"
#include "SimMail.h"
#include "SimMailDlg.h"
#include "smtp.h"
#include "MailMessage.h"
#define DEBUG
#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()
/////////////////////////////////////////////////////////////////////////////
// CSimMailDlg dialog
CSimMailDlg::CSimMailDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimMailDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSimMailDlg)
m_strFrom = _T("");
m_strTo = _T("");
m_strSubject = _T("");
m_strServer = _T("");
m_strPort = _T("");
m_strText = _T("");
m_strEnText = _T("");
m_strPW = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSimMailDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSimMailDlg)
DDX_Text(pDX, IDC_FROM, m_strFrom);
DDV_MaxChars(pDX, m_strFrom, 128);
DDX_Text(pDX, IDC_TO, m_strTo);
DDV_MaxChars(pDX, m_strTo, 128);
DDX_Text(pDX, IDC_SUBJECT, m_strSubject);
DDV_MaxChars(pDX, m_strSubject, 128);
DDX_Text(pDX, IDC_SERVER, m_strServer);
DDV_MaxChars(pDX, m_strServer, 128);
DDX_Text(pDX, IDC_PORT, m_strPort);
DDV_MaxChars(pDX, m_strPort, 32);
DDX_Text(pDX, IDC_TEXT, m_strText);
DDX_Text(pDX, IDC_ENTEXT, m_strEnText);
DDX_Text(pDX, IDC_PW, m_strPW);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSimMailDlg, CDialog)
//{{AFX_MSG_MAP(CSimMailDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BSEND, OnBsend)
ON_BN_CLICKED(IDC_BQUIT, OnBquit)
ON_EN_MAXTEXT(IDC_PORT, OnMaxtextPort)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSimMailDlg message handlers
BOOL CSimMailDlg::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
// 设置端口的最大位数
SendDlgItemMessage(IDC_PORT, EM_SETLIMITTEXT, (WPARAM) 5, 0);
// 设置为加密模式
SendDlgItemMessage(IDC_JM, BM_SETCHECK, BST_CHECKED, 0);
// 设置默认的值!
m_strFrom = _T("alex@mx.cei.gov.cn");
m_strTo = _T("alex6637@msn.com");
m_strSubject= _T("恭喜你!中了大奖了!");
m_strServer = _T("smtp.cei.gov.cn");
m_strPort = _T("25");
m_strText = _T("");
m_strEnText = _T("");
m_strPW = _T("UNIXMEME");
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSimMailDlg::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 CSimMailDlg::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 CSimMailDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSimMailDlg::OnBsend()
{
// TODO: Add your control notification handler code here
// 1. 检查数据的合法性
// (包括五个单行文本框,邮件正文,加密复选框,密钥文本框)
if( UpdateData(TRUE) == 0 ) // 得到输入的数据
{
MessageBox("得不到所需的数据!", "错误!", MB_OK | MB_ICONEXCLAMATION);
return;
}
if( m_strFrom.Find('@') == -1 || m_strFrom.Find('.') == -1 )
{
MessageBox("发件人地址不正确! \n请重新输入!", "警告!", MB_OK | MB_ICONEXCLAMATION);
return;
}
if( m_strTo.Find('@') == -1 || m_strTo.Find('.') == -1 )
{
MessageBox("收件人地址不正确! \n请重新输入!", "警告!", MB_OK | MB_ICONEXCLAMATION);
return;
}
if( m_strSubject.GetLength() == 0 )
MessageBox("确认主题为空吗?", "提示:", MB_OK | MB_ICONEXCLAMATION);
if( m_strServer.Find('.') == -1 )
{
MessageBox("SMTP服务器地址不正确!\n请重新输入!", "警告!", MB_OK | MB_ICONEXCLAMATION);
return;
}
if( m_strPort.GetLength() == 0 )
{
MessageBox("端口号不能为空!", "警告!", MB_OK | MB_ICONEXCLAMATION);
return;
}
else
{
// 转换位数字
}
m_strEnText.Empty();
if( m_strText.GetLength() == 0 )
{
MessageBox("邮件正文不能为空!", "警告!", MB_OK | MB_ICONEXCLAMATION);
return;
}
if( SendDlgItemMessage(IDC_JM, BM_GETCHECK, 0, 0) != BST_CHECKED )
{
CString strError;
strError.LoadString(IDS_JMPMT);
MessageBox(strError, "警告!", MB_OK | MB_ICONEXCLAMATION);
return;
}
else {
if( m_strPW.GetLength() == 0 )
{
MessageBox("密钥不能为空!", "警告!", MB_OK | MB_ICONEXCLAMATION);
return;
}
// 加密正文!
// 1. 先将内容写入临时文件中(tempfile.dzb), 关闭文件
HANDLE hFile = CreateFile("tempfile.dzb", GENERIC_WRITE, 0, NULL, \
CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if( hFile == INVALID_HANDLE_VALUE )
{
MessageBox("加密文件错误!", "错误!", MB_OK | MB_ICONEXCLAMATION);
return;
}
DWORD dwWrite;
BOOL bFlag = FALSE;
bFlag = WriteFile(hFile, (LPVOID)(LPCTSTR)m_strText, m_strText.GetLength(), &dwWrite, NULL);
if( !bFlag || dwWrite != (unsigned int)m_strText.GetLength() )
{
MessageBox("写入文件错误", "错误!", MB_OK | MB_ICONEXCLAMATION);
return;
}
CloseHandle(hFile);
hFile = NULL;
// 2. 调用程序idea tempfile.dzb ew 密码
CString strArg = "idea.exe tempfile.dzb ew " + m_strPW;
if( system((LPCSTR)strArg) == -1 )
{
MessageBox("文件加密错误!", "错误!", MB_OK | MB_ICONEXCLAMATION);
return;
}
// 3. 读入加密后的文件tempfile.dzb, 放入m_strEntext中
hFile = CreateFile("tempfile.dzb", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if( hFile == INVALID_HANDLE_VALUE )
{
MessageBox("读入文件错误", "错误!", MB_OK | MB_ICONEXCLAMATION);
hFile = NULL;
return;
}
DWORD dwRead, dwHigh;
LPVOID buffer = (LPVOID) malloc(GetFileSize(hFile, &dwHigh));
bFlag = ReadFile(hFile, buffer, GetFileSize(hFile, &dwHigh), &dwRead, NULL);
if( !bFlag || GetFileSize(hFile, &dwHigh) != dwRead )
{
MessageBox("读入文件错误", "错误!", MB_OK | MB_ICONEXCLAMATION);
CloseHandle(hFile);
return;
}
CloseHandle(hFile);
// 删除临时文件!
DeleteFile("tempfile.dzb");
// 在IDC_ENTEXT中显示出来!
TCHAR* pBuffer = (TCHAR*)buffer;
LPVOID* sendBuffer = (LPVOID*)malloc(dwRead); // sendBuffer用于发送数据!
if(!sendBuffer)
return;
memcpy(sendBuffer, pBuffer, dwRead);
m_strEnText = _T("");
for( DWORD i = 0; i<dwRead; i++)
{
if( *(pBuffer + i) == '\0' )
* (pBuffer + i) = 0x2e;
m_strEnText += *(pBuffer + i);
}
SendDlgItemMessage(IDC_ENTEXT, WM_SETTEXT, 0, (LPARAM)buffer);
free(pBuffer);
free(sendBuffer);
}
// 2. 创建CSMTP类对象和CMailMessage类对象
CSMTP smtp(m_strServer, atoi((LPCTSTR)m_strPort));
CMailMessage msg;
// 3. 格式化邮件头,体,收发人等等
msg.m_strFrom = m_strFrom;
msg.m_strTo = m_strTo;
msg.m_strSubject = m_strSubject;
#ifdef DEBUG
msg.m_strBody = m_strText;
#else
msg.m_strBody = m_strEnText;
#endif
// 4. 连接服务器,发送邮件,关闭与服务器的连接
if( !smtp.Connect() || \
!smtp.SendMessage(&msg) || !smtp.Disconnect() )
{
CString strError = _T("邮件发送失败!");
strError += smtp.GetLastError();
MessageBox((LPCTSTR)strError, "警告!", MB_OK | MB_ICONEXCLAMATION);
return;
}
// 5. 通知用户结果
CString strOk;
strOk.LoadString(IDS_SENDOK);
MessageBox((LPCSTR)strOk, "恭喜!", MB_OK | MB_ICONEXCLAMATION);
}
void CSimMailDlg::OnBquit()
{
// TODO: Add your control notification handler code here
// 关闭对话框
EndDialog(TRUE);
}
void CSimMailDlg::OnMaxtextPort()
{
// TODO: Add your control notification handler code here
MessageBox("端口位数不能大于5位!", "警告!", MB_OK | MB_ICONEXCLAMATION);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -