Can’t delete email using MAPI

I’ve created Outlook 2019 add-in in C# and trying to use DeleteMessages method from MAPI to delete single email. However I’m always getting E_INVALIDARG as a result (look at code below).

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, let’s assume that I only support x64).

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 snippets to call DeleteMessages I’ve tried so far (item is MailItem object):

1.==================

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

2.==================

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

int idLen = msgEntryId.Length;

IntPtr pMsgCom = Marshal.AllocCoTaskMem(12);
Marshal.WriteInt32(pMsgCom, 1);
IntPtr pArrayCom = Marshal.AllocCoTaskMem(12);
Marshal.WriteInt64(pMsgCom, 4, (long)pArrayCom);
Marshal.WriteInt32(pArrayCom, idLen);
IntPtr entryBytesCom = Marshal.AllocCoTaskMem(idLen);
Marshal.WriteInt64(pArrayCom, 4, (long)entryBytesCom);
Marshal.Copy(msgEntryId, 0, entryBytesCom, idLen);

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

3.==================

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

int idLen = msgEntryId.Length;

IntPtr pMsgCom = Marshal.AllocCoTaskMem(16);
Marshal.WriteInt32(pMsgCom, 1);
IntPtr pArrayCom = Marshal.AllocCoTaskMem(16);
Marshal.WriteInt64(pMsgCom, 8, (long)pArrayCom);
Marshal.WriteInt32(pArrayCom, 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);

4.==================

[StructLayout(LayoutKind.Sequential)]
public struct SBinary
{
    public uint cb;
    public IntPtr lpb;

    public static SBinary SBinaryCreate(byte[] data)
    {
        SBinary b;
        b.cb = (uint)data.Length;
        b.lpb = Marshal.AllocHGlobal((int)b.cb);
        for (int i = 0; i < b.cb; i++)
        {
            Marshal.WriteByte(b.lpb, i, data[i]);
        }
        return b;
    }
}

[StructLayout(LayoutKind.Sequential)]
internal struct SBinaryArray
{
    public uint cValues;
    public IntPtr lpbin;

    public static SBinaryArray SBinaryArrayCreate(uint length, IntPtr buffer)
    {
        SBinaryArray b;
        b.cValues = length;
        b.lpbin = buffer;
        return b;
    }
}

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

SBinary messageBin = SBinary.SBinaryCreate(msgEntryId);
IntPtr messageBinPtr = Marshal.AllocHGlobal(Marshal.SizeOf(messageBin));
Marshal.StructureToPtr(messageBin, messageBinPtr, false);

SBinaryArray messageBinArray = SBinaryArray.SBinaryArrayCreate(1, messageBinPtr);
IntPtr messageBinArrayPtr = Marshal.AllocHGlobal(Marshal.SizeOf(messageBinArray));
Marshal.StructureToPtr(messageBinArray, messageBinArrayPtr, false);

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

I also tried to use OpenEntry (from MAPISession) and then Marshal.GetObjectForIUnknown to get IMAPIFolder object. However the result was the same and got E_INVALIDARG for DeleteMessages. Any ideas why DeleteMessages returns E_INVALIDARG?

DeleteMesages takes SBinaryArray. Its first element (cValues) is always 4 bytes, not 8 (you are using WriteInt64). Next is the pointer to the entry id (if you specify only one entry). Its size is 4 bytes under x86 and 8 bytes under x64. Use IntPtr.Size.

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật