User Id :    Password :      New Member   Forgot Password  
Send email in with attachment
Description Send email in with attachment in   No. of Views     9167
  Rating     4
Author Najmul Hoda   Posted On     20 Aug 2011
Tags ASP.NET,C#,General    

Sample Code   Download Code


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.

using System.Net;
using System.Net.Mail; 


Then write  a method  "SendEmailWithAttamentWithSyncMode" to send email.


    private void SendEmailWithAttamentWithSyncMode(string[] fileNames)
            MailMessage message = new MailMessage();
            //from address  
            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 Message";
            //attach our file to mail message as attachment  
            //here we can add more than one attachments  
            foreach (string filePaths in fileNames)
                message.Attachments.Add(new Attachment(filePaths));

            // 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)
            throw smtpex;
        catch (Exception ex)
            throw 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.

Thank you

About Author

About Author He is a team lead, having 8+ years of experience. He loves writing technical article for .Net developers. Najmul Hoda
No Photo
Country United States
Company BirlaSoft
Home Page

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Posted By Vikro on 02 Sep 2011 at 11:19 PM
good one and more important thing u looking same to same like my friend his name is Amit .so do u want see him then u join on facebook of email address
Write your comment here.
Verification Code