首页 > 代码库 > SmtpDlg 调用SMTP

SmtpDlg 调用SMTP

 

// SmtpDlg.h : 头文件//#pragma once#include "afxwin.h"#include "string"using namespace std;// CSmtpDlg 对话框class CSmtpDlg : public CDialogEx{// 构造public:    CSmtpDlg(CWnd* pParent = NULL);    // 标准构造函数// 对话框数据    enum { IDD = IDD_SMTPSENDEMAIL_DIALOG };    protected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持    void CallBackDispFunc(string strMsg);// 实现protected:    HICON m_hIcon;    // 生成的消息映射函数    virtual BOOL OnInitDialog();    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);    afx_msg void OnPaint();    afx_msg HCURSOR OnQueryDragIcon();    DECLARE_MESSAGE_MAP()public:    afx_msg void OnBnClickedCancel();    afx_msg void OnBnClickedOk();    afx_msg void OnBnClickedSendmail();    afx_msg void OnBnClickedSendmail2();    CEdit m_eHost;    CEdit m_ePost;    CEdit m_eAccount;    CEdit m_ePassword;    CEdit m_eFrom;    CEdit m_eTo;    CEdit m_eSubject;    CEdit m_eDateTime;    CEdit m_eContent;    CEdit m_eAttachment;    CEdit m_eOutput;    CString m_csHost;    CString m_csPort;    CString m_csAccount;    CString m_csPassword;    CString m_csFrom;    CString m_csTo;    CString m_csSubject;    CString m_csDateTime;    CString m_csContent;    CString m_csAttachment;    CString m_csOutput;};

 

