1

一段时间以来,我一直在使用 Winforms 应用程序中的 Mapi32 发送带有附件的新邮件消息,并且效果非常好。(是的,我知道不支持从 C# 调用 MAPI32。)

在过去的几天内,当 Outlook 运行时,它已停止工作。但是,如果 Outlook 未运行,它将按预期工作。这在 Vista 和 XP 中都会发生。

有没有 SOer 遇到过这个问题?你是怎么解决的?

这是我一直在使用的代码:

public class EmailController
{
    [DllImport("MAPI32.DLL", CharSet = CharSet.Ansi)]
    public static extern int MAPISendMail(IntPtr lhSession, IntPtr ulUIParam,
        MapiMessage lpMessage, int flFlags, int ulReserved);
    public const int MAPI_LOGON_UI = 0x00000001;
    private const int MAPI_DIALOG = 0x00000008;

    public static int SendMail(string strAttachmentFileName, string strSubject,string to)
    {

        IntPtr session = new IntPtr(0);
        IntPtr winhandle = new IntPtr(0);

        MapiMessage msg = new MapiMessage();
        msg.subject = strSubject;

        int sizeofMapiDesc = Marshal.SizeOf(typeof(MapiFileDesc));
        IntPtr pMapiDesc = Marshal.AllocHGlobal(sizeofMapiDesc);

        MapiFileDesc fileDesc = new MapiFileDesc();
        fileDesc.position = -1;
        int ptr = (int)pMapiDesc;

        string path = strAttachmentFileName;
        fileDesc.name = Path.GetFileName(path);
        fileDesc.path = path;
        Marshal.StructureToPtr(fileDesc, (IntPtr)ptr, false);

        msg.files = pMapiDesc;
        msg.fileCount = 1;


        List<MapiRecipDesc> recipsList = new List<MapiRecipDesc>();
        MapiRecipDesc recipient = new MapiRecipDesc();

        recipient.recipClass = 1;
        recipient.name = to;
        recipsList.Add(recipient);

        int size = Marshal.SizeOf(typeof(MapiRecipDesc));
        IntPtr intPtr = Marshal.AllocHGlobal(recipsList.Count * size);

        int recipPtr = (int)intPtr;
        foreach (MapiRecipDesc mapiDesc in recipsList)
        {
            Marshal.StructureToPtr(mapiDesc, (IntPtr)recipPtr, false);
            recipPtr += size;
        }

        msg.recips = intPtr;
        msg.recipCount = 1;
        int result = MAPISendMail(session, winhandle, msg, MAPI_LOGON_UI | MAPI_DIALOG, 0);

        return result;
    }
}

[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 MapiFileDesc
{
    public int reserved;
    public int flags;
    public int position;
    public string path;
    public string name;
    public IntPtr type;
}

[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;
}
4

3 回答 3

4

如果您的应用程序以提升的权限(即作为管理员)运行而 Outlook 不是,则发送将失败。您必须告诉用户关闭所有正在运行的 Outlook 实例并重试。

于 2013-07-19T04:10:09.670 回答
1

int 结果 = MAPISendMail(会话,winhandle,味精,MAPI_LOGON_UI | MAPI_DIALOG,0);

   return result;

它返回什么?

于 2009-10-12T14:07:15.010 回答
0

好吧,除了明显的“使用支持的 C# 邮件发送方法”注释之外,我想知道 mapi32.dll 文件是否发生了问题 - 您是否尝试过 Outlook 中的“检测和修复”?

我还在这里( http://office.microsoft.com/en-us/outlook/HP011164781033.aspx )读到,您可以执行一些步骤(帖子和评论)来让 Outlook 修复或替换 dll。

于 2009-02-25T19:10:36.070 回答