简体   繁体   English

无法使用 MAPI 删除 email

[英]Can't delete email using MAPI

I've created Outlook add-in in C# and trying to use DeleteMessages method from MAPI to delete email.我在 C# 中创建了 Outlook 加载项,并尝试使用 MAPI 中的 DeleteMessages 方法删除 email。 However I'm always getting E_INVALIDARG as a result (look at code below).但是我总是得到 E_INVALIDARG 结果(请看下面的代码)。

I've checked following article: https://www.codeproject.com/Articles/455823/Managed-MAPI-Part-1-Logon-MAPI-Session-and-Retriev , did some own changes and got the code below (only necessary code for IMAPIFolder and HRESULT).我检查了以下文章: https://www.codeproject.com/Articles/455823/Managed-MAPI-Part-1-Logon-MAPI-Session-and-Retriev ,做了一些自己的更改并得到了下面的代码(仅IMAPIFolder 和 HRESULT 的必要代码)。

public interface IMAPIFolder
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    HRESULT DeleteMessages(IntPtr lpMsgList, uint ulUIParam, IntPtr lpProgress, uint ulFlags);
}
public enum HRESULT
{
    E_INVALIDARG = 0x80070057
}
public class EntryID
{
    private byte[] id_;
    
    public EntryID(byte[] id) 
    {
        id_ = id;
    }

    public static EntryID GetEntryID(string entryID)
    {
        if (string.IsNullOrEmpty(entryID))
            return null;
        int count = entryID.Length / 2;
        StringBuilder s = new StringBuilder(entryID);
        byte[] bytes = new byte[count];
        for (int i = 0; i < count; i++)
        {
            if ((2 * i + 2) > s.Length)
                return null;
            string s1 = s.ToString(2 * i, 2);
            if (!Byte.TryParse(s1, System.Globalization.NumberStyles.HexNumber, null as IFormatProvider, out bytes[i]))
                return null;
        }
        return new EntryID(bytes);
    }
}

Code snippet to call DeleteMessages - item is MailItem object:调用 DeleteMessages 的代码片段 - 项目是 MailItem object:

string mailItemEntryId = ((MailItem)item).EntryID
byte[] msgEntryId = EntryID.GetEntryID(mailItemEntryId).AsByteArray;

int idLen = msgEntryId.Length;

IntPtr pMsgCom = Marshal.AllocCoTaskMem(16);
Marshal.WriteInt64(pMsgCom, 1L);
IntPtr pArrayCom = Marshal.AllocCoTaskMem(16);
Marshal.WriteInt64(pMsgCom, 8, (long)pArrayCom);
Marshal.WriteInt64(pArrayCom, (long)idLen);
IntPtr entryBytesCom = Marshal.AllocCoTaskMem(idLen);
Marshal.WriteInt64(pArrayCom, 8, (long)entryBytesCom);
Marshal.Copy(msgEntryId, 0, entryBytesCom, idLen);

var mapifolder = (IMAPIFolder)((MAPIFolder)mailItem.Parent).MAPIOBJECT
var result = mapifolder.DeleteMessages(pMsgCom, 0, IntPtr.Zero, 0);

I also tried to use OpenEntry (from MAPISession) and then Marshal.GetObjectForIUnknown to get IMAPIFolder object.我还尝试使用 OpenEntry(来自 MAPISession)然后 Marshal.GetObjectForIUnknown 来获取 IMAPIFolder object。 However the result was the same and got E_INVALIDARG for DeleteMessages.但是结果是相同的,并为 DeleteMessages 获得了 E_INVALIDARG。 Any ideas why DeleteMessages returns E_INVALIDARG?任何想法为什么 DeleteMessages 返回 E_INVALIDARG?

DeleteMesages takes SBinaryArray . DeleteMesages需要SBinaryArray Its first element ( cValues ) is always 4 bytes, not 8 (you are using WriteInt64 ).它的第一个元素( cValues )始终是 4 个字节,而不是 8 个(您正在使用WriteInt64 )。 Next is the pointer to the entry id (if you specify only one entry).接下来是指向条目 id 的指针(如果您只指定一个条目)。 Its size is 4 bytes under x86 and 8 bytes under x64.它的大小在 x86 下为 4 字节,在 x64 下为 8 字节。 Use IntPtr.Size .使用IntPtr.Size

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM