• Loading...
UltimateMail
Attachments Property
See Also  Example Send comments on this topic to ATP, Inc.
Atp.Net.Mail Namespace > MailMessage Class : Attachments Property




Gets the collection of attachments of this message.

Syntax

Visual Basic (Declaration) 
Public ReadOnly Property Attachments As AttachmentCollection
Visual Basic (Usage)Copy Code
Dim instance As MailMessage
Dim value As AttachmentCollection
 
value = instance.Attachments
C# 
public AttachmentCollection Attachments {get;}
Managed Extensions for C++ 
public: __property AttachmentCollection* get_Attachments();
C++/CLI 
public:
property AttachmentCollection^ Attachments {
   AttachmentCollection^ get();
}

Return Value

A collection of attachments of this message.

Example

C#Copy Code
using System;
using Atp.Net.Mail;

namespace Samples
{
   
class MailExtractAttachment
   {
       
static void Main()
       {
           ExtractAttachments(
"C:\\TestMessage.eml");
       }

       
/// <summary>
       
/// Extracs attachments from a mail file.
       
/// </summary>
       
/// <param name="fileName">Mail file name.</param>
       
/// <returns>A boolean value indicating whether the extraction is successful.</returns>
       
private static bool ExtractAttachments(string fileName)
       {
           
try
           {
               
// Load the mail message from disk
               
MailMessage mail = new MailMessage();
               mail.Load(fileName);

               
// DecryptData the message if it is encrypted
               
if (mail.IsEncrypted)
               {
                   
if (!mail.Decryptable)
                   {
                       Console.WriteLine(
"Message cannot be decrypted. You do not have the private key.");
                       
return false;
                   }
                   Smime.Decrypt(mail);
               }

               
// ValidateRequest the signature if the message is signed
               
if (mail.IsSigned)
               {
                   MailSignatureValidationResult result = Smime.Validate(mail);
                   
if (result.Valid)
                   {
                       Console.WriteLine(
"The message is signed and the signature is valid.");
                   }
                   
else
                   {
                       
Console.WriteLine("The message is signed, but the signature is not valid.");                        
                   }
               }

               Console.WriteLine(
"Message contains {0} attachments.", mail.Attachments.Count);

               
// If message has no attachments, just exit
               
if (mail.Attachments.Count == 0)
                   
return false;

               
foreach (Attachment attachment in mail.Attachments)
               {
                   
// Write the file
                   
Console.WriteLine("Saving '{0}' ({1}).", attachment.FileName, attachment.MediaType);
                   attachment.Save(attachment.FileName);
               }

               
return true;
           }
           
catch (Exception exc)
           {
               Console.WriteLine(
"An error occurred: {0}", exc.Message);
           }

           
return false;
       }
   }
}
Visual BasicCopy Code
Imports Microsoft.VisualBasic
Imports System
Imports Atp.Net.Mail

Module MailExtractAttachment
    Sub Main()
        ExtractAttachments("C:\TestMessage.eml")
    End Sub

    ''' <summary>
    ''' Extracs attachments from a mail file.
    ''' </summary>
    ''' <param name="fileName">Mail file name.</param>
    ''' <returns>A boolean value indicating whether the extraction is successful.</returns>
    Private Function ExtractAttachments(ByVal fileName As String) As Boolean
        Try
            ' Load the mail message from disk
            Dim mail As New MailMessage()
            mail.Load(fileName)

            ' DecryptData the message if it is encrypted
            If mail.IsEncrypted Then
                If (Not mail.Decryptable) Then
                    Console.WriteLine("Message cannot be decrypted. You do not have the private key.")
                    Return False
                End If
                Smime.Decrypt(mail)
            End If

            ' ValidateRequest the signature if the message is signed
            If mail.IsSigned Then
                Dim result As MailSignatureValidationResult = Smime.Validate(mail)
                If result.Valid Then
                    Console.WriteLine("The message is signed and the signature is valid.")
                Else
                    Console.WriteLine("The message is signed, but the signature is not valid.")
                End If
            End If

            Console.WriteLine("Message contains {0} attachments.", mail.Attachments.Count)

            ' If message has no attachments, just exit
            If mail.Attachments.Count = 0 Then
                Return False
            End If

            For Each attachment As Attachment In mail.Attachments
                ' Write the file
                Console.WriteLine("Saving '{0}' ({1}).", attachment.FileName, attachment.MediaType)
                attachment.Save(attachment.FileName)
            Next attachment

            Return True
        Catch exc As Exception
            Console.WriteLine("An error occurred: {0}", exc.Message)
        End Try

        Return False
    End Function
End Module

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ATP, Inc. is a Microsoft Partner. Our components are compatible with Windows 7 and optimized for Visual Studio