以下是从DevExpress代码中摘出来的,修改前先贴出来共享一下,有更好的建议请留言
直接构造该类,会把传入参数作为默认值来调用Outlook的发送窗体,对于要求直接发送的,此类不适用
注意:构造时,传入的Handle应为Application.Run(form)或form.ShowDialog()中form的指针,或使用form.BeginInvoke()调用到窗体,否则将无法显示窗体和发送
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace HKH.Common
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
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;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class MapiRecipDesc
{
public int reserved;
public int recipClass;
public string name;
public string address;
public int eIDSize;
public IntPtr entryID;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class MapiFileDesc
{
public int reserved;
public int flags;
public int position;
public string pathName;
public string fileName;
public IntPtr fileType;
}
public class MAPI
{
static#region static
private const int MapiLogonUI = 0x00000001;
private const int MAPI_DIALOG = 0x8;
[DllImport("MAPI32.DLL", CharSet = CharSet.Ansi)]
private static extern int MAPISendMail(IntPtr session, IntPtr uiParam,
MapiMessage message, 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 MAPILogoff(IntPtr session, IntPtr hwnd,
int flags, int reserved);
static IntPtr OffsetPtr(IntPtr ptr, Type structureType, int offset)
{
return (IntPtr)((long)ptr + offset * Marshal.SizeOf(structureType));
}
#endregion
private string files;
private string subject = String.Empty;
private string body = String.Empty;
private string recipentName = String.Empty;
private string recipientAddress = String.Empty;
private IntPtr handle = IntPtr.Zero;
private IntPtr session = IntPtr.Zero;
private int error = 0;
/**//// <summary>
/// Create and Send a Mail
/// </summary>
/// <param name="handle">Application.Run(form) OR form.ShowDialog() form.Handle</param>
/// <param name="files"></param>
/// <param name="mailSubject"></param>
/// <param name="mailBody"></param>
/// <param name="recipentName"></param>
/// <param name="recipientAddress"></param>
public MAPI(IntPtr handle, string files, string mailSubject, string mailBody, string recipentName, string recipientAddress)
{
this.files = files;
this.subject = mailSubject;
this.body = mailBody;
this.recipentName = recipentName;
this.recipientAddress = recipientAddress;
if (Logon(handle))