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

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.
//your code here... //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

Url: http://www.aspose.com/.net/email-component.aspx

Language: C# | User: Sheraz Khan | Created: Sep 30, 2015 | Tags: Add Attachments to an Email, Save Attachments to Email Message, save an attachment from an email, Send email with attachments, .NET Email Component, .NET Outlook API Add Attachments to an Email Save Attachments to Email Message save an attachment from an email Send email with attachments .NET Email Component .NET Outlook API