Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Send Email With Attachement

Article:
Viewed:  1185 
Posted On:  02/09/2012 21:51:54 
HOW TO: Send email with attachement in asp.net 

In this article I am going to show how we can send email with attachment in asp.net.

This is my aspx code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Send Email With Attachement</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 100%" align="center">
        <div align="left">
            <div>
                <h4>
                    Send Email With Attachment
                </h4>
            </div>
            <table cellpadding="1" cellspacing="1" width="98%" align="center">
                <tr>
                    <td align="center">
                        <asp:Label ID="LabelError" runat="server" ForeColor="red" EnableViewState="False"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label5" runat="server" Text="From Email :" Width="140px"></asp:Label>
                        <asp:TextBox ID="txtFromEmail" runat="server" Width="300px"></asp:TextBox>
                        <span style="color: #ff0033">*</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblName" runat="server" Text="To Email :" Width="140px"></asp:Label>
                        <asp:TextBox ID="txtToEmail" runat="server" Width="300px"></asp:TextBox>
                        <span style="color: #ff0033">*</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Subject :" Width="140px"></asp:Label>
                        <asp:TextBox ID="txtSubject" runat="server" Width="300px"></asp:TextBox>
                        <span style="color: #ff0033">*</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Message :" Width="140px"></asp:Label>
                        <asp:TextBox ID="txtMessage" runat="server" Width="450px" Height="160px" TextMode="MultiLine"></asp:TextBox>
                        <span style="color: #ff0033">*</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Subject :" Width="140px"></asp:Label>
                        <input id="Attachement" type="file" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server" Width="140px"></asp:Label>
                        <asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" Width="140px" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>

 

This is my aspx.cs code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

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

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtToEmail.Text); 

        mail.From = new MailAddress(txtFromEmail.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtMessage.Text;
        mail.IsBodyHtml = true;

         //Attach file using FileUpload Control 
        if (Attachement.Value != "")
        {
            mail.Attachments.Add(new Attachment(Attachement.PostedFile.InputStream, Attachement.PostedFile.FileName));
        }

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //SMTP server details may be localhost
        smtp.Credentials = new System.Net.NetworkCredential ("myID@gmail.com", "mypwd");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}

 When I run my application.

 Email.png

Figure 1.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us