C#中使用MAPI发送E-Mail

  以下是从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;

  /**

  /// Create and Send a Mail

  ///

  /// Application.Run(form) OR form.ShowDialog() form.Handle

  ///

  ///

  ///

  ///

  ///

  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))

  {

  MapiMessage msg = CreateMessage();

  error = MAPISendMail(session, handle, msg, MAPI_DIALOG, 0);

  Logoff();

  DisposeMessage(msg);

  }

  }

  private MapiMessage CreateMessage()

  {

  MapiMessage msg = new MapiMessage();

  msg.subject = subject;

  msg.noteText = body;

  if (files != null && files.Length > 0)

  {

  msg.fileCount = files.Length;

  msg.files = GetFilesDesc();

  }

  if (!string.IsNullOrEmpty(recipentName) || !string.IsNullOrEmpty(recipientAddress))

  {

  msg.recipCount = 1;

  msg.recips = GetRecipDesc();

  }

  return msg;

  }

  private IntPtr GetFilesDesc()

  {

  lcbjz.blog.jp

  wzbjz.blog.jp

  blog.livedoor.jp/lanzj

  czzj.blog.jp

  labjz.blog.jp

  czbjz.blog.jp

  chuz.blog.jp

  zzmz.blog.jp

  jcbjz.blog.jp

  nbbjz.blog.jp

  IntPtr ptra = AllocMemory(typeof(MapiFileDesc), files.Length);

  for (int i = 0; i < files.Length; i++)

  {

  MapiFileDesc fileDesc = new MapiFileDesc();

  fileDesc.position = i;

  fileDesc.fileName = Path.GetFileName(files[i]);

  fileDesc.pathName = files[i];

  Marshal.StructureToPtr(fileDesc, OffsetPtr(ptra, typeof(MapiFileDesc), files.Length - 1 - i), false);

  }

  return ptra;

  }

  private IntPtr GetRecipDesc()

  {

  IntPtr ptra = AllocMemory(typeof(MapiRecipDesc), 1);

  MapiRecipDesc recipDesc = new MapiRecipDesc();

  recipDesc.reserved = 0;

  const int MAPI_TO = 1;

  recipDesc.recipClass = MAPI_TO;

  recipDesc.name = recipentName;

  recipDesc.address = recipientAddress;

  recipDesc.eIDSize = 0;

  recipDesc.entryID = IntPtr.Zero;

  Marshal.StructureToPtr(recipDesc, (IntPtr)ptra, false);

  return ptra;

  }

  private IntPtr AllocMemory(Type structureType, int count)

  {

  return Marshal.AllocHGlobal(Marshal.SizeOf(structureType) * count);

  }

  private void Logoff()

  {

  if (session != IntPtr.Zero)

  {

  error = MAPILogoff(session, handle, 0, 0);

  session = IntPtr.Zero;

  }

  }

  private bool Logon(IntPtr hwnd)

  {

  this.handle = hwnd;

  error = MAPILogon(hwnd, null, null, 0, 0, ref session);

  if (error != 0)

  error = MAPILogon(hwnd, null, null, MapiLogonUI, 0, ref session);

  return error == 0;

  }

  private void DisposeMessage(MapiMessage msg)

  {

  FreeMemory(msg.files, typeof(MapiFileDesc), files.Length);

  FreeMemory(msg.recips, typeof(MapiRecipDesc), 1);

  msg = null;

  }

  /**

  /// Release Resource

  ///

  ///

  ///

  ///

  private void FreeMemory(IntPtr ptr, Type structureType, int count)

  {

  if (ptr != IntPtr.Zero)

  {

  for (int i = 0; i < count; i++)

  {

  Marshal.DestroyStructure(OffsetPtr(ptr, structureType, i), structureType);

  }

  Marshal.FreeHGlobal(ptr);

  }

  }

  }

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值