smtpDoc.cpp
资源名称:mysmtp2.rar [点击查看]
上传用户:young001
上传日期:2007-07-04
资源大小:33k
文件大小:4k
源码类别:
WEB邮件程序
开发平台:
Visual C++
- // smtpDoc.cpp : implementation of the CSmtpDoc class
- //
- #include "stdafx.h"
- #include "smtp.h"
- #include "smtpDoc.h"
- #include "CSmtp.h"
- #include "SmtpSetupDlg.h"
- #include "AttachDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CSmtpDoc
- IMPLEMENT_DYNCREATE(CSmtpDoc, CDocument)
- BEGIN_MESSAGE_MAP(CSmtpDoc, CDocument)
- //{{AFX_MSG_MAP(CSmtpDoc)
- ON_COMMAND(ID_PREFERENCES_SEND, OnPreferencesSend)
- ON_COMMAND(ID_PREFERENCES_SETUP, OnPreferencesSetup)
- ON_COMMAND(ID_PREFERENCES_ATTACH, OnPreferencesAttach)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CSmtpDoc construction/destruction
- CSmtpDoc::CSmtpDoc()
- {
- // TODO: add one-time construction code here
- m_SmtpServer = AfxGetApp() -> GetProfileString(_T("SETUP"), _T("SMTPSERVER"), _T("smtp.263.net"));
- m_From = AfxGetApp() -> GetProfileString(_T("SETUP"), _T("FROM"), _T("coolkknd@263.net"));
- m_MailTo = AfxGetApp() -> GetProfileString(_T("SETUP"), _T("TO"), _T("coolkknd@263.net"));
- m_Subject = AfxGetApp() -> GetProfileString(_T("SETUP"), _T("SUBJECT"), _T("简单邮件发送测试"));
- }
- CSmtpDoc::~CSmtpDoc()
- {
- }
- BOOL CSmtpDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- ((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CSmtpDoc serialization
- void CSmtpDoc::Serialize(CArchive& ar)
- {
- // CEditView contains an edit control which handles all serialization
- ((CEditView*) m_viewList.GetHead())->SerializeRaw(ar);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CSmtpDoc diagnostics
- #ifdef _DEBUG
- void CSmtpDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CSmtpDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CSmtpDoc commands
- void CSmtpDoc::OnPreferencesSend()
- {
- // TODO: Add your command handler code here
- POSITION pos = GetFirstViewPosition();
- CEditView* p_View = (CEditView*) GetNextView(pos);
- CEdit& m_Ctrl = p_View -> GetEditCtrl();
- CSmtp smtp("");
- CMIMEMessage msg;
- CBase64 auth;
- CString m_sErr;
- smtp.SetServerProperties(m_SmtpServer);
- smtp.m_User = auth.Encode(m_User,m_User.GetLength());
- smtp.m_Pass = auth.Encode(m_Pass,m_Pass.GetLength());
- m_sErr.Format("user: %srnpass: %srn",smtp.m_User,smtp.m_Pass);
- //AfxMessageBox(m_sErr);
- msg.m_sFrom = _T(m_From);
- msg.AddMultipleRecipients(m_MailTo);
- msg.m_sSubject = m_Subject;
- m_Ctrl.GetWindowText(msg.m_sBody);
- if(!smtp.Connect()) {
- AfxMessageBox(smtp.GetLastError());
- return ;
- }
- if(!smtp.Auth()) {
- AfxMessageBox(smtp.GetLastError());
- return ;
- }
- if(!smtp.SendMessage(&msg)) {
- AfxMessageBox(smtp.GetLastError());
- smtp.Disconnect();
- return ;
- }
- smtp.Disconnect();
- m_Files.RemoveAll();
- AfxMessageBox(_T("Message sent successfully"));
- }
- void CSmtpDoc::OnPreferencesSetup()
- {
- // TODO: Add your command handler code here
- SmtpSetupDlg dlg;
- if (dlg.DoModal() == IDOK) {
- m_SmtpServer = dlg.m_SmtpServer;
- m_From = dlg.m_From;
- m_MailTo = dlg.m_MailTo;
- m_Subject = dlg.m_Subject;
- m_User = dlg.m_User;
- m_Pass = dlg.m_Pass;
- AfxGetApp() -> WriteProfileString(_T("SETUP"), _T("SMTPSERVER"), m_SmtpServer);
- AfxGetApp() -> WriteProfileString(_T("SETUP"), _T("FROM"), m_From);
- AfxGetApp() -> WriteProfileString(_T("SETUP"), _T("TO"), m_MailTo);
- AfxGetApp() -> WriteProfileString(_T("SETUP"), _T("SUBJECT"), m_Subject);
- }
- }
- void CSmtpDoc::OnPreferencesAttach()
- {
- // TODO: Add your command handler code here
- AttachDlg dlg;
- if (dlg.DoModal() == IDOK) {
- m_Files.RemoveAll();
- m_Files.Append(dlg.m_Files);
- }
- }
- void CSmtpDoc::DeleteContents()
- {
- // TODO: Add your specialized code here and/or call the base class
- m_Files.RemoveAll();
- CDocument::DeleteContents();
- }