c# - 如何检查是否已在 WPF 项目中使用 MAPI 发送邮件?
问题描述
我无法知道用户是否已发送邮件或中止该过程。
我正在开发一个 WPF 项目,它加密 aes-256 中的文件并打开一个 Outlook 弹出窗口,您可以在其中发送加密文件。要解密文件,需要通过 SMS 向 Person 发送代码,但如果尚未发送邮件,则也不应发送 SMS。问题是我找不到确定它的方法。
try
{
SendMail(zipfile + ".aes");
if (mapi.sent == true)
SendNewSms(); //do not send the SMS if the email has not been sent
else if (mapi.sent == false)
MessageBox.Show("It didn't work!!");
}
catch
{
MessageBox.Show("Error: MAIL");
}
public void SendMail(string attachment)
{
string subject = "";
string body = "";
string attachmentPath = attachment;
mapi.AddAttachment(attachmentPath);
mapi.SendMailPopup(subject, body);
}
MAPI 类有这个应该发送邮件的代码。“sent”变量是我自己添加的布尔值
[DllImport("MAPI32.DLL")]
static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);
int SendMail(string strSubject, string strBody, int how)
{
MapiMessage msg = new MapiMessage();
msg.subject = strSubject;
msg.noteText = strBody;
msg.recips = GetRecipients(out msg.recipCount);
msg.files = GetAttachments(out msg.fileCount);
m_lastError = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, how,
0);
if (m_lastError > 1)
MessageBox.Show("MAPISendMail failed! " + GetLastError(), "MAPISendMail");
Cleanup(ref msg);
return m_lastError;
}
我的期望是,如果邮件已发送,布尔值将设置为 true,否则它将保持为 false。
希望写得通俗易懂!
解决方案
简单 MAPI 不提供是否发送消息的任何指示。但是,您可以跟踪已发送邮件文件夹的更改。将项目添加到文件夹后,您的消息就已发送。
在 Outlook 对象模型中,您可以找到MailItem.Sent属性,该属性返回一个布尔值,指示是否已发送消息。