This documentation is archived and is not being maintained.

MailMessage.Attachments Property

Gets the attachment collection used to store data attached to this e-mail message.

Namespace:  System.Net.Mail
Assembly:  System (in System.dll)

Public ReadOnly Property Attachments As AttachmentCollection

Use the collection returned by the Attachments property to add an attachment, such as a file or the contents of a Stream, to this MailMessage.

Create an Attachment that contains or references the data to be attached, and then add the Attachment to the collection returned by Attachments.

The following code example demonstrates creating and sending an e-mail message with an attachment.

Public Shared Sub CreateMessageWithAttachment(ByVal server As String)
    ' Specify the file to be attached and sent.
    ' This example assumes that a file named Data.xls exists in the
    ' current working directory.
    Dim file As String = "data.xls"
    ' Create a message and set up the recipients.
    Dim message As New MailMessage("", "", "Quarterly data report.", "See the attached spreadsheet.")

    ' Create  the file attachment for this e-mail message.
    Dim data As New Attachment(file, MediaTypeNames.Application.Octet)
    ' Add time stamp information for the file.
    Dim disposition As ContentDisposition = data.ContentDisposition
    disposition.CreationDate = IO.File.GetCreationTime(file)
    disposition.ModificationDate = IO.File.GetLastWriteTime(file)
    disposition.ReadDate = IO.File.GetLastAccessTime(file)
    ' Add the file attachment to this e-mail message.

    'Send the message.
    Dim client As New SmtpClient(server)
    ' Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials

    Catch ex As Exception
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString())
    End Try
    ' Display the values in the ContentDisposition for the attachment.
    Dim cd As ContentDisposition = data.ContentDisposition
    Console.WriteLine("Content disposition")
    Console.WriteLine("File {0}", cd.FileName)
    Console.WriteLine("Size {0}", cd.Size)
    Console.WriteLine("Creation {0}", cd.CreationDate)
    Console.WriteLine("Modification {0}", cd.ModificationDate)
    Console.WriteLine("Read {0}", cd.ReadDate)
    Console.WriteLine("Inline {0}", cd.Inline)
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count)
    For Each d In cd.Parameters
        Console.WriteLine("{0} = {1}", d.Key, d.Value)
    Next d
End Sub

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.