Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Saving Email AttachmentsDownload: 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 |
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.