2013년 1월 31일 목요일

How To: Add an attachment to an Outlook e-mail message

How To: Add an attachment to an Outlook e-mail message



C# and Add-in Express:
private void AddAttachmentFromFile(Outlook._Application OutlookApp) 
    Outlook.Inspector activeInspector = OutlookApp.ActiveInspector(); 
    if (activeInspector != null
    { 
        Outlook.MailItem activeMailItem = activeInspector.CurrentItem as Outlook.MailItem; 
        if (activeMailItem != null
        { 
            Outlook.Attachments mailAttachments = activeMailItem.Attachments; 
            // the last two arguments can be applied if the Rich Text format is used 
            Outlook.Attachment newAttachment = mailAttachments.Add( 
                @"C:\Text Document.txt", Outlook.OlAttachmentType.olByValue, 
                1, "The test attachment"); 
            activeMailItem.Save(); 
            // don't forget to release underlying COM objects 
            if (newAttachment != null) Marshal.ReleaseComObject(newAttachment); 
            if (mailAttachments != null) Marshal.ReleaseComObject(mailAttachments); 
            Marshal.ReleaseComObject(activeMailItem); 
        } 
        Marshal.ReleaseComObject(activeInspector); 
    } 
VB.NET and Add-in Express:
Private Sub AddAttachmentFromFile(OutlookApp As Outlook._Application) 
    Dim activeInspector As Outlook.Inspector = OutlookApp.ActiveInspector() 
    If Not IsNothing(activeInspector) Then 
        Dim activeMailItem As Outlook.MailItem = activeInspector.CurrentItem 
        If Not IsNothing(activeMailItem) Then 
            Dim mailAttachments As Outlook.Attachments = activeMailItem.Attachments 
            ' the last two arguments can be applied if the Rich Text format is used 
            Dim newAttachment As Outlook.Attachment = mailAttachments.Add( _ 
                "C:\Text Document.txt", Outlook.OlAttachmentType.olByValue, _ 
                 1, "The test attachment"
            activeMailItem.Save() 
            ' don't forget to release underlying COM objects 
            If Not IsNothing(newAttachment) Then 
                Marshal.ReleaseComObject(newAttachment) 
            End If 
            If Not IsNothing(mailAttachments) Then 
                Marshal.ReleaseComObject(mailAttachments) 
            End If 
            Marshal.ReleaseComObject(activeMailItem) 
        End If 
        Marshal.ReleaseComObject(activeInspector) 
    End If 
End Sub 
C# and VSTO:
using System.Runtime.InteropServices; 
// ... 
private void AddAttachmentFromFile(Outlook.Application Application) 
    Outlook.Inspector activeInspector = Application.ActiveInspector(); 
    if (activeInspector != null
    { 
        Outlook.MailItem activeMailItem = activeInspector.CurrentItem as Outlook.MailItem; 
        if (activeMailItem != null
        { 
            Outlook.Attachments mailAttachments = activeMailItem.Attachments; 
            // the last two arguments can be applied if the Rich Text format is used 
            Outlook.Attachment newAttachment = mailAttachments.Add( 
               @"C:\Text Document.txt", Outlook.OlAttachmentType.olByValue, 
               1, "The test attachment"); 
            activeMailItem.Save(); 
            // don't forget to release underlying COM objects 
            if (newAttachment != null) Marshal.ReleaseComObject(newAttachment); 
            if (mailAttachments != null) Marshal.ReleaseComObject(mailAttachments); 
            Marshal.ReleaseComObject(activeMailItem); 
         } 
         Marshal.ReleaseComObject(activeInspector); 
     } 
VB.NET and VSTO:
Imports System.Runtime.InteropServices 
' ... 
Private Sub AddAttachmentFromFile(Application As Outlook.Application) 
    Dim activeInspector As Outlook.Inspector = Application.ActiveInspector() 
    If Not IsNothing(activeInspector) Then 
        Dim activeMailItem As Outlook.MailItem = activeInspector.CurrentItem 
        If Not IsNothing(activeMailItem) Then 
            Dim mailAttachments As Outlook.Attachments = activeMailItem.Attachments 
            ' the last two arguments can be applied if the Rich Text format is used 
            Dim newAttachment As Outlook.Attachment = mailAttachments.Add( _ 
                "C:\Text Document.txt", Outlook.OlAttachmentType.olByValue, _ 
                1, "The test attachment"
            activeMailItem.Save() 
            ' don't forget to release underlying COM objects 
            If Not IsNothing(newAttachment) Then 
                Marshal.ReleaseComObject(newAttachment) 
            End If 
            If Not IsNothing(mailAttachments) Then 
                Marshal.ReleaseComObject(mailAttachments) 
            End If 
            Marshal.ReleaseComObject(activeMailItem) 
        End If 
        Marshal.ReleaseComObject(activeInspector) 
    End If 
End Sub 
See you on our forums and in the e-mail support!




http://www.add-in-express.com/creating-addins-blog/2011/08/12/how-to-add-existing-e-mail-message-as-attachment/
http://www.add-in-express.com/creating-addins-blog/2011/08/10/how-to-add-attachment-to-e-mail-message/

댓글 없음: