Sorry, we don't find any difference between your code and our code in default event handler. We use following method:
Code: Select all
protected void SendEMail(string subject, string body, string filePath)
{
try
{
MAPI.SendEMail(subject, body, filePath);
}
catch (Exception e)
{
StiLogService.Write(this.GetType(), "Send E-mail");
StiLogService.Write(this.GetType(), e);
if (!StiReport.HideMessages)MessageBox.Show(e.Message);
}
}
I have only one recommendation, try to use our code directly. You can find full version of MAPI class above:
Code: Select all
using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Stimulsoft.Report.Export
{
#region MapiMessage
[StructLayout(LayoutKind.Sequential)]
public class MapiMessage
{
public int reserved;
public string subject;
public string noteText;
public string messageType;
public string dateReceived;
public string conversationID;
public int flags;
public IntPtr originator;
public int recipCount;
public IntPtr recips;
public int fileCount;
public IntPtr files;
}
#endregion
#region MapiFileDesc
[StructLayout(LayoutKind.Sequential)]
public class MapiFileDesc
{
public int reserved;
public int flags;
public int position;
public string pathName;
public string fileName;
public IntPtr fileType;
}
#endregion
[System.Security.SuppressUnmanagedCodeSecurity]
public sealed class MAPI
{
#region Methods
private static MapiMessage CreateMessage(string subject, string body, string filePath)
{
MapiMessage message = new MapiMessage();
message.subject = subject;
message.noteText = body;
message.fileCount = 1;
message.files = GetFileDesc(filePath);
return message;
}
private static void DisposeMessage(MapiMessage msg)
{
if (msg.files != IntPtr.Zero)
{
Marshal.DestroyStructure(msg.files, typeof(MapiFileDesc));
Marshal.FreeHGlobal(msg.files);
msg = null;
}
}
private static IntPtr GetFileDesc(string filePath)
{
if (filePath != null && filePath.Length > 0)
{
Type mapiType = typeof(MapiFileDesc);
int size = Marshal.SizeOf(mapiType);
IntPtr mapiPtr = Marshal.AllocHGlobal(size);
MapiFileDesc mapiDesc = new MapiFileDesc();
mapiDesc.position = -1;
mapiDesc.fileName = Path.GetFileName(filePath);
mapiDesc.pathName = filePath;
Marshal.StructureToPtr(mapiDesc, mapiPtr, false);
return mapiPtr;
}
return IntPtr.Zero;
}
private static void Logoff(IntPtr hwnd, ref IntPtr session)
{
if (session != IntPtr.Zero)
{
MAPI.MAPILogoff(session, hwnd, 0, 0);
session = IntPtr.Zero;
}
}
private static bool Logon(IntPtr hwnd, ref IntPtr session)
{
int error = MAPI.MAPILogon(hwnd, null, null, 0, 0, ref session);
if (error != 0)
{
error = MAPI.MAPILogon(hwnd, null, null, MapiLogonUI, 0, ref session);
}
return error == 0;
}
#endregion
#region Imports
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int _controlfp(int n, int mask);
[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi)]
private static extern int MAPILogoff(IntPtr session, IntPtr hwnd, int flags, int reserved);
[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi)]
private static extern int MAPILogon(IntPtr hwnd, string profileName, string password, int flags, int reserved, ref IntPtr session);
[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi)]
private static extern int MAPISendMail(IntPtr session, IntPtr uiParam, MapiMessage message, int flags, int reserved);
#endregion
#region Fields
private const int MAPI_DIALOG = 8;
private const int MapiLogonUI = 1;
#endregion
public static void SendEMail(string subject, string body, string filePath)
{
try
{
IntPtr handle = IntPtr.Zero;
IntPtr session = IntPtr.Zero;
if (Logon(handle, ref session))
{
MapiMessage message = CreateMessage(subject, body, filePath);
MAPI.MAPISendMail(session, handle, message, MAPI_DIALOG, 0);
Logoff(handle, ref session);
DisposeMessage(message);
}
}
finally
{
const int _MCW_EW = 0x8001F;
const int _EM_INVALID = 0x10;
_controlfp(_MCW_EW, _EM_INVALID);
}
}
private MAPI()
{
}
}
}
Thank you.