2009-02-25 203 views
1

我一直使用Winforms应用程序中的Mapi32发送带有附件的新邮件消息,现在它运行得非常好。 (是的,我知道从C#调用MAPI32不受支持。)当Outlook运行时,MAPISendMail不起作用

在最近几天内,它在Outlook运行时停止工作。但是,如果Outlook未运行,它将按预期工作。这发生在Vista和XP中。

有没有这样的问题?你是如何解决它的?

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

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; 
} 
+1

调用者进程的完整性级别可能与Outlook的完整性级别不同。使用Process Explorer查找正在运行的进程的IL。 – wqw 2012-11-22 12:30:11

+0

可能与您的问题没有关系,但是我有`MAPISendMail`挂在Outlook上,因为邮件地址有空格! – 2017-11-15 12:50:30

回答

0

好了,比评明显的“支持C#邮件的发送方法使用”其他,我想知道如果事情发生在Mapi32.dll文件 - 有您在Outlook中尝试了“检测并修复”?

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

1

int result = MAPISendMail(session,winhandle,msg,MAPI_LOGON_UI | MAPI_DIALOG,0);

return result; 

是什么回报?

2

如果您的应用程序使用提升特权(即以管理员身份)运行,而Outlook不在,则发送将失败。您将不得不通知用户关闭所有正在运行的Outlook实例,然后重试。

相关问题