// SmtpDlg.cpp : 实现文件//#include "stdafx.h"#include "Smtp.h"#include "SmtpDlg.h"#include "afxdialogex.h"#include "SmtpSendEmail.h"#include "..\\Mail_L\\Mail_L.h"#ifdef _DEBUG#define new DEBUG_NEW#endifvoid Split(string strSrc, char cSplit, vector<string>& vecRet){    vector<string> vecSendTo;    while(strSrc.size()>0)    {        basic_string <TCHAR>::size_type pos = strSrc.find(cSplit);        if(pos != basic_string <TCHAR>::npos)        {            vecRet.push_back(strSrc.substr(0, pos));            strSrc = strSrc.substr(pos+1);        }        else        {            vecRet.push_back(strSrc);            break;        }    }}// 用于应用程序“关于”菜单项的 CAboutDlg 对话框class CAboutDlg : public CDialogEx{public:    CAboutDlg();// 对话框数据    enum { IDD = IDD_ABOUTBOX };    protected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持// 实现protected:    DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD){}void CAboutDlg::DoDataExchange(CDataExchange* pDX){    CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()// CSmtpDlg 对话框CSmtpDlg::CSmtpDlg(CWnd* pParent /*=NULL*/)    : CDialogEx(CSmtpDlg::IDD, pParent)    , m_csHost(_T(""))    , m_csPort(_T(""))    , m_csAccount(_T(""))    , m_csPassword(_T(""))    , m_csFrom(_T(""))    , m_csTo(_T(""))    , m_csSubject(_T(""))    , m_csDateTime(_T(""))    , m_csContent(_T(""))    , m_csAttachment(_T(""))    , m_csOutput(_T("")){    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CSmtpDlg::DoDataExchange(CDataExchange* pDX){    CDialogEx::DoDataExchange(pDX);    DDX_Control(pDX, IDC_EDIT1, m_eHost);    DDX_Control(pDX, IDC_EDIT2, m_ePost);    DDX_Control(pDX, IDC_EDIT3, m_eAccount);    DDX_Control(pDX, IDC_EDIT4, m_ePassword);    DDX_Control(pDX, IDC_EDIT5, m_eFrom);    DDX_Control(pDX, IDC_EDIT6, m_eTo);    DDX_Control(pDX, IDC_EDIT7, m_eSubject);    DDX_Control(pDX, IDC_EDIT8, m_eDateTime);    DDX_Control(pDX, IDC_EDIT9, m_eContent);    DDX_Control(pDX, IDC_EDIT10, m_eAttachment);    DDX_Control(pDX, IDC_Output, m_eOutput);    DDX_Text(pDX, IDC_EDIT1, m_csHost);    DDX_Text(pDX, IDC_EDIT2, m_csPort);    DDX_Text(pDX, IDC_EDIT3, m_csAccount);    DDX_Text(pDX, IDC_EDIT4, m_csPassword);    DDX_Text(pDX, IDC_EDIT5, m_csFrom);    DDX_Text(pDX, IDC_EDIT6, m_csTo);    DDX_Text(pDX, IDC_EDIT7, m_csSubject);    DDX_Text(pDX, IDC_EDIT8, m_csDateTime);    DDX_Text(pDX, IDC_EDIT9, m_csContent);    DDX_Text(pDX, IDC_EDIT10, m_csAttachment);    DDX_Text(pDX, IDC_Output, m_csOutput);}BEGIN_MESSAGE_MAP(CSmtpDlg, CDialogEx)    ON_WM_SYSCOMMAND()    ON_WM_PAINT()    ON_WM_QUERYDRAGICON()    ON_BN_CLICKED(IDOK, &CSmtpDlg::OnBnClickedOk)    ON_BN_CLICKED(IDCANCEL, &CSmtpDlg::OnBnClickedCancel)    ON_BN_CLICKED(IDSendMail, &CSmtpDlg::OnBnClickedSendmail)    ON_BN_CLICKED(IDSendMail2, &CSmtpDlg::OnBnClickedSendmail2)END_MESSAGE_MAP()// CSmtpDlg 消息处理程序BOOL CSmtpDlg::OnInitDialog(){    CDialogEx::OnInitDialog();    // 将“关于...”菜单项添加到系统菜单中。    // IDM_ABOUTBOX 必须在系统命令范围内。    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);    ASSERT(IDM_ABOUTBOX < 0xF000);    CMenu* pSysMenu = GetSystemMenu(FALSE);    if (pSysMenu != NULL)    {        BOOL bNameValid;        CString strAboutMenu;        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);        ASSERT(bNameValid);        if (!strAboutMenu.IsEmpty())        {            pSysMenu->AppendMenu(MF_SEPARATOR);            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);        }    }    // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动    //  执行此操作    SetIcon(m_hIcon, TRUE);            // 设置大图标    SetIcon(m_hIcon, FALSE);        // 设置小图标    // TODO: 在此添加额外的初始化代码    //m_csHost        = "163.com";            //shms3.wind.com.cn    //m_csPort        = "2504";    //m_csAccount        = "spriteflk";                    //lkfeng    //m_csPassword    = "asdQWE123";                //asdQWE123    //m_csFrom        = "spriteflk@163.com";        //"lkfeng@wind.com.cn"    //m_csTo            = "lkfeng@wind.com.cn";        //"zyhu.Adam@wind.com.cn";    m_csHost        = "smtp.wind.com.cn";            //shms3.wind.com.cn    m_csPort        = "25";    m_csAccount        = "lkfeng";                    //lkfeng    m_csPassword    = "asdQWE123";                //asdQWE123    m_csFrom        = "lkfeng@wind.com.cn";        //"lkfeng@wind.com.cn"    m_csTo            = "lkfeng@wind.com.cn";        //"zyhu.Adam@wind.com.cn";    m_csSubject        = "测试邮件-flk";    m_csDateTime    = "2008-5-29";    m_csContent        = "收到测试邮件,请告知我:)";    m_csAttachment    = "D:\\test\\SmtpSendEmail\\SmtpSendEmail\\DSC_2583 副本.jpg";    UpdateData(false);    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE}void CSmtpDlg::OnSysCommand(UINT nID, LPARAM lParam){    if ((nID & 0xFFF0) == IDM_ABOUTBOX)    {        CAboutDlg dlgAbout;        dlgAbout.DoModal();    }    else    {        CDialogEx::OnSysCommand(nID, lParam);    }}// 如果向对话框添加最小化按钮,则需要下面的代码//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,//  这将由框架自动完成。void CSmtpDlg::OnPaint(){    if (IsIconic())    {        CPaintDC dc(this); // 用于绘制的设备上下文        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);        // 使图标在工作区矩形中居中        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;        // 绘制图标        dc.DrawIcon(x, y, m_hIcon);    }    else    {        CDialogEx::OnPaint();    }}//当用户拖动最小化窗口时系统调用此函数取得光标//显示。HCURSOR CSmtpDlg::OnQueryDragIcon(){    return static_cast<HCURSOR>(m_hIcon);}void CSmtpDlg::OnBnClickedOk(){    // TODO: 在此添加控件通知处理程序代码    CDialogEx::OnOK();}void CSmtpDlg::OnBnClickedCancel(){    // TODO: 在此添加控件通知处理程序代码    CDialogEx::OnCancel();}void CSmtpDlg::OnBnClickedSendmail(){    UpdateData(true);        string strHost            = (TCHAR*)m_csHost.GetBuffer();    string strPort            = (TCHAR*)m_csPort.GetBuffer(0);    string strAccount        = (TCHAR*)m_csAccount.GetBuffer(0);    string strPassword        = (TCHAR*)m_csPassword.GetBuffer(0);    string strFrom            = (TCHAR*)m_csFrom.GetBuffer(0);    string strTos            = (TCHAR*)m_csTo.GetBuffer(0);    string strSubject        = (TCHAR*)m_csSubject.GetBuffer(0);    string strDateTime        = (TCHAR*)m_csDateTime.GetBuffer(0);    string strContent        = (TCHAR*)m_csContent.GetBuffer(0);    string strAttachments    = (TCHAR*)m_csAttachment.GetBuffer(0);    vector<string> vecSendTo;    Split(strTos, ;, vecSendTo);    vector<string> vecAttachmentFile;    Split(strAttachments, ;, vecAttachmentFile);    CSmtpSendEmail* pSender = CSmtpSendEmail::Builder(strHost, strPort, strAccount, strPassword,        strFrom, vecSendTo, strSubject, strDateTime,        strContent, vecAttachmentFile);    pSender->SendMail();}void CSmtpDlg::CallBackDispFunc(string strMsg){    m_csOutput += strMsg.c_str();    UpdateData(false);}void CSmtpDlg::OnBnClickedSendmail2(){    UpdateData(true);    string strHost            = (TCHAR*)m_csHost.GetBuffer();    string strPort            = (TCHAR*)m_csPort.GetBuffer(0);    string strAccount        = (TCHAR*)m_csAccount.GetBuffer(0);    string strPassword        = (TCHAR*)m_csPassword.GetBuffer(0);    string strFrom            = (TCHAR*)m_csFrom.GetBuffer(0);    string strTos            = (TCHAR*)m_csTo.GetBuffer(0);    string strSubject        = (TCHAR*)m_csSubject.GetBuffer(0);    string strDateTime        = (TCHAR*)m_csDateTime.GetBuffer(0);    string strContent        = (TCHAR*)m_csContent.GetBuffer(0);    string strAttachments    = (TCHAR*)m_csAttachment.GetBuffer(0);    vector<string> vecSendTo;    Split(strTos, ;, vecSendTo);    vector<string> vecAttachmentFile;    Split(strAttachments, ;, vecAttachmentFile);    CMail_L* pSender = CMail_L::Builder(strHost, strPort, strAccount, strPassword,        strFrom, vecSendTo, strSubject, strDateTime,        strContent, vecAttachmentFile,        0);    pSender->SendMail();}