⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netmapi.cs

📁 A (hopefully) complete extended MAPI wrapper for WinXP, WinCE, and .NET.
💻 CS
字号:
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: NetMAPI.cs
// Description: .NET Extended MAPI wrapper
//				Works in Windows CE and Windows XP with Outlook 2000 and 2003, maybe others but untested
//
// Copyright (C) 2006, Noel Dillabough
//
// This source code is free to use and modify provided this notice remains intact and that any enhancements
// or bug fixes are posted to the CodeProject page hosting this class for all to benefit.
//
// Usage: see the Codeproject article at http://www.codeproject.com
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace MAPIEx {
	public enum Priority { IMPORTANCE_LOW, IMPORTANCE_NORMAL, IMPORTANCE_HIGH };
	public enum RecipientType { MAPI_TO=1, MAPI_CC, MAPI_BCC };

	/// <summary>
	/// Extended MAPI Wrapper for .NET, to use a Unicode version change nCharSet and compile MAPIEx for Unicode.
	/// Note: Very thin wrapper that makes many assumpions; for example it opens the default store; feel free to 
	/// extend this for your needs.  
	/// </summary>
	public class NetMAPI {
		private const CharSet nCharSet=CharSet.Ansi;
		private const int nBufferSize=1024;

		#region DLLCalls
		[DllImport("MAPIEx.dll", EntryPoint="MAPIInit", CharSet=nCharSet)]
		public static extern bool Init();

		[DllImport("MAPIEx.dll", EntryPoint="MAPITerm", CharSet=nCharSet)]
		public static extern void Term();

		[DllImport("MAPIEx.dll", EntryPoint="MAPICreateMessage", CharSet=nCharSet)]
		public static extern bool CreateMessage(string strSubject, string strBody, Priority nPriority);

		[DllImport("MAPIEx.dll", EntryPoint="MAPIAddRecipient", CharSet=nCharSet)]
		public static extern bool AddRecipient(string strEmail, RecipientType nType);

		[DllImport("MAPIEx.dll", EntryPoint="MAPIAddAttachment", CharSet=nCharSet)]
		public static extern bool AddAttachment(string strPath);

		[DllImport("MAPIEx.dll", EntryPoint="MAPISend", CharSet=nCharSet)]
		public static extern bool Send();

		[DllImport("MAPIEx.dll", EntryPoint="MAPIGetContents", CharSet=nCharSet)]
		public static extern bool GetContents();

		[DllImport("MAPIEx.dll", CharSet=nCharSet)]
		public static extern bool MAPIGetNextMessage(bool bUnreadOnly);

		[DllImport("MAPIEx.dll", CharSet=nCharSet)]
		public static extern void MAPIGetSenderName(StringBuilder s, int nMaxLength);

		[DllImport("MAPIEx.dll", CharSet=nCharSet)]
		public static extern void MAPIGetSenderEmail(StringBuilder s, int nMaxLength);

		[DllImport("MAPIEx.dll", CharSet=nCharSet)]
		public static extern void MAPIGetSubject(StringBuilder s, int nMaxLength);

		[DllImport("MAPIEx.dll", CharSet=nCharSet)]
		public static extern int MAPIGetBodyLength();

		[DllImport("MAPIEx.dll", CharSet=nCharSet)]
		public static extern void MAPIGetBody(StringBuilder s, int nMaxLength);

		[DllImport("MAPIEx.dll", CharSet=nCharSet)]
		public static extern bool MAPIHasAttachments();

		[DllImport("MAPIEx.dll", CharSet=nCharSet)]
		public static extern bool MAPISaveAttachments(StringBuilder s, int nIndex);
		#endregion

		public static bool GetNextMessage(ref Message m,bool bUnreadOnly) {
			if(!MAPIGetNextMessage(bUnreadOnly)) return false;

			int nLen=MAPIGetBodyLength();
			if(nLen<nBufferSize) nLen=nBufferSize;
			StringBuilder s=new StringBuilder(nLen);

			MAPIGetSenderName(s,nLen);m.SenderName=s.ToString();
			MAPIGetSenderEmail(s,nLen);m.SenderEmail=s.ToString();
			MAPIGetSubject(s,nLen);m.Subject=s.ToString();
			MAPIGetBody(s,nLen);m.Body=s.ToString();
			return true;
		}

		public static bool SendEmail(string strEmail, string strSubject, string strBody, string strAttachment) {
			if(NetMAPI.CreateMessage(strSubject,strBody,Priority.IMPORTANCE_NORMAL)) {
				NetMAPI.AddRecipient(strEmail,RecipientType.MAPI_TO);
				if(strAttachment.Length>0 && !NetMAPI.AddAttachment(strAttachment)) return false;
				return NetMAPI.Send();
			}
			return false;
		}
	}

	public class Message {
		public string SenderName;
		public string SenderEmail;
		public string Subject;
		public string Body;

		public Message() {}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -