Categories
Asp, Asp.net

Send Email with Attachment in Asp.Net

send email with attachment in asp.net

In this tutorial i will let you know how to Send Email with Attachment in Asp.Net.I am saving this file into memory stream rather saving it on server.

Here i am using Gmail smtp to send mail from Asp.net code.For this first we have to add the following namespaces.

Design your page according to your specifications. Design view of page crated by me.

Namespaces:-

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

Design:-

<%@ 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></title>
    <style type="text/css">
        .style1
        {
            width: 600px;
            float: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center" class="style1">
            <tr>
                <td>
                    To:-</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Subject:-</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Cc:-</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Body:-</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server" Height="115px" TextMode="MultiLine" 
                        Width="265px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Attachment:-</td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="Button1" runat="server" Text="Send" onclick="Button1_Click" />
                    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                </td>
              </tr>
        </table>
    </div>
    </form>
</body>
</html>

Code:-

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net.Mail;
using System.Net.Security;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("mailsend@gmail.com");
        mail.To.Add(TextBox1.Text);
        mail.CC.Add(TextBox3.Text);
        mail.Subject = TextBox2.Text;
        mail.IsBodyHtml = true;
        mail.Body = TextBox4.Text;
        if (FileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.Credentials = new System.Net.NetworkCredential("mailsend@gmail.com", "demo@123");
        smtp.EnableSsl = true;
        smtp.Send(mail);
        Label1.Text = "Email sent to " + TextBox1.Text + " Successfully !";
    }
}