📄 smtpemaildlg.cpp
字号:
// SMTPEMailDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SMTPEMail.h"
#include "SMTPEMailDlg.h"
#include "AttachmentsDlg.h"
#include "MIMEMessage.h"
#include "SMTP.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 };
CString m_strEditHelp;
//}}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)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
m_strEditHelp = _T("");
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
DDX_Text(pDX, IDC_EDIT_HELP, m_strEditHelp);
//}}AFX_DATA_MAP
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_strEditHelp=
"相关帮助信息:"
CRLF
CRLF
" 一、 发送邮件服务器和发信人信箱有密切关系,假如你的信箱为loop@sohu.com,则发送邮件服务器一般为:smtp.sohu.com,当然这也不是绝对的,你可以到你的邮件相关网站查询。"
CRLF
CRLF
" 二、 发信信箱格式比较灵活,如下三种格式均为合法的:1 在你的E-Mail首尾加小于大于符号:<loomman@sina.com> 2 直接填写你的E-Mail:loomman@sina.com 3 仅仅填写你的E-Mail帐号:loomman"
CRLF
CRLF
" 三、 接受信箱可以为多个,中间用分号;隔开,例如:Pookie<pookie@sina.com.cn>;snuggles<snuggles@sohu.com>;<loomman@hotmail.com> 某些信箱前面的部分字符串是收信人名称,例如上面的Pookie可以省略不写,例如下面一行和上面一行是等价的,它们都是可以接受的信箱输入格式。<pookie@sina.com.cn>;<snuggles@sohu.com>;<loomman@hotmail.com>"
CRLF
CRLF
" 四、 邮件主题,当然就是邮件的标题了,你可以随意输入自己喜爱的标题。"
CRLF
CRLF
" 五、 当前附件,顾名思义,就是你当前你所选中的附件了,按右边的增加附件按钮,你可以很方便的加入想要发送的附件,附件可以为多个。"
CRLF
CRLF
" 六、 为了方便大家使用,针对每个空白我都做了一些选项,这样你可以直接从下拉框中选择你需要的,节省时间。"
CRLF
CRLF
" 七、 最后不得不提一下,由于当今好多发送邮件服务器都要求身份验证,也就是需要提供的你的信箱密码,鉴于技术问题,这类信箱本程序暂时不支持。"
CRLF
CRLF
" dacylee "
CRLF
" 2008年8月8日"
CRLF
;
UpdateData(FALSE); // Update the Dialog
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSMTPEMailDlg dialog
CSMTPEMailDlg::CSMTPEMailDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSMTPEMailDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSMTPEMailDlg)
m_strEditBody = _T("");
m_from = _T("");
m_smtp = _T("");
m_subject = _T("");
m_to = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSMTPEMailDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSMTPEMailDlg)
DDX_Control(pDX, IDC_FROM, m_ctlFrom);
DDX_Control(pDX, IDC_TO, m_ctlTo);
DDX_Control(pDX, IDC_SMTP, m_ctlSmtp);
DDX_Control(pDX, IDC_EDIT_ATTACHMENTS, m_ctrlEditAttachments);
DDX_Text(pDX, IDC_EDIT_BODY, m_strEditBody);
DDX_Text(pDX, IDC_FROM, m_from);
DDX_Text(pDX, IDC_SMTP, m_smtp);
DDX_Text(pDX, IDC_SUBJECT, m_subject);
DDX_Text(pDX, IDC_TO, m_to);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSMTPEMailDlg, CDialog)
//{{AFX_MSG_MAP(CSMTPEMailDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
ON_BN_CLICKED(IDSEND, OnSend)
ON_BN_CLICKED(IDC_BUTTON_ATTACHMENTS, OnButtonAttachments)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSMTPEMailDlg message handlers
BOOL CSMTPEMailDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CSMTPEMailDlg::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 CSMTPEMailDlg::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 CSMTPEMailDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSMTPEMailDlg::OnOK()
{
}
void CSMTPEMailDlg::OnAbout()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CSMTPEMailDlg::OnSend()
{
UpdateData( TRUE );
if(m_smtp=="")
{
AfxMessageBox( _T( "发送邮件服务器不能为空!" ) );
m_ctlSmtp.SetFocus();
return;
}
if(m_from=="")
{
AfxMessageBox( _T( "发送邮箱不能为空!" ) );
m_ctlFrom.SetFocus();
return;
}
if(m_to=="")
{
AfxMessageBox( _T( "接受邮箱不能为空!" ) );
m_ctlTo.SetFocus();
return;
}
CSMTP smtp( "" );
CMIMEMessage msg;
smtp.SetServerProperties( m_smtp );
msg.m_sFrom = m_from;
msg.AddMultipleRecipients( m_to );
msg.m_sSubject = m_subject;
msg.m_sBody = m_strEditBody;
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( "邮件发送成功!" ) );
}
void CSMTPEMailDlg::OnButtonAttachments()
{
CAttachmentsDlg dlg;
dlg.m_Files.RemoveAll();
dlg.m_Files.Append( m_Files );
if( dlg.DoModal() == IDOK )
{
m_Files.RemoveAll();
m_Files.Append( dlg.m_Files );
m_ctrlEditAttachments.SetWindowText(dlg.m_strAttachments);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -