PowerTCP Mail for .NET
Attachment Class
Members  Example 




A MIME part that has a "Content-Disposition: attachment" header label. Supports non-MIME uu-encoded attachments. Content represents the file on disk.
Object Model
Attachment ClassContentDisposition ClassContentType ClassHeaderDictionary ClassHeaderField Class
Syntax
Public Class Attachment 
   Inherits Resource
Dim instance As Attachment
public class Attachment : Resource 
public __gc class Attachment : public Resource 
public ref class Attachment : public Resource 
Remarks

During decoding, an Attachment is created when ContentDisposition.DispositionType == "attachment" or ContentType.MediaType starts with "application". File content is written to a file using a name returned by Path.GetRandomFileName() and stored in Directory; Attachment.FileName returns its actual filename. Use Attachment.Content.MoveTo(string) to move the decoded file to a permanent location.

Decoded attachment files are not automatically deleted when the object goes out of scope; to easily delete the decoded attachments from disk, delete Attachment.Directory or use Dispose(). Attachment.Dispose(), MailMessage.Dispose() (and the Dispose method for other Part types that can contain subparts), Imap.Dispose(), and Pop.Dispose() will each delete the attachment files associated with the object; Imap.Dispose() and Pop.Dispose() will delete the attachments for all messages under Imap.Mailboxes or Pop.Messages, and will also delete Attachment.Directory. Attachments that have been moved or renamed after decoding will not be deleted by Dispose(). Note: Imap.Dispose() and Pop.Dispose() will abort any active connection; when connected to a server, Close() should be called before Dispose() to gracefully shutdown the application.

Attachments may be decoded to memory instead of to disk; before decoding, set DecodeToMemory to true. When encoding, use a constructor that includes a byte[] parameter.

Attachment.ContentDisposition.Inline defaults to false. Set to true to indicate that the attachment should be shown "inline".

Example
Demonstrates saving attachments to disk. This example demonstrates creating a text MailMessage and adding an attachment.
/// <summary>
/// Saves the email's attachments to the specified directory.
/// </summary>
/// <param name="message">MailMessage containing attachments to save.</param>
/// <param name="saveDirectory">Directory to save attachments to.</param>
private void SaveAttachments(MailMessage message, string saveDirectory)
{
    foreach (Attachment attached in message.Attachments)
    {
        string filePath = Path.Combine(saveDirectory, attached.FileName);
        if (!Attachment.DecodeToMemory)
        {
            //If the component is configured to decode attachments to disk (the default), 
            //use the Content property that returns a FileInfo representing the file on disk,
            //and copy the file to the desired location.
            attached.Content.CopyTo(filePath);
        }
        else
        {
            //If the component is configured to decode attachments to memory, get the content 
            //MemoryStream with GetContentStream(), and write its content to a file.
            using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
                attached.GetContentStream().CopyTo(fs);
        }
    }
}
''' <summary>
''' Saves the email's attachments to the specified directory.
''' </summary>
''' <param name="message">MailMessage containing attachments to save.</param>
''' <param name="saveDirectory">Directory to save attachments to.</param>
Private Sub SaveAttachments(ByVal message As MailMessage, ByVal saveDirectory As String)
    For Each attached As Attachment In message.Attachments
        Dim filePath As String = Path.Combine(saveDirectory, attached.FileName)
        If Not Attachment.DecodeToMemory Then
            'If the component is configured to decode attachments to disk (the default), 
            'use the Content property that returns a FileInfo representing the file on disk,
            'and copy the file to the desired location.
            attached.Content.CopyTo(filePath)
        Else
            'If the component is configured to decode attachments to memory, get the content 
            'MemoryStream with GetContentStream(), and write its content to a file.
            Using fs As New FileStream(filePath, FileMode.CreateNew, FileAccess.Write)
                attached.GetContentStream().CopyTo(fs)
            End Using
        End If
    Next attached
End Sub
private MailMessage CreateMailMessageWithAttachment()
{
    MailMessage message = new MailMessage();
    message.To = "to@dart.com";
    message.From = "from@dart.com";
    message.Subject = "This is a simple message";
    message.Text = "Please see the attached file.";
    message.Attachments.Add(new Attachment("C:\\File\\myAttachment.jpg"));
    return message;
}
Private Function CreateMailMessageWithAttachment() As MailMessage
    Dim message As New MailMessage()
    message.To = "to@dart.com"
    message.From = "from@dart.com"
    message.Subject = "This is a simple message"
    message.Text = "Please see the attached file."
    message.Attachments.Add(New Attachment("C:\File\myAttachment.jpg"))
    Return message
End Function
Inheritance Hierarchy

System.Object
   Dart.Mail.Part
      Dart.Mail.Resource
         Dart.Mail.Attachment

See Also

Reference

Attachment Members
Dart.Mail Namespace


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic