C# Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

C# Examples

Bounced Mail
Bz2
Character Encoding
CSV
DKIM / DomainKey
Digital Certificates
Digital Signatures
Email
Email Object
FTP
HTML Conversion
HTTP
IMAP
Encryption
MHT / HTML Email
MIME
POP3
RSA
S/MIME
SMTP
Socket
Spider
SSH
SSH Tunnel
SSH Key
SFTP
Tar Archive
Upload
XML
XMP
Zip Compression


More Examples...
Amazon S3
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

Saving Email Attachments

Download: Chilkat .NET Assemblies

This C# example shows how to save email attachments to a directory. It also discusses issues relating to overwriting files and handling characters not allowed in Windows filenames.

// Demonstrates how to save email attachments to a directory
    private void save_email_attachments(string dirPath, Chilkat.Email email)
    {
    // You can easily save all the attachments to the specified directory
    // by calling SaveAllAttachments.
    // The SaveAllAttachments method will automatically create the directory
    // if it does not already exist.
    bool success = email.SaveAllAttachments(dirPath);
    
    // the return value is true for success, false for failure.  A failure typically
    // would occur if the process did not have permission to create files
    // in the directory.
    if (!success) {
        // The last-error information should contain enough information for you
        // to resolve the problem.
        MessageBox.Show(email.LastErrorText);
        return;
    }
    
    // The OverwriteExisting property controls whether already-existing files
    // are automatically overwritten.  By default, it is set to true so that existing
    // files will be overwritten.
    
    // Setting OverwriteExisting = false will cause the attachment-saving methods to generate
    // unique filenames if a file with the same name already exists.  The actual filename(s)
    // saved will be present by calling GetAttachmentFilename for each attachment *after*
    // saving.
    // For example...
    email.OverwriteExisting = false;
    success = email.SaveAllAttachments(dirPath);
    
    int n = email.NumAttachments;
    int i;
    for (i=0; i<n; i++)
        {
        // If the attachment filename was changed to prevent overwriting,
        // GetAttachmentFilename will return the new filename.
        listBox1.Items.Add(email.GetAttachmentFilename(i));
        }
    
    // You may also save individual attachments:
    for (i=0; i<n; i++)
        {
        listBox1.Items.Add("Original Filename: " + email.GetAttachmentFilename(i));
        success = email.SaveAttachedFile(i, dirPath);
        // If OverwriteExisting = true, the saved filename will always equal the original filename,
        // unless there are characters present in the filename that are not allowed by Windows,
        // such as * ? < > | etc.  In those cases the illegal characters are either removed or replaced
        // with underscore characters to allow the file to be saved.
        listBox1.Items.Add("Saved Filename: " + email.GetAttachmentFilename(i));
        }
    
    }

Important: The download for this example does not contain the ChilkatDotNet.dll which
must be downloaded and installed separately at http://www.chilkatsoft.com/downloads.asp.
Once installed, add a reference to the DLL in the project by following the instructions at
http://www.example-code.com/vbdotnet/step2.asp

 

© 2000-2012 Chilkat Software, Inc. All Rights Reserved.

Email Component · XML Parser