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、msg、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 に答える