Home > Code > C# > How to Add & Save Attachments to an Email Message inside .NET applications

How to Add & Save Attachments to an Email Message inside .NET applications

by sher azam   on Sep 30, 2015   Category: C#   |  Views: 667    |  Points: 25   |  Gold 


This technical tip explains how .NET developers can add and save files attached to an email message inside their .NET applications. The programming samples are provided both in VB.NET and C# to show how to add and save files attached to an email message. To save an attachment from an email, first create an instance of the MailMessage class and then specify sender and receiver email addresses in the MailMessage instance. After that, create an instance of the Attachment class. To add an attachment, load attachment into the Attachment instance and Save attachments in the Attachment instance specifying the path. At the end create an instance of the SmtpClient class and send the email using the Send method.

Adding and Saving an Attachment

//[C# Code Sample]

using System;
using Aspose.Email.Mail;
using Aspose.Email;

namespace Saveattachment
{
class Program
{
static void Main(string[] args)
{

//Create an instance of the MailMessage class
MailMessage message = new MailMessage();

//From field
message.From = "sender@sender.com";

//To field
message.To.Add("receiver@receiver.com");

//Adding 1st attachment
//Create an instance of Attachment class
Attachment attachment;

//Load or add an attachment
attachment = new Attachment(@"d:\1.txt");

//Save the attachment to a different path
attachment.Save(@"e:\2.txt");

//Now add the attachment to the message
message.Attachments.Add(attachment);

//Create an instance of the SmtpClient Class
SmtpClient client = new SmtpClient();

//Specify your mailing host server
client.Host = "smtp.server.com";

//Specify your mail user name
client.Username = "Username";

//Specify your mail password
client.Password = "Password";

//Specify your Port #
client.Port = 25;

try
{
//Client.Send will send this message
client.Send(message);
//Display ‘Message Sent’, only if message sent successfully
Console.WriteLine("Message sent");
}

catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.ToString());
}

Console.WriteLine("Press enter to quit");
Console.Read();

}
}
}

//[VB.NET Code Sample]

Imports System
Imports Aspose.Email.Mail
Imports Aspose.Email

Namespace Saveattachment
Class Program
Shared Sub Main(ByVal args() As String)

'Create an Instance of the MailMessage class
Dim message As MailMessage = New MailMessage()

'From field
message.From = "sender@sender.com"

'To field
message.To.Add("receiver@receiver.com")

'Adding 1st attachment
'Create an instance of Attachment class
Dim attachment As Attachment

'Load or add an attachment
attachment = New Attachment("d:\1.txt")

'Save the attachment to a different path
attachment.Save("e:\2.txt")

'Now add the attachment to the message
message.Attachments.Add(attachment);

'Create an instance of the SmtpClient Class
Dim client As SmtpClient = New SmtpClient()

'Specify your mailing host server
client.Host = "smtp.server.com"

'Specify your mail user name
client.Username = "Username"

'Specify your mail password
client.Password = "Password"

'Specify your Port #
client.Port = 25

Try
'Client.Send will send this message
client.Send(message)
'Display ‘Message Sent’, only if message sent successfully
Console.WriteLine("Message sent")
Catch ex As Exception
System.Diagnostics.Trace.WriteLine(ex.ToString())
End Try

Console.WriteLine("Press enter to quit")
Console.Read()

End Sub
End Class
End Namespace



Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts