This article demonstrates how to extract the attachment from an email message and delete the attachment via Spire.Email in C# and VB.NET. Spire.Email supports to work with MSG, EML, EMLX, MHTML, PST, OST and TNEF email file formats. On this article we use .msg message format for example.
Firstly, please view the sample email message with attachments:

How to extract the attachment from an Email message:
[C#]
using Spire.Email;
using System.IO;
namespace ExtractAttachment
{
class Program
{
static void Main(string[] args)
{
//Load the mail message from file
MailMessage mail = MailMessage.Load("Test.msg");
//Create a folder named Attachments
if (!Directory.Exists("Attachments"))
{
Directory.CreateDirectory("Attachments");
}
foreach (Attachment attach in mail.Attachments)
{
//To get and save the attachment
string filePath = string.Format("Attachments\\{0}", attach.ContentType.Name);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
FileStream fs = File.Create(filePath);
attach.Data.CopyTo(fs);
}
}
}
}
[VB.NET]
Imports Spire.Email
Imports System.IO
Namespace ExtractAttachment
Class Program
Private Shared Sub Main(args As String())
'Load the mail message from file
Dim mail As MailMessage = MailMessage.Load("Test.msg")
'Create a folder named Attachments
If Not Directory.Exists("Attachments") Then
Directory.CreateDirectory("Attachments")
End If
For Each attach As Attachment In mail.Attachments
'To get and save the attachment
Dim filePath As String = String.Format("Attachments\{0}", attach.ContentType.Name)
If File.Exists(filePath) Then
File.Delete(filePath)
End If
Dim fs As FileStream = File.Create(filePath)
attach.Data.CopyTo(fs)
Next
End Sub
End Class
End Namespace

How to delete the attachment from an Email message:
[C#]
using Spire.Email;
namespace DeleteAttachment
{
class Program
{
static void Main(string[] args)
{
MailMessage mail = MailMessage.Load("Test.msg");
// Delete the attachment by index
mail.Attachments.RemoveAt(0);
// Delete the attachment by attachment name
for (int i = 0; i < mail.Attachments.Count; i++)
{
Attachment attach = mail.Attachments[i];
if (attach.ContentType.Name == "logo.png")
{
mail.Attachments.Remove(attach);
}
}
mail.Save("HasDeletedAttachment.msg", MailMessageFormat.Msg);
}
}
}
[VB.NET]
Imports Spire.Email
Namespace DeleteAttachment
Class Program
Private Shared Sub Main(args As String())
Dim mail As MailMessage = MailMessage.Load("Test.msg")
' Delete the attachment by index
mail.Attachments.RemoveAt(0)
' Delete the attachment by attachment name
For i As Integer = 0 To mail.Attachments.Count - 1
Dim attach As Attachment = mail.Attachments(i)
If attach.ContentType.Name = "logo.png" Then
mail.Attachments.Remove(attach)
End If
Next
mail.Save("HasDeletedAttachment.msg", MailMessageFormat.Msg)
End Sub
End Class
End Namespace

