简体   繁体   English

如何从C#中的草稿文件夹发送Outlook电子邮件

[英]how to send outlook email from draft folder in c#

I am trying to send an outlook email item from MAPI Draft Folder in c# but no success. 我正在尝试从c#中的MAPI草稿文件夹发送一个Outlook电子邮件项目,但是没有成功。 I am still searching how to send the MAPI Folder (folderDrafts) Items, casting it to MailItem is not applicable. 我仍在搜索如何发送MAPI文件夹(folderDrafts)项目,将其强制转换为MailItem不适用。

Here is the code I used: 这是我使用的代码:

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace nameSpace = app.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder folderDrafts = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderDrafts);

Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
mailItem.Subject = "Test Outlook Mail Item";
mailItem.To = "receiver";
mailItem.HTMLBody = "html";
mailItem.Save();
mailItem = (Microsoft.Office.Interop.Outlook.MailItem)folderDrafts.Items[0];
mailItem.Send();

Maybe someone can help. 也许有人可以帮忙。 Thanks in advance! 提前致谢!

There is no need to create a new MailItem object if you just need to send items stored in the Draft Items folder. 如果只需要发送存储在“草稿项目”文件夹中的项目,则无需创建新的MailItem对象。

Be aware, various Outlook items can be stored in the folder, not only mail items. 请注意,各种Outlook项目可以存储在文件夹中,而不仅仅是邮件项目。 So, you need to check out the MessageClass before casting the item to the MailItem class in the code. 因此,您需要在将项目强制转换为代码中的MailItem类之前签出MessageClass。 For example, in C# you can use the is or as operators. 例如,在C#中,您可以使用is或as运算符。 Also you can check out the MessageClass property of the item using the late-binding technology, see Type.InvokeMember. 另外,您也可以使用后期绑定技术签出项目的MessageClass属性,请参阅Type.InvokeMember。

You may find the How To: Create and send an Outlook message programmatically article helpful. 您可能会发现“ 如何:以编程方式创建和发送Outlook消息”文章很有帮助。

摆脱以下行:

mailItem = (Microsoft.Office.Interop.Outlook.MailItem)folderDrafts.Items[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM