• Loading...
UltimateMail
Attachment Class
Members  Example  See Also  Send comments on this topic to ATP, Inc.
Atp.Net.Mail Namespace : Attachment Class




Represents an e-mail attachment.

Object Model




Syntax

Visual Basic (Declaration) 
<SerializableAttribute()>
Public Class Attachment 
   Inherits AttachmentBase
Visual Basic (Usage)Copy Code
Dim instance As Attachment
C# 
[SerializableAttribute()]
public class Attachment : AttachmentBase 
Managed Extensions for C++ 
[SerializableAttribute()]
public __gc class Attachment : public AttachmentBase 
C++/CLI 
[SerializableAttribute()]
public ref class Attachment : public AttachmentBase 

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

Inheritance Hierarchy

System.Object
   Atp.Net.Mail.AttachmentBase
      Atp.Net.Mail.Attachment

Requirements

Namespace: Atp.Net.Mail

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

Assembly: UltimateMail (in UltimateMail.dll)

See Also

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