Most of the systems need feature to send email to users. For example when a user registers to a website a user activation mail is sent to the user email id. Or some sites send email to the make the users know about the new product that they have launched. Sometimes these mail contains some attachment. Here we will discuss how to send email with attachment.
Sending email very easy with ASP.Net. For this we need to add System.NET.Mail namespace that conatins all the required classes to send email.
Let us look on the implementation of this features with ASP.NET 3.5.
First we need to add two namespaces.
Then write a method "SendEmailWithAttamentWithSyncMode" to send email.
private void SendEmailWithAttamentWithSyncMode(string fileNames)
MailMessage message = new MailMessage();
message.From = new MailAddress("fromEmailAddress", "FromDisplayName");
// to address
message.To.Add(new MailAddress("ToEmailAddress", "ToDisplayName"));
// set content email
message.Body = "This is auto generated email dont reply and you have two attachements also";
//set this email content display user inbox like Html format
message.IsBodyHtml = true; // this is the heading of the email
message.Subject = "The dotnetlogiX.com Message";
//attach our file to mail message as attachment
//here we can add more than one attachments
foreach (string filePaths in fileNames)
// 25 is default for SMTP server
SmtpClient emailClient = new SmtpClient("ServerIP", 25);
emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
// set users to default use the default credential
emailClient.UseDefaultCredentials = true;
catch (SmtpException smtpex)
catch (Exception ex)
In above method we have created a mailmessage object and then set from email, to email address. then the body for the message and contents too. Then we have set this email to display in userinbox like HTML format.
Then we add our attachment to the email message and finally passed to the Smtpclient object with send method.
Now using this method you can send your mail.