C++ SimpleMAPI SendMail 总是失败?

发布于 2024-07-13 13:41:47 字数 1592 浏览 5 评论 0原文

我正在尝试使用 SimpleMAPI 来显示在 Vista SP1 上使用 Windows Mail 或 Thunderbird 在 C++ 应用程序 (Borland C++ Builder 2006) 中与附件进行“写消息”对话。 我应该能够使用 MAPISendMail 来做这。

我没有填写收件人地址,因为我希望用户在邮件客户端显示“写消息”对话框时填写收件人地址。 我也没有填写发件人地址,因为我希望邮件客户端使用默认地址。 我尝试过对它们进行硬编码,看看这是否是问题所在,但事实并非如此。

我的代码如下所示:

HINSTANCE hMAPI;
LPMAPISENDMAIL pSendMail;
MapiMessage message;
MapiFileDesc file;

ZeroMemory( &message, sizeof( MapiMessage ) );
ZeroMemory( &file, sizeof( MapiFileDesc ) );

hMAPI = LoadLibraryA( "MAPI32.DLL" );

pSendMail = (LPMAPISENDMAIL)GetProcAddress( hMAPI, "MAPISendMail" );

// setup the attachment...
file.nPosition     = -1;
file.lpszPathName  = "C:\\my_attachment.dat";

// set up the message...
message.lpszSubject     = "My Subject";
message.lpszNoteText    = "My Message...";
message.lpszMessageType = "";
message.nRecipCount     = 0;
message.lpRecips        = NULL; // we don't know the recipient address(s)
message.nFileCount      = 1;
message.lpFiles         = &file;
message.lpOriginator    = NULL; // we don't know the users from address

dwResult = pSendMail( lhSessionNull, (DWORD)Application->Handle, &message, MAPI_LOGON_UI | MAPI_DIALOG, 0 );
if( dwResult == SUCCESS_SUCCESS )
{ 
  // ...yay! :)
}
else
{
  // ...we always fail here with: MAPI_E_FAILURE
}

它总是失败,错误代码为 2 (MAPI_E_FAILURE)。 我究竟做错了什么?

提前谢谢了。

I am trying to use SimpleMAPI to display a 'write message' dialogue with an attachment on Vista SP1 with either Windows Mail or Thunderbird in a C++ app (Borland C++ Builder 2006). I should be able to use MAPISendMail to do this.

I don't fill in a recipient address as I expect the user to do that when the mail client displays a 'write message' dialog. I also don't fill in an originator address as I expect the mail client to use the default. I have tried hardcoding them to see if thats the problem and it is not.

My code looks like this:

HINSTANCE hMAPI;
LPMAPISENDMAIL pSendMail;
MapiMessage message;
MapiFileDesc file;

ZeroMemory( &message, sizeof( MapiMessage ) );
ZeroMemory( &file, sizeof( MapiFileDesc ) );

hMAPI = LoadLibraryA( "MAPI32.DLL" );

pSendMail = (LPMAPISENDMAIL)GetProcAddress( hMAPI, "MAPISendMail" );

// setup the attachment...
file.nPosition     = -1;
file.lpszPathName  = "C:\\my_attachment.dat";

// set up the message...
message.lpszSubject     = "My Subject";
message.lpszNoteText    = "My Message...";
message.lpszMessageType = "";
message.nRecipCount     = 0;
message.lpRecips        = NULL; // we don't know the recipient address(s)
message.nFileCount      = 1;
message.lpFiles         = &file;
message.lpOriginator    = NULL; // we don't know the users from address

dwResult = pSendMail( lhSessionNull, (DWORD)Application->Handle, &message, MAPI_LOGON_UI | MAPI_DIALOG, 0 );
if( dwResult == SUCCESS_SUCCESS )
{ 
  // ...yay! :)
}
else
{
  // ...we always fail here with: MAPI_E_FAILURE
}

It always fails with error code 2 (MAPI_E_FAILURE). What am I doing wrong?

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

网名女生简单气质 2024-07-20 13:41:47

在 Delphi 中使用类似的方法,发现它不能在线程内部工作。 (Delphi TThread 组件)我使用了完全相同的代码,并且从线程内调用总是失败,即使我使用了同步

Using similar in Delphi and discovered that it does not work from inside a thread. (Delphi TThread component) I used the exact same code and call from within the thread always failed, even though I used synchronize

最好是你 2024-07-20 13:41:47

message.lpRecips = NULL; // 我们不知道收件人地址

尝试分配 lpRecips 并将其 lpszAddress 设置为“SMTP:”

message.lpRecips = NULL; // we don't know the recipient address(s)

Try assinging lpRecips and set it's lpszAddress to "SMTP:"

痴意少年 2024-07-20 13:41:47

您必须首先登录到 MAPI 会话

LHANDLE hMapiSession;
status = lpMapiLogon(NULL, NULL, NULL, MAPI_NEW_SESSION | MAPI_LOGON_UI, 0, &hMapiSession);

,然后才能调用 SendMail()。 之后,您必须再次注销:

lpMapiLogoff(hMapiSession, NULL, 0, 0);

You have to first logon to the MAPI session

LHANDLE hMapiSession;
status = lpMapiLogon(NULL, NULL, NULL, MAPI_NEW_SESSION | MAPI_LOGON_UI, 0, &hMapiSession);

then, you can call SendMail(). And after that, you have to logoff again:

lpMapiLogoff(hMapiSession, NULL, 0, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文