📄 mainfrm.cpp
字号:
}
else
{
pMail->m_pMainFrm->SetTask("");
pMail->m_pMainFrm->SetTimer(0x0199, 50, NULL);
strMsg.Format("发送邮件失败。");
AfxMessageBox(strMsg);
}
if( m_bClosing )
{
pMail->m_pMainFrm->SetTimer(0x0099, 500, NULL);
}
return 0;
}
CPJNSMTPMessage* CMainFrame::CreateMessage(const CMail& mail)
{
//Create the message
CPJNSMTPMessage* pMessage = new CPJNSMTPMessage;
CPJNSMTPBodyPart attachment;
//Set the mime flag
pMessage->SetMime(FALSE);
//Set the charset of the message and all attachments
pMessage->SetCharset("GB2312");
attachment.SetCharset("GB2312");
//Set the message priority
pMessage->m_Priority = CPJNSMTPMessage.HIGH_PRIORITY;
//Setup the all the recipient types for this message
char szTemp[1024];
strcpy(szTemp, mail.m_sTo);
char *token = strtok(szTemp, ",");
if( token == NULL )
{
pMessage->AddMultipleRecipients(szTemp, CPJNSMTPMessage::TO);
}
while( token != NULL )
{
pMessage->AddMultipleRecipients(token, CPJNSMTPMessage::TO);
token = strtok( NULL, "," );
}
if (!mail.m_sCC.IsEmpty())
{
strcpy(szTemp, mail.m_sCC);
char *token = strtok(szTemp, ",");
if( token == NULL )
{
pMessage->AddMultipleRecipients(szTemp, CPJNSMTPMessage::CC);
}
while( token != NULL )
{
pMessage->AddMultipleRecipients(token, CPJNSMTPMessage::CC);
token = strtok( NULL, "," );
}
}
if (!mail.m_sBCC.IsEmpty())
{
strcpy(szTemp, mail.m_sBCC);
char *token = strtok(szTemp, ",");
if( token == NULL )
{
pMessage->AddMultipleRecipients(szTemp, CPJNSMTPMessage::BCC);
}
while( token != NULL )
{
pMessage->AddMultipleRecipients(token, CPJNSMTPMessage::BCC);
token = strtok( NULL, "," );
}
}
if (!mail.m_sSubject.IsEmpty())
pMessage->m_sSubject = mail.m_sSubject;
if (!mail.m_sBody.IsEmpty())
{
pMessage->AddTextBody(mail.m_sBody);
}
//Add the attachment(s) if necessary
if (!mail.m_sFile.IsEmpty())
{
strcpy(szTemp, mail.m_sFile);
char *token = strtok(szTemp, ",");
if( token == NULL )
{
pMessage->AddMultipleAttachments(szTemp);
}
while( token != NULL )
{
pMessage->AddMultipleAttachments(token);
token = strtok( NULL, "," );
}
}
//Setup the from address
if (mail.m_sName.IsEmpty())
{
pMessage->m_From = mail.m_sAddress;
//pMessage->m_ReplyTo = m_sAddress; uncomment this if you want to send a Reply-To header
}
else
{
CPJNSMTPAddress address(mail.m_sName, mail.m_sAddress);
pMessage->m_From = address;
}
pMessage->m_sXMailer = _T(""); //comment this line out if you want to send a X-Mailer header
pMessage->m_DSN = PJNSMTP_DSN_NOT_SPECIFIED;
#ifdef _DEBUG
//Add one custom header (for test purpose)
pMessage->AddCustomHeader(_T("X-Program: CSTMPMessageTester"));
//Try out the copy constructor and operator= methods
CPJNSMTPMessage copyOfMessage(*pMessage);
#endif
return pMessage;
}
BOOL CMainFrame::SendMail(const CMail& mail)
{
CString sServer = mail.m_sServer;
if( !CGlobal::GetInstance().IsIP(sServer) )
{
sServer = CGlobal::GetInstance().GetIPByUrl(sServer,true);
}
//Create the message
CPJNSMTPMessage* pMessage = NULL;
int nPort = 25;
pMessage = CreateMessage(mail);
//connect to server
CPJNSMTPConnection smtp;
CPJNSMTPConnection::AuthenticationMethod Authenticate = (CPJNSMTPConnection::AuthenticationMethod)2;
CString strMsg;
BOOL bConnect = smtp.Connect(sServer, Authenticate, mail.m_sUserName, mail.m_sPassword, nPort);
if (!bConnect)
{
CString sResponse = smtp.GetLastCommandResponse();
TRACE(_T("Failed to connect to SMTP server\n"));
strMsg.Format("发送信息到 %s 失败。",mail.m_sTo);
delete pMessage;
return FALSE;
}
else
{
//Send the message
BOOL b = smtp.SendMessage(*pMessage);
if ( b == FALSE )
{
CString sResponse = smtp.GetLastCommandResponse();
TRACE(_T("Failed to send the SMTP message\n"));
strMsg.Format("发送信息到 %s 失败。",mail.m_sTo);
delete pMessage;
smtp.Disconnect();
return FALSE;
}
else
{
strMsg.Format("发送信息到 %s 成功。",mail.m_sTo);
}
}
delete pMessage;
smtp.Disconnect();
return TRUE;
}
void CMainFrame::OnTeacherInfo()
{
// TODO: Add your command handler code here
}
extern const char* SheetItem[];
void CMainFrame::OnUpdateTeacherInfo(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable((GetLevel() == 3 && m_pSelectView->GetItemText(1) == SheetItem[1] && (theApp.IsAdmin() || m_pSelectView->GetItemText(2) == theApp.GetLogin()))
|| (GetLevel() == 2 && theApp.IsAdmin() && m_pSelectView->GetItemText(1) == SheetItem[1]));
}
void CMainFrame::OnAdminPassword()
{
// TODO: Add your command handler code here
CPasswordDlg PasswordDlg(this);
PasswordDlg.DoModal();
}
void CMainFrame::OnUpdateAdminPassword(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(theApp.GetLogin() == "administrator" );
}
void CMainFrame::OnSetTerm()
{
// TODO: Add your command handler code here
CTermDlg termdlg(this);
if( termdlg.DoModal() == IDOK )
{
m_pTimeView->DrawList();
}
}
void CMainFrame::OnUpdateSetTerm(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(theApp.IsAdmin() );
}
void CMainFrame::Mail2Admin()
{
if( m_sEMail == "" || m_sEMail.Find("@") < 0 )
{
SetTimer(0x0099, 100, NULL);
return;
}
CString sBody = "";
CString sSubject = "毕业设计管理系统";
CString sAttach = "";
CString sFile;
CString sTeacher = theApp.GetLogin();
CTeacher* pTeacher = CGlobal::GetInstance().FindTeacher(sTeacher);
if( m_bModifySheet == TRUE )
{
if( !pTeacher )
sFile.Format("%s\\Sheet\\%s", theApp.GetWorkDirectory(), "名单.xls");
else
sFile.Format("%s\\Sheet\\%s-%s", theApp.GetWorkDirectory(), pTeacher->GetName(), "名单.xls");
m_pSelectView->DoExportSheet(sFile);
sBody += "名单已修改";
sAttach += sFile;
}
if( m_bModifyTheme == TRUE )
{
if( !pTeacher )
sFile.Format("%s\\Theme\\%s", theApp.GetWorkDirectory(), "题目.xls");
else
sFile.Format("%s\\Theme\\%s-%s", theApp.GetWorkDirectory(), pTeacher->GetName(), "题目.xls");
m_pSelectView->DoExportTheme(sFile);
if( sBody != "" )
sBody += ",";
sBody += "题目已修改";
if( sAttach != "" )
sAttach += ",";
sAttach += sFile;
}
if( m_bModifyTheme == TRUE )
{
if( !pTeacher )
sFile.Format("%s\\Score\\%s", theApp.GetWorkDirectory(), "成绩.xls");
else
sFile.Format("%s\\Score\\%s-%s", theApp.GetWorkDirectory(), pTeacher->GetName(), "成绩.xls");
m_pSelectView->DoExportScore(sFile);
if( sBody != "" )
sBody += ",";
sBody += "成绩已修改";
if( sAttach != "" )
sAttach += ",";
sAttach += sFile;
}
m_mail.m_sTo = m_sEMail;
m_mail.m_sCC = "";
m_mail.m_sBCC = "";
m_mail.m_sSubject = sSubject;
m_mail.m_sBody = sBody;
m_mail.m_sFile = sAttach;
m_mail.m_pMainFrm = this;
AfxBeginThread(CMainFrame::SendMailThreadProc,(LPVOID)&m_mail);//启动线程
}
void CMainFrame::OnClose()
{
// TODO: Add your message handler code here and/or call default
m_bClosing = TRUE;
if( m_bModifySheet == TRUE || m_bModifyScore == TRUE || m_bModifyTheme == TRUE )
{
if( !theApp.IsAdmin() )
{
if( AfxMessageBox("名单/题目/成绩已经修改,发邮件给管理员?",MB_YESNO|MB_ICONQUESTION) == IDYES )
{
Mail2Admin();
m_bModifySheet = FALSE;
m_bModifyScore = FALSE;
m_bModifyTheme = FALSE;
return;
}
}
}
CFrameWnd::OnClose();
}
void CMainFrame::OnDestroy()
{
CFrameWnd::OnDestroy();
// TODO: Add your message handler code here
}
void CMainFrame::OnBackupDb()
{
// TODO: Add your command handler code here
theApp.SetRegistryKey(REGISTRY_DB_KEY);
CString strDBFile;
theApp.GetRegistryValue("GPMIS","DBQ",strDBFile,"");
theApp.SetRegistryKey(REGISTRY_KEY);
if( strDBFile != "" )
{
CString strBKDBFile;
CTime dt = CTime::GetCurrentTime();
strBKDBFile.Format("%s\\Document\\GPMIS.%d.%d.%d.mdb",theApp.GetWorkDirectory(),
dt.GetYear(),dt.GetMonth(),dt.GetDay());
CopyFile(strDBFile, strBKDBFile, FALSE);
}
}
void CMainFrame::OnUpdateBackupDb(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(theApp.IsAdmin());
}
void CMainFrame::OnRestoreDb()
{
// TODO: Add your command handler code here
theApp.SetRegistryKey(REGISTRY_DB_KEY);
CString strDBFile;
theApp.GetRegistryValue("GPMIS","DBQ",strDBFile,"");
theApp.SetRegistryKey(REGISTRY_KEY);
if( strDBFile != "" )
{
static char BASED_CODE szFilter[] = "MS Access Files (*.mdb)|*.mdb||";
char szPath[MAX_PATH];
sprintf(szPath,"%s\\Document",theApp.GetWorkDirectory());
CFileDialog FileDlg(TRUE,"MDB",NULL,
OFN_FILEMUSTEXIST|OFN_NONETWORKBUTTON|
OFN_PATHMUSTEXIST,szFilter);
FileDlg.m_ofn.lpstrInitialDir = szPath;
if( FileDlg.DoModal() != IDOK )
return;
// To get the selected file's path and name
CString strBKDBFile;
strBKDBFile = FileDlg.GetPathName();
if(strBKDBFile.IsEmpty())
{
return;
}
CopyFile(strBKDBFile, strDBFile, FALSE);
theApp.ReConnectDB();
CGlobal::GetInstance().CreateStudentList(m_nYear);
CGlobal::GetInstance().CreateClassList(m_nYear);
CGlobal::GetInstance().CreateTitleMap();
CGlobal::GetInstance().CreateOthersMap();
m_pSelectView->DrawTreeItem();
m_pSheetView->DrawList();
m_pThemeView->DrawList();
m_pScoreView->DrawList();
}
}
void CMainFrame::OnUpdateRestoreDb(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(theApp.IsAdmin());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -