www.pudn.com > QRAPPuie.rar > Maildlg.cpp
// Maildlg.cpp : implementation file
//
#include "stdafx.h"
#include "QRAPP2.h"
#include "Maildlg.h"
#include "mapidefs.h"
#include "mapix.h"
#include "cemapi.h"
#include "QRAPP2Dlg.h"
// CMaildlg dialog
IMPLEMENT_DYNAMIC(CMaildlg, CDialog)
CMaildlg::CMaildlg(CWnd* pParent /*=NULL*/)
: CDialog(CMaildlg::IDD, pParent)
, m_szTo(_T(""))
, m_szSub(_T(""))
, m_szTxt(_T(""))
{
}
CMaildlg::~CMaildlg()
{
}
void CMaildlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_MAILTO, m_ctrlTo);
DDX_Control(pDX, IDC_EDIT_MAILSUBJECT, m_ctrlSubject);
DDX_Control(pDX, IDC_EDIT_MAILCONTENT, m_ctrlTxt);
}
BEGIN_MESSAGE_MAP(CMaildlg, CDialog)
ON_COMMAND(ID_OPTION_SAVEASDRAFT, &CMaildlg::OnOptionSaveasdraft)
ON_COMMAND(ID_OPTION_SENDMAIL, &CMaildlg::OnOptionSendmail)
ON_COMMAND(ID_RETURN32808, &CMaildlg::OnReturn32808)
ON_COMMAND(ID_OPTION_SEND, &CMaildlg::OnOptionGetqrcode)
ON_COMMAND(ID_OPTION_SAVETOFILE32780, &CMaildlg::OnOptionSavetofile32780)
ON_WM_PAINT()
ON_WM_CTLCOLOR()
ON_WM_ACTIVATE()
END_MESSAGE_MAP()
// CMaildlg message handlers
BOOL CMaildlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
if (!m_dlgCommandBar.Create(this) ||
!m_dlgCommandBar.InsertMenuBar(IDR_MENU_MAIL))
{
TRACE0("Failed to create CommandBar\n");
return FALSE; // fail to create
}
m_ctrlTo.SetWindowTextW(m_szTo);
m_ctrlSubject.SetWindowTextW(m_szSub);
m_ctrlTxt.SetWindowTextW(m_szTxt);
GetDlgItem(IDC_STATIC_APP_MALL)->SetFocus();
return FALSE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CMaildlg::OnOptionSaveasdraft()
{
// TODO: Add your command handler code here
HRESULT hr;
ICEMAPISession *pSession = NULL;
IMAPITable *pTable = NULL;
SRowSet *pSRowSet = NULL;
IMsgStore *pStore = NULL;
//Begin a MAPI Session.
hr = MAPIInitialize(NULL);
if (hr != S_OK) {
// MAPIInitialize failed.
AfxMessageBox( _T("保存失败"),MB_OK, 0);
return; // Replace with specific error handling.
}
hr = MAPILogonEx(0, NULL, NULL, 0, (LPMAPISESSION *)&pSession);
if (hr !=S_OK)
{
AfxMessageBox(_T("保存失败"),MB_OK,0);
return;
}
//Connect to a Message Store.
hr = pSession->GetMsgStoresTable(0,&pTable);
if (hr !=S_OK)
{
AfxMessageBox(_T("保存失败"),MB_OK,0);
return;
}
hr = pTable->QueryRows(1,0,&pSRowSet);//pRowSet->aRow[0].lpProps->ulPropTag == PR_ENTRYID
hr = pSession->OpenMsgStore(0,pSRowSet->aRow[0].lpProps[0].Value.bin.cb,
(ENTRYID *)pSRowSet->aRow[0].lpProps[0].Value.bin.lpb,
NULL,
0,
&pStore); //////////?????
//create a message object
ULONG cValues = 0;
SPropValue *pSProps = NULL;
SPropTagArray STags = {1,{PR_CE_IPM_DRAFTS_ENTRYID}};
ULONG ulObjType = 0;
IMAPIFolder *pFldrDrafts = NULL;
IMessage *pMsg = NULL;
hr = pStore->GetProps(&STags,0,&cValues,&pSProps);
hr = pStore->OpenEntry(pSProps[0].Value.bin.cb,
(LPENTRYID)pSProps[0].Value.bin.lpb,
NULL,
0,
&ulObjType,
(IUnknown **)&pFldrDrafts);
if (hr != S_OK)
{
AfxMessageBox(_T("保存失败"),MB_OK,0);
return;
}
hr = pFldrDrafts->CreateMessage(NULL,0,&pMsg);
if (hr != S_OK)
{
AfxMessageBox(_T("保存失败"),MB_OK,0);
return;
}
//Prepare and set the list of recipients for the message.
/// ULONG cRecipients = 1; // Sending this to only 1 person
LPWSTR pszTo = m_szTo.GetBuffer();
m_szTo.ReleaseBuffer();
/// ULONG cRecProps = 3; // Setting 3 properties for each recipient
SPropValue spjonRecip[3];
spjonRecip[0].ulPropTag = PR_RECIPIENT_TYPE;
spjonRecip[0].dwAlignPad = 0;
spjonRecip[0].Value.ul = MAPI_TO;
spjonRecip[1].ulPropTag = PR_ADDRTYPE;
spjonRecip[1].dwAlignPad = 0;
spjonRecip[1].Value.lpszW = _T("SMTP");//??
spjonRecip[2].ulPropTag = PR_EMAIL_ADDRESS;
spjonRecip[2].dwAlignPad = 0;
spjonRecip[2].Value.lpszW = pszTo;
ADRLIST adrReceipients = { 1, {0, 3, spjonRecip} };
hr = pMsg->ModifyRecipients(MODRECIP_ADD,&adrReceipients);
//Prepare and set the following properties of the message: PR_MSG_STATUS, PR_MESSAGE_FLAGS, and PR_SUBJECT.
ULONG cMsgProps = 3; //setting 3 properties for the message
LPWSTR pszSubject = m_szSub.GetBuffer();
m_szSub.ReleaseBuffer();
LPSPropValue pMsgVal = NULL;
ULONG cbMsgVal = 0;
cbMsgVal = cMsgProps *(sizeof(SPropValue)+
(wcslen(pszSubject)+3)*(sizeof(WCHAR)));
hr = MAPIAllocateBuffer(cbMsgVal,(void **)&pMsgVal);
memset((void *)pMsgVal,0,cbMsgVal);
pMsgVal[0].ulPropTag = PR_MSG_STATUS;//Identify the message as SMTP by using the PR_MSG_STATUS property to set the message status to MSGSTATUS_RECTYPE_SMTP
pMsgVal[0].Value.ul = MSGSTATUS_RECTYPE_SMTP; ///
pMsgVal[1].ulPropTag = PR_MESSAGE_FLAGS;//Identify the current state of the message as unsent by using the PR_MESSAGE_FLAGS property to set the MSGFLAG_UNSENT flag
pMsgVal[1].Value.ul = MSGFLAG_UNSENT;
pMsgVal[2].ulPropTag = PR_SUBJECT;//Set the subject by using the PR_SUBJECT property
pMsgVal[2].Value.lpszW = pszSubject;
hr = pMsg->SetProps(cMsgProps,pMsgVal,NULL);
hr = MAPIFreeBuffer((void *)pMsgVal);
//Prepare and write the textual body of the message.
IStream *pStm = NULL;
hr = pMsg->OpenProperty(PR_BODY,
NULL,
0,
MAPI_MODIFY,
(LPUNKNOWN *)&pStm);
LPWSTR pszBody = m_szTxt.GetBuffer();
m_szTxt.ReleaseBuffer();
ULONG cbWritten = 0;
ULONG cbBody = 0;
cbBody =(wcslen(pszBody)+1)*(sizeof(WCHAR));
hr = pStm->Write(pszBody,cbBody,&cbWritten);
pStm->Release();
pStm = NULL;
//Submit the message ???
// hr =pMsg->SubmitMessage(0);//SubmitMessage func saves all of the message's properties and marks the message as ready to be sent.
//Release
pMsg->Release();
pMsg = NULL;
pStore->Release();
pFldrDrafts->Release();
MAPIFreeBuffer(pSProps);
pStore = NULL;
pFldrDrafts = NULL;
pSProps = NULL;
pTable->Release();
pTable = NULL;
AfxMessageBox( _T("成功保存到草稿箱!"),0, 0);
// FreeProws(pSRowSet);
//pSRowSet = NULL;
}
void CMaildlg::OnOptionSendmail()
{
// TODO: Add your command handler code here
TCHAR szCmdLine[164];
LPWSTR p = NULL;
// lstrcpy(szCmdLine,TEXT("-service \"sms\" -to \"12352444\" -body \"" ));//messagebody\" TEXT("-service \"sms\" -to \"12352444\" -body \"" )
// lstrcpy(szCmdLine,TEXT("mailto:yaolj_sh@amoi.com.cn" ));
lstrcpy(szCmdLine,TEXT("-to \""));
lstrcat(szCmdLine,m_szTo);
lstrcat(szCmdLine,TEXT("\""));
lstrcat(szCmdLine,TEXT("-subject \""));
//lstrcat(szCmdLine,m_szSub);
m_AppMenuFunc.AddBeforeQuotation(m_szSub,&p);
lstrcat(szCmdLine,p);
if (p!= NULL)
{
delete []p;
p=NULL;
}
lstrcat(szCmdLine,TEXT("\""));
lstrcat(szCmdLine,TEXT("-body \""));
//lstrcat(szCmdLine,m_szTxt);
m_AppMenuFunc.AddBeforeQuotation(m_szTxt,&p);
lstrcat(szCmdLine,p);
if (p!= NULL)
{
delete []p;
p=NULL;
}
lstrcat(szCmdLine,TEXT("\""));
//lstrcpy(szCmdLine,TEXT("mailto:yaolj_sh@amoi.com.cn -subject:subjstr -body:szbody")); ///??
m_AppMenuFunc.CreateTmailProcess(szCmdLine);
}
void CMaildlg::OnReturn32808()
{
// TODO: Add your command handler code here
CDialog::OnOK();
}
void CMaildlg::OnOptionGetqrcode()
{
// TODO: Add your command handler code here
CDialog::OnOK();
CQRAPP2Dlg dlg;
dlg.OnBnClickedButtonGetqrcode();
}
void CMaildlg::OnOptionSavetofile32780()
{
// TODO: Add your command handler code here
m_AppMenuFunc.SaveAsInfofile(m_szInformation);
}
void CMaildlg::OnPaint()
{
/// CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CDialog::OnPaint() for painting messages
CRect rect;
CPaintDC dc(this);
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(206,214,255)); //设置为绿色背景
CDialog::OnPaint();
}
HBRUSH CMaildlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(CTLCOLOR_STATIC == nCtlColor)
{
pDC->SetBkColor(RGB(206,214,255)); //背景色为白206 214 225
pDC->SetTextColor(RGB(0,0,0));
hbr = CreateSolidBrush(RGB(206,214,255));
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
void CMaildlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CDialog::OnActivate(nState, pWndOther, bMinimized);
// TODO: Add your message handler code here
if (nState == WA_ACTIVE)
{
DEVMODE mode= {0};
mode.dmSize = sizeof(DEVMODE);
mode.dmFields = DM_DISPLAYORIENTATION;
::ChangeDisplaySettingsEx(NULL, &mode, 0, CDS_TEST, NULL);
if (mode.dmDisplayOrientation != DMDO_0)
::ChangeDisplaySettingsEx(NULL, &g_devModeForqr, NULL, 0, NULL);
}
}