none
How to delete the attachment for a particular to address in outlook using c# RRS feed

  • Question

  • I want to remove the attachments from outlook for a specific to address(for example if to address contain gamil.com then i want to delete the attachments) at the mail sending evevt. When we use multiple to address it give problems. I use the below code.

    when i use multiple to address (like, xxx.gmail.com,yyy.yahooo.com) it remove the attachments for both mails.

    pls help,

    Thanks in advance....

    string[] toa = mail.To.Split(';'); 
    
    for (int a = 0; a <= toa.Count(); a++)
         {
           if(toa[a].Contains("gmail.com"))
           {
            //remove attachments
           }
         }			
    Thursday, April 30, 2015 8:03 AM

Answers

  • Hello,

    You can handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.

    Use the Recipients property (a Recipients collection that represents all the recipients for the Outlook item) of the MailItem class instead of the To property.

    Use Recipients(index) or Recipients.Item(index), where index is the name or index number, to return a single Recipient object. The name can be a string representing the display name, the alias, or the full SMTP e-mail address of the recipient.

    The Recipient class provides the Address property which returns a string representing the e-mail address of the Recipient.

    P.S. You can't remove the attachment for a specific email address. You need to send separate emails individually - with an attachment and without. In the ItemSend event handler you can check out the Recipients list and copy the source item if you need to send the item without an attachment. Remove the "blocked" recipients from the original mail item and set them on the copied one. Then remove the attachment and send the copy.

    Thursday, April 30, 2015 1:07 PM

All replies

  • Hello,

    You can handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.

    Use the Recipients property (a Recipients collection that represents all the recipients for the Outlook item) of the MailItem class instead of the To property.

    Use Recipients(index) or Recipients.Item(index), where index is the name or index number, to return a single Recipient object. The name can be a string representing the display name, the alias, or the full SMTP e-mail address of the recipient.

    The Recipient class provides the Address property which returns a string representing the e-mail address of the Recipient.

    P.S. You can't remove the attachment for a specific email address. You need to send separate emails individually - with an attachment and without. In the ItemSend event handler you can check out the Recipients list and copy the source item if you need to send the item without an attachment. Remove the "blocked" recipients from the original mail item and set them on the copied one. Then remove the attachment and send the copy.

    Thursday, April 30, 2015 1:07 PM
  • If you want messages with different content to be sent to different recipients, you have no choice but to create separate  messages. When processing the message being sent, create a new message (MailItem.Copy), remove the first set of recipients and the attachments from the original message, remove the second set of recipients from the copy. Send the copy.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, April 30, 2015 7:11 PM