Outlook: Handle recieved delivery notification and delivery receipt

Add-in Express™ Support Service
That's what is more important than anything else

Outlook: Handle recieved delivery notification and delivery receipt
 
abel




Posts: 3
Joined: 2018-04-10
Hello,

I've created an addin for Outlook. When the user clicks one of the buttons, the selected mail (Outlook.MailItem) is added as an attachment to a new mail. That works fine.
Some users have asked me if they can do this with a recieved delivery notification / delivery receipt. Of course Outlook.MailItem does not work here.

So, is there a way to do this? I searched but only found information about adding a delivery notification / delivery receipt to a new mail. But I need to interact with a recieved item from a mail I sent before.

I have simplified my code and posted it below if you want to know exactly what I did in my method. :)

Thank you. Best regards.


private void SendMailAsAttachment()
{
	Outlook.Attachments attachments = null;
	Outlook.NameSpace nSpace = null;
	Outlook.MAPIFolder targetFolder = null;
	Outlook.Items folderItems = null;
	Outlook.MailItem newMailItem = null;
	Outlook.MailItem mailAttachment = null;
	List<Outlook.MailItem> list_mailAttachments = new List<Outlook.MailItem>();

	try
	{
		if (OutlookApp != null)
		{
			nSpace = OutlookApp.GetNamespace("MAPI");
			targetFolder = nSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
			folderItems = targetFolder.Items;
			newMailItem = folderItems.Add(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
			if (OutlookApp.ActiveExplorer() != null)
			{
				Outlook._Explorer explorer = OutlookApp.ActiveExplorer();
				int iCountSelected = explorer.Selection.Count;
				for (int i = 1; i <= iCountSelected; i++)
				{
					mailAttachment = explorer.Selection[i] as Outlook.MailItem;
					if (mailAttachment != null)
					{
						list_mailAttachments.Add(mailAttachment);
					}
					mailAttachment = null;
				}
			}
			newMailItem.Subject = "any subject";
			newMailItem.Recipients.Add("anyname@company.tld");
			newMailItem.HTMLBody = "any mailtext";
			attachments = newMailItem.Attachments;
			foreach (Outlook.MailItem item in list_mailAttachments) 
			{
				attachments.Add(item, Outlook.OlAttachmentType.olEmbeddeditem, 1, item.Subject.ToString());
			}
			newMailItem.Save();
			newMailItem.Display(false);
		}
	}
	catch (Exception ex)
	{
		System.Windows.Forms.MessageBox.Show(ex.Message);
	}
	finally
	{
		if (attachments != null) Marshal.ReleaseComObject(attachments);
		if (nSpace != null) Marshal.ReleaseComObject(nSpace);
		if (targetFolder != null) Marshal.ReleaseComObject(targetFolder);
		if (folderItems != null) Marshal.ReleaseComObject(folderItems);
		if (newMailItem != null) Marshal.ReleaseComObject(newMailItem);
		if (mailAttachment != null) Marshal.ReleaseComObject(mailAttachment);
	}
}


What I forgot to tell you: I already tried this: I made a List<object> (instead of List<Outlook.MailItem>) and added every selected object to this list (during the loop
for (int i = 1; i <= iCountSelected; i++)
). But is there a way to check if this object is a delivery notification / delivery receipt?
Posted 15 Jul, 2020 05:46:20 Top
Andrei Smolin


Add-in Express team


Posts: 17306
Joined: 2006-05-11
abel writes:
delivery notification / delivery receipt


To request a delivery receipt, set MailItem.OriginatorDeliveryReportRequested = true; see https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.originatordeliveryreportrequested.

To request a read receipt (read notification), set MailItem.ReadReceiptRequested = true; see https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.readreceiptrequested.

To handle receipts when they are selected or opened, you need to know that they are of the Outlook.ReportItem type.

On another note. "if (OutlookApp.ActiveExplorer() != null)" creates a COM object that you need to release.

Your code calls explorer.Selection twice: explorer.Selection.Count and explorer.Selection[i]. Each of the calls creates a separate COM object; I suggest that you create that COM object once and release it after use. Also, be aware that Explorer.Selection may produce an exception when in certain folders such as RSS Feeds; the list of such folders depends on the Outlook version used.

Pay attention: If the user selects e.g. a ReportItem, "explorer.Selection[i] as Outlook.MailItem" will leave that ReportItem unreleased.

newMailItem.Recipients and Recipients.Add create two COM objects.

attachments.Add() returns a COM object that you need to release.

Make sure that you release every MailItem in list_mailAttachments after use.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 15 Jul, 2020 07:10:38 Top
abel




Posts: 3
Joined: 2018-04-10
Hello Andrei,

thank you very much for your quick response. Outlook.ReportItem is exactly what I need.

Your other advices are also very helpful. I see that I have to change a lot of code then (in the whole project).



Best regards
Posted 16 Jul, 2020 05:02:06 Top
Andrei Smolin


Add-in Express team


Posts: 17306
Joined: 2006-05-11
You are welcome!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 16 Jul, 2020 05:22:38 Top