본문으로 바로가기

메일전송,Mail,SEND

category 개발언어/c++ 2016. 7. 16. 18:59

 

메일전송


#include 

void CReportErrorDlg::OnSend() 
{
   CWhatWereYouDoingDlg dlg1(this);
   dlg1.m_strString = m_strWhatWereYouDoing;
   if(dlg1.DoModal() == IDOK)
   {
      m_strWhatWereYouDoing = dlg1.m_strString;
      HINSTANCE hMail = NULL;
      hMail = ::LoadLibraryA("MAPI32.DLL");

       if (hMail == NULL)
       {
           AfxMessageBox(AFX_IDP_FAILED_MAPI_LOAD);
           return;
       }
       ASSERT(hMail != NULL);

       ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
       (FARPROC&)lpfnSendMail = GetProcAddress(hMail, "MAPISendMail");
       if (lpfnSendMail == NULL)
       {
           AfxMessageBox(AFX_IDP_INVALID_MAPI_DLL);
           return;
       }
       ASSERT(lpfnSendMail != NULL);

      // make a recipient
      MapiRecipDesc rec;
   
      char szRName[] = "수신자명"; //받는 사람 위치에 뿌려질 이름입니다. 
      char szRAddress[] = "SMTP:수신자eMail"; //실제 받을 email 주소를 기입하면 됩니다.
      memset(&rec, 0, sizeof(rec));
      rec.ulRecipClass = MAPI_TO;
      rec.lpszName = szRName;              
      rec.lpszAddress = szRAddress;
   
      char szSubject[] = "An Exception has occurred";
   
      // prepare the message -----------------------------------------------------
       
      MapiMessage message;
       memset(&message, 0, sizeof(message));
      
      message.nRecipCount = 1;
      message.lpRecips = &rec;
      message.lpszSubject = szSubject;

      CString s = m_strWhatWereYouDoing;
      s += "\n";
      PrepareErrorMessage(s);

      message.lpszNoteText = s.GetBuffer(s.GetLength());

       // prepare for modal dialog box
       AfxGetApp()->EnableModeless(FALSE);
       HWND hWndTop;
       CWnd* pParentWnd = CWnd::GetSafeOwner(NULL, &hWndTop);

       // some extra precautions are required to use MAPISendMail as it
       // tends to enable the parent window in between dialogs (after
       // the login dialog, but before the send note dialog).
       pParentWnd->SetCapture();
       ::SetFocus(NULL);
       pParentWnd->m_nFlags |= WF_STAYDISABLED;

       int nError = lpfnSendMail(0, (ULONG)pParentWnd->GetSafeHwnd(),
           &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
      s.ReleaseBuffer();

       // after returning from the MAPISendMail call, the window must
       // be re-enabled and focus returned to the frame to undo the workaround
       // done before the MAPI call.
       ::ReleaseCapture();
       pParentWnd->m_nFlags &= ~WF_STAYDISABLED;

       pParentWnd->EnableWindow(TRUE);
       ::SetActiveWindow(NULL);
       pParentWnd->SetActiveWindow();
       pParentWnd->SetFocus();
       if (hWndTop != NULL)
           ::EnableWindow(hWndTop, TRUE);
       AfxGetApp()->EnableModeless(TRUE);

       if (nError != SUCCESS_SUCCESS &&
           nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
       {
           AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
       }
      ::FreeLibrary(hMail);
   }
}

   

[파일 첨부 예제]

앞서 보여드린 예의 message 필드를 채우는 과정에서 다음과 같이 코딩합니다.

message.nFileCount = 2; //첨부 파일 2개

MapiFileDesc mfd[2]; //첨부될 파일 배열 2개
//첫번째 첨부 파일 정보
memset(&mfd[0], 0, sizeof(mfd[0]));
mfd[0].lpszPathName = "C:\\aclient.cfg"; //실제 파일 경로
mfd[0].lpszFileName = "aclient.cfg"; //메일 인터페이스에 나타날 첨부된 파일 제목

mfd[0].nPosition=0; or -1

 


//두번째 첨부 파일 정보
memset(&mfd[1], 0, sizeof(mfd[0]));
mfd[1].lpszPathName = "C:\\aclient.dat";
mfd[1].lpszFileName = "aclient.dat";
mfd[1].nPosition=1; or -1


message.lpFiles = mfd; //첨부 목록 설정