#include "pch.hpp"
//===========================================================================//
// File:	 Mail.cpp														 //
// Project:  GameOS					                                         //
// Contents: MAPI (email) routines											 //
//---------------------------------------------------------------------------//
// Date      Who    Modification                                             //
// 03/25/98  AG		Created													 //
//																			 //
//---------------------------------------------------------------------------//
// Copyright (C) 1999 Microsoft Corp.						                 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//
#include "ErrorHandler.hpp"
#pragma warning(disable: 4228)
#include <mapi.h>
#pragma warning(default: 4228)
#include "Windows.hpp"




//
// Turn a MAPI error number into a message
//
char* GetMapiError( int error )
{
	switch( error )
	{
		case	MAPI_E_USER_ABORT:
			return "User aborted";
		case	MAPI_E_FAILURE:
			return "Failure";
		case	MAPI_E_LOGIN_FAILURE:
			return "Login Failure";
		case	MAPI_E_DISK_FULL:
			return "Disk Full";
		case	MAPI_E_INSUFFICIENT_MEMORY:
			return "Insufficient memory";
		case	MAPI_E_ACCESS_DENIED:
			return "Access denied";
		case	MAPI_E_TOO_MANY_SESSIONS:
			return "Too many sessions";
		case	MAPI_E_TOO_MANY_FILES:
			return "Too many files";
		case	MAPI_E_TOO_MANY_RECIPIENTS:
			return "Too many recipients";
		case	MAPI_E_ATTACHMENT_NOT_FOUND:
			return "Attachment not found";
		case	MAPI_E_ATTACHMENT_OPEN_FAILURE:
			return "Attachment open failure";
		case	MAPI_E_ATTACHMENT_WRITE_FAILURE:
			return "Attachment write failure";
		case	MAPI_E_UNKNOWN_RECIPIENT:
			return "Unknown recipient";
		case	MAPI_E_BAD_RECIPTYPE:
			return "Bad reciptype";
		case	MAPI_E_NO_MESSAGES:
			return "No messages";
		case	MAPI_E_INVALID_MESSAGE:
			return "Invalid message";
		case	MAPI_E_TEXT_TOO_LARGE:
			return "Text too large";
		case	MAPI_E_INVALID_SESSION:
			return "Invalid session";
		case	MAPI_E_TYPE_NOT_SUPPORTED:
			return "Type not supported";
		case	MAPI_E_AMBIGUOUS_RECIPIENT:
			return "Ambiguous recipient";
		case	MAPI_E_MESSAGE_IN_USE:
			return "Message in use";
		case	MAPI_E_NETWORK_FAILURE:
			return "Network failure";
		case	MAPI_E_INVALID_EDITFIELDS:
			return "Invalid editfields";
		case	MAPI_E_INVALID_RECIPS:
			return "Invalid recips";
		case	MAPI_E_NOT_SUPPORTED:
			return "Not supported";
		default:
		static char Buffer[256];
		wsprintf( Buffer, "Unknown error %d", error );
		return Buffer;
	}
}



MapiFileDesc Attachment[2];

//
//
// Send the passed text as an email message
//
//
char* SendMail( HWND Window, char* Subject, char* Text, char* Bitmap, char* LogFile )
{
//
// Load up the mail DLL
//
	HINSTANCE hMail=LoadLibrary("MAPI32.DLL");
	if( 0==hMail )
		return "Could not find MAPI32.DLL";
//
// Find MAPISendMail function
//
	ULONG (PASCAL *_MAPISendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
	(FARPROC&)_MAPISendMail = GetProcAddress(hMail, "MAPISendMail");
	if (_MAPISendMail == NULL)
	{
		FreeLibrary(hMail);
		return "Bad MAPI32.DLL";
	}
//
// Prepare the message
//
	MapiMessage message;
	memset(&message, 0, sizeof(message));
	message.lpszNoteText=Text;
	message.lpszSubject=Subject;
//
// Prepair any attachments
//
	memset(Attachment, 0, sizeof(Attachment));

	if( Bitmap )
	{
		Attachment[message.nFileCount].nPosition=-1;
		Attachment[message.nFileCount].lpszPathName=Bitmap;
		Attachment[message.nFileCount].lpszFileName="Screen.bmp";
		message.nFileCount++;
		message.lpFiles=Attachment;
	}
	if( LogFile )
	{
		Attachment[message.nFileCount].nPosition=-1;
		Attachment[message.nFileCount].lpszPathName=LogFile;
		Attachment[message.nFileCount].lpszFileName="LogFile.txt";
		message.nFileCount++;
		message.lpFiles=Attachment;
	}
//
// Try sending the mail
//
	int error = _MAPISendMail(0,(LONG)Window,&message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
//
// If there is an error, return error string
//
	if( error!=SUCCESS_SUCCESS && error!=MAPI_E_USER_ABORT )
	{
		static char Buffer[256];
		wsprintf( Buffer, "Error '%s'\n",GetMapiError(error) );
		FreeLibrary(hMail);
		return Buffer;
	}
//
// Remove MAPI dll and return with no errors
//
	FreeLibrary(hMail);
	return 0;
}





