16,471
社区成员
发帖
与我相关
我的任务
分享
//接上
// sto 链接表 sSubject 标题 sContent 内容 sSource 附件路径
void CMonitor_Sys_MailDlg::sendmail( string sto, string sSubject, string sContent,string sSource)
{
//处理数据
CSmtp mail;
if(mail.GetLastError() != CSMTP_NO_ERROR)
return ;
mail.SetSMTPServer("smtp.126.com",25);
mail.SetLogin("xxxxxx");
mail.SetPassword("******");
mail.SetSenderName("NMG Monitor Robot");
mail.SetSenderMail("xxxxxx@126.com");
mail.SetReplyTo("xxxxxx@126.com");
mail.SetSubject(sSubject.c_str());
mail.AddRecipient(sto.c_str());
mail.SetXPriority(XPRIORITY_NORMAL);
mail.SetXMailer("The Bat! (v3.02) Professional");
mail.SetMessageBody(sContent.c_str());
if (sSource.size() > 3)
{
CString source = sSource.c_str();
mail.AddAttachment(source);
}
mail.Send();
}
TRevEmail CMonitor_Sys_MailDlg::revmail()
{
TRevEmail tret;
tret.ssubject = "";
tret.fromName = "";
CPOP3 pop3( "pop.126.com" );
pop3.SetUserProperties("xxxxxx","******");
if (!pop3.Connect())
{
// CString strp = "test1 ";
// strp += pop3.GetLastError();
// AfxMessageBox(strp );
if( !pop3.Disconnect() )
{
TRACE_LOG( pop3.GetLastError() );
}
return tret;
}
CMailMessage msg;
if (!pop3.GetMessage(1,&msg))// m_MN 先取1
{
// CString strp = "test2 ";
// strp += pop3.GetLastError();
// AfxMessageBox( strp );
return tret;
}
// strret=msg.m_sBody;
// strret = strret.Mid(0,10);
CString strsubject = msg.m_sSubject;
tret.fromName = msg.m_sFrom;
string ssubject = strsubject;
int pos1 = ssubject.find("B?") + 2;
int pos2 = ssubject.find("?=");
if(pos1 >0 && pos2 > 0)
{
CString strdecode = strsubject.Mid(pos1, pos2-pos1);
string sdecode = strdecode;
string sret = base64_decode(sdecode );
tret.ssubject = sret;
}
pop3.DeleteMessage(1 );
if( !pop3.Disconnect() )
{
TRACE_LOG( pop3.GetLastError() );
}
return tret;
}
LISTTEmailMsg CMonitor_Sys_MailDlg::GetConfig(string & cIp, int & cPort)
{
LISTTEmailMsg listEmail;
CStdioFile file;
string sfilepath=getCurPath()+"config.txt";
if(!file.Open(sfilepath.c_str(), CFile::modeRead))
{
TRACE_LOG("没有发现或打开配置文件!");
}else{
CString strLine;
while(file.ReadString(strLine))
{
TEmailMsg tEmail;
CString shead;
string sLine = strLine.GetBuffer(strLine.GetLength());
int npare;
strings parss;
strings::iterator ite;
divide_string(sLine.c_str(),parss,":",TRUE);
for(npare = 0,ite = parss.begin(); ite != parss.end();ite++,npare++)
{
switch(npare) {
case 0:
shead = (* ite).c_str();
break;
case 1:
{
if (shead.Find("IP")>=0)
{
cIp = (* ite);
}
else if (shead.Find("PORT")>=0)
{
cPort = atoi((* ite).c_str());
}
else if (shead.Find("EM")>=0)
{
string sLine1 = (* ite);
int npare1;
strings parss1;
strings::iterator ite1;
divide_string(sLine1.c_str(),parss1,",",TRUE);
for(npare1 = 0,ite1 = parss1.begin(); ite1 != parss1.end();ite1++,npare1++)
{
switch(npare1) {
case 0:
tEmail.warning_type = atoi((* ite1).c_str());
break;
case 1:
tEmail.toName = (* ite1);
break;
default:
break;
}
}
listEmail.push_back(tEmail);
}
}
break;
default:
break;
}
}
}
file.Close();
}
return listEmail;
}
string CMonitor_Sys_MailDlg::GetToName(int warning_type)
{
string ret;
list<TEmailMsg>::iterator iter ;
for(iter = m_listEmailMsg.begin(); iter != m_listEmailMsg.end(); ++iter)//query list
{
if (warning_type == (*iter).warning_type)
{
ret = (*iter).toName;
break;
}
}
return ret;
}
void CMonitor_Sys_MailDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (nIDEvent == 1)
{
KillTimer(1);
TRevEmail tret = revmail();
if(tret.ssubject.length() > 3)
{
TRACE_LOG("receive from %s , subject:%s", tret.fromName.c_str(), tret.ssubject.c_str());
myServer.SendLine(tret.ssubject);
}
SetTimer(1, 5000, NULL);
}
CDialog::OnTimer(nIDEvent);
}
// Monitor_Sys_MailDlg.cpp : implementation file
#include "stdafx.h"
#include "Monitor_Sys_Mail.h"
#include "Monitor_Sys_MailDlg.h"
#include <process.h>
#include "CSmtp.h"
#include "POP3.h"
#include "LogStore.h"
#include "MailMessage.h"
#include "MailMessage.h"
#include "base64.h"
#include <string.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();
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMonitor_Sys_MailDlg dialog
CMonitor_Sys_MailDlg::CMonitor_Sys_MailDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMonitor_Sys_MailDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMonitor_Sys_MailDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STATIC_3, m_suiinfo3);
DDX_Control(pDX, IDOK, m_button_ok);
DDX_Control(pDX, IDC_STATIC_2, m_suiinfo2);
DDX_Control(pDX, IDC_STATIC_1, m_suiinfo1);
}
BEGIN_MESSAGE_MAP(CMonitor_Sys_MailDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMonitor_Sys_MailDlg message handlers
void RecvThread( void* pParams )
{
CMonitor_Sys_MailDlg *pSat = (CMonitor_Sys_MailDlg *) pParams;
if (pSat == NULL) {
return;
}
while (1)
{
string ret = getMsgforSocket(1);
if (ret.size() > 0)
{
TWarningMsg tWarningMsg;
string sLine = ret;
int npare;
strings parss;
strings::iterator ite;
divide_string(sLine.c_str(),parss,"$",TRUE);
TRACE_LOG("rev: %s.", sLine.c_str());
for(npare = 0,ite = parss.begin(); ite != parss.end();ite++,npare++)
{
string shead;
switch(npare) {
case 0:
tWarningMsg.warning_type = atoi((* ite).c_str());
break;
case 1:
tWarningMsg.warning_time = (* ite);
break;
case 2:
tWarningMsg.warning_name = (* ite);
break;
case 3:
tWarningMsg.warning_content = (* ite);
break;
default:
break;
}
}
string sto = pSat->GetToName(tWarningMsg.warning_type) ;
string sSource;
if (sto.size() >0)
{
string sSubject = tWarningMsg.warning_name;//"NGN 系统报障";
string sContent = tWarningMsg.warning_content;// "NGN CX 监测点 流量不正常";
sContent += "/r/n";
sContent += tWarningMsg.warning_time;
string sLine1 = sto;//(* ite);
int npare1;
strings parss1;
strings::iterator ite1;
divide_string(sLine1.c_str(),parss1,";",TRUE);
for(npare1 = 0,ite1 = parss1.begin(); ite1 != parss1.end();ite1++,npare1++)
{
string stoname = (* ite1);
pSat->sendmail( stoname, sSubject, sContent, sSource);
}
string stime = getCurTime();
pSat->m_suiinfo1.SetWindowText(stime.c_str());
pSat->m_suiinfo2.SetWindowText(sSubject.c_str());
}
}
Sleep(1000);
}
_endthread();
}
BOOL CMonitor_Sys_MailDlg::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
m_suiinfo1.SetWindowText("");
m_suiinfo2.SetWindowText("");
m_suiinfo3.SetWindowText("未连接");
SetTimer(1, 5000, NULL);
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMonitor_Sys_MailDlg::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 CMonitor_Sys_MailDlg::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 CMonitor_Sys_MailDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMonitor_Sys_MailDlg::OnOK()
{
// TODO: Add extra validation here
// MyLineMsgReceiver myReceiver;
myServer.SetReceiver(&myReceiver);;
string IP_ADDR ;//= "127.0.0.1";
int PORT;//= 4567;
m_listEmailMsg = GetConfig(IP_ADDR, PORT);
_beginthread(RecvThread, 0, (void *)this);
int ret = myServer.StartServer(IP_ADDR.c_str(),PORT);
if (ret >= 0)
{
m_suiinfo3.SetWindowText("服务端已启动");
}
m_button_ok.EnableWindow(FALSE);
Sleep(1000);
/* CString str;//1002;LOGIN_NAME
str.Format("1002;");
string sline = str;
myClient.SendLine(sline);
*/ // CDialog::OnOK();
}