article

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

Email
Submitted on: 9/30/2015 6:30:01 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 4443
 
     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.

 
				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.


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.