using System;
using System.Runtime.InteropServices;
namespace YariSoft.Utils{
public class Mapi
{
[StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
private class MapiRecipDesc
{
public int ulReserved = 0;
public int ulRecipClass = 1;
public string lpszName = "";
public string lpszAddress = "";
public int ulEIDSize = 0;
public IntPtr lpEntryID = IntPtr.Zero;
public MapiRecipDesc()
{
}
public MapiRecipDesc( string Address, int RecipClass ):this()
{
this.lpszAddress = Address;
this.ulRecipClass = RecipClass;
}
}
[StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
private class MapiMessage
{
public int ulReserved = 0;
public string lpszSubject = "";
public string lpszNoteText = "";
public string lpszMessageType = "";
public string lpszDateReceived = "";
public string lpszConversationID = "";
public int flFlags = 0;
public object lpOriginator = null;
public int nRecipCount = 0;
public IntPtr lpRecips = IntPtr.Zero;
public int nFileCount = 0;
public object lpFiles = null;
public MapiMessage()
{
}
public void Dispose()
{
this.DeleteMapiRecipDesc();
}
public MapiMessage( string Address, string Subject, string NoteText ):this()
{
this.lpszSubject = Subject;
this.lpszNoteText = NoteText;
this.nRecipCount = 1;
this.AllocateMapiRecipDesc ( Address, ref this.lpRecips, 1 );
}
private void AllocateMapiRecipDesc( string Address, ref IntPtr Pointer, int RecipClass )
{
MapiRecipDesc recip = new MapiRecipDesc ( Address, RecipClass );
IntPtr ptr = Marshal.AllocHGlobal( Marshal.SizeOf( typeof( MapiRecipDesc )));
Marshal.StructureToPtr( recip, ptr, false );
Pointer = ptr;
return;
}
private void DeleteMapiRecipDesc()
{
if( this.lpRecips != IntPtr.Zero ){
Marshal.DestroyStructure( this.lpRecips, typeof(MapiRecipDesc) );
Marshal.FreeHGlobal( this.lpRecips );
}
}
}
[System.Runtime.InteropServices.DllImport( "MAPI32.DLL")]
private static extern int MAPISendMail( int lhSession,
int ulUIParam,
MapiMessage lpMessage,
int flFlags,
int ulReserved );
public static int Send( string Address, string Subject, string Text, bool Anonymous )
{
int result = 0;
MapiMessage lpMessage = new MapiMessage ( Address, Subject, Text );
try{
result = MAPISendMail( 0, 0, lpMessage, 0, 0 );
} finally{
lpMessage.Dispose();
}
return result;
}
}
}
|