How To Send Mail Along With Attachment In ASP.Net Using C#

Hello guys, in my previous article i.e How To Send Mail In ASP.NET Using C#.  I discussed about how one can send mail to other fellow in a simple manner by using simple ASP.NET application, now in the same code I’ll tell how one can send attachment along with the mail, attachment could be image file, audio/video file anything (yes size depends as some limits are set, and same for the type of file).

Now you can refer the previous post for reference in order to send mail, as in this I would be discussing about, how one can attach the file along with the mail.

Below is the code, and explanation and download link will be at the end…..

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <h1>How To Send Mail In ASP.NET</h1>
        <table>
            <tr>
                <th>To:</th>
                <td><asp:TextBox ID="to1" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <th>Subject:</th>
                <td><asp:TextBox ID="sub1" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <th>Mesage:</th>
                <td><asp:TextBox ID="msg" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <th>Address:</th>
                <td><asp:TextBox ID="add" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <th>Mobile:</th>
                <td><asp:TextBox ID="mb" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <th><asp:Button ID="btn" runat="server" Text="Send Mail" OnClick="btn_Click" /></th>
            </tr>
            </table>
    </div>
    </form>
</body>
</html>

 


 

Default.aspx.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Click(object sender, EventArgs e)
    {
        string address = add.Text;
        string mobile = mb.Text;
        string message = msg.Text;
        string from = "your_gmail_account_email_id@gmail.com";
        string to = to1.Text;
        string sub = sub1.Text;
        string body = message + Environment.NewLine + address + Environment.NewLine + mobile;
        string link = Server.MapPath("~/com.jpg");
        Attachment attach = new Attachment(link);
        MailMessage m = new MailMessage(from, to, sub, body);
        m.Attachments.Add(attach);
        SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
        ss.Credentials = new System.Net.NetworkCredential("your_gmail_account_email_id@gmail.com", "your_gmail_account_password");
        ss.EnableSsl = true;
        ss.Send(m);
        btn.Text = "Done";
    }
}

  Code Explanation:

The code is all same as in the previous post How To Send Mail In ASP.NET Using C#. The only difference is the here in this code you have to use an additional class naming ATTACHMENT.   This class is used to attach file in the mail. See how it works, if observe,

        string link = Server.MapPath("~/com.jpg");
        Attachment attach = new Attachment(link);
        MailMessage m = new MailMessage(from, to, sub, body);
        m.Attachments.Add(attach);

 


I have taken a variable LINK of string data type, in which I have stored the path of my file which I want to send along with the mail, here I have directly mentioned the path, you can even create an application in which one can upload the file.

Well, lets come back on the code,  Server.MapPath is a method which is used to return the physical path of the file, here Tilda sign (~) means root of the application, as my image was stored in the main directory of my application, so I have used

~/com.jpg, this tells the application to search for image name com.jpg which is located in the main directory.

You can see the image below…..

send mail along with attachment

 

 

 

 

 

 

 

 

 

 

 

Click On Image To Enlarge

As you can see the image the image file com.jpg is located in the main directory.

Now Attachment is the class, for which I created an object attach, and this accepts one parameter of string type, in which we’ll pass the link of the file.

 

And the last line m.Attachment.Add(attach), this line of code attach our file with the mail and send it to the respective destination.

You can see in the image below I send mail on my account, you can check here….

send mail along with attachment
 

 

 

 

 

 

 

 

 

 

 

Click On The Image To Enlarge

 

Hope this would help you send attachments along with the mail, you can even create an application in which you can let user upload the file which is to be send along with the mail, creating such app is not at all difficult, just try….. All The Best 🙂

 

Download Demo Work


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar