Gets the collection of attachments of this message.
Syntax
Return Value
A collection of attachments of this message.
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 Basic | Copy Code |
|---|
Imports Microsoft.VisualBasic
Imports System
Imports Atp.Net.Mail
Module MailExtractAttachment
Sub Main()
ExtractAttachments("C:\TestMessage.eml")
End Sub
Private Function ExtractAttachments(ByVal fileName As String) As Boolean
Try
Dim mail As New MailMessage()
mail.Load(fileName)
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
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 mail.Attachments.Count = 0 Then
Return False
End If
For Each attachment As Attachment In mail.Attachments
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 |
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also