我创建了一个这样的Outlook邮件:
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = mailSubject;
mailItem.To = "";
mailItem.Attachments.Add(totalPath);
mailItem.Body = mailBody;
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
mailItem.Display(true);
//Mail is send succesfully?
是否可以检查邮件是成功发送还是取消?
编辑: 当我执行以下代码时,会出现错误'项目被移动或删除'
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = mailSubject;
mailItem.To = "";
mailItem.CC = "EDI.Orders@trust.com";
mailItem.Attachments.Add(totalPath);
mailItem.Body = mailBody;
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
mailItem.Display(true);
//Mail is send succesfully?
if (mailItem.Sent)
{
MessageBox.Show("Sended");
}
else
{
MessageBox.Show("canceled");
}
错误出现在mailItem.Sent上。 我的问题是mailItem.Sent是如何工作的?
答案 0 :(得分:1)
mailItem.Sent
属性显示是否已发送电子邮件。
此外,您可以附加到mailItem.Write
事件。 (但请注意 - 也会因为保存等而被召唤)
mailItem.Write += mailItem_Write;
void mailItem_Write(ref bool Cancel)
{
//your code...
}
答案 1 :(得分:1)
MailItem.Sent
属性返回一个布尔值,指示是否已发送消息。
答案 2 :(得分:1)
对于你的问题;
MailItem.Sent属性(Outlook)
返回一个布尔值,指示是否已发送消息。 只读。
https://msdn.microsoft.com/EN-US/library/office/ff868242.aspx
在调用Display之后检查此属性时,我怀疑当项目不再处于活动状态时,对Sent
的调用失败。确定邮件是否确实已发送的一种方法是处理Outlook.Application类的ItemSend事件。
Outlook.Application outlookApp = new Outlook.Application();
outlookApp.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(outlookApp_ItemSend);
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = "Test";
mailItem.To = "a.b@c.com";
mailItem.CC = "";
mailItem.Body = "Test Message";
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
mailItem.Display(true);
在Send事件处理程序中检查消息;
void outlookApp_ItemSend(object Item, ref bool Cancel)
{
if (Item is Outlook.MailItem)
{
string subject = ((Outlook.MailItem)Item).Subject;
if (subject == "Test")
{
//Item sent
}
}
}
答案 3 :(得分:1)
这是一个不会阻止前景的解决方案。
class Class1
{
static bool mailWasSent = false;
private static System.Windows.Forms.ApplicationContext _context;
static void Main(string[] args)
{
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = "My Subject";
mailItem.To = "";
mailItem.Attachments.Add(@"C:\test.pdf");
mailItem.Body = "This is my Body-Text";
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
((Outlook.ItemEvents_10_Event)mailItem).Close += MailItem_onClose;
((Outlook.ItemEvents_10_Event)mailItem).Send += MailItem_onSend;
//mailItem.Display(true); // This call will make mailItem MODAL -
// This way, you are not allowed to create another new mail, ob browse Outlook-Folders while mailItem is visible
// Using ApplicationContext will wait until your email is sent or closed without blocking other Outlook actions.
using (_context = new System.Windows.Forms.ApplicationContext())
{
mailItem.Display();
System.Windows.Forms.Application.Run(_context);
}
if (mailWasSent)
{
System.Windows.Forms.MessageBox.Show("Email was sent");
}
else
{
System.Windows.Forms.MessageBox.Show("Email was NOT sent");
}
}
private static void MailItem_onSend(ref bool Cancel)
{
mailWasSent = true;
}
private static void MailItem_onClose(ref bool Cancel)
{
_context.ExitThread();
}
}