Send Email With Attachment Using Smtp Server In Asp.Net

Hi here i am explaining how to send email with attachment using smtp server in Asp.net by using gmail credentials..

Design the page like below:



<div>
        <table style="border: 1px solid" align="center">
            <tr>
                <td colspan="2" align="center">
                    <b>Send Mail using gmail credentials in asp.net</b>
                </td>
            </tr>
            <tr>
                <td>
                    Gmail Username:
                </td>
                <td>
                    <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Gmail Password:
                </td>
                <td>
                    <asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Subject:
                </td>
                <td>
                    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    To:
                </td>
                <td>
                    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    Body:
                </td>
                <td>
                    <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Attachment:
                </td>
                <td>
                    <asp:FileUpload ID="upload" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnSubmit" Text="Send" runat="server" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
    </div>


then in the button click event write the following code:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            // Sender e-mail address.
            Msg.From = new MailAddress(txtUsername.Text);
            // Recipient e-mail address.
            Msg.To.Add(txtTo.Text);
            Msg.Subject = txtSubject.Text;
            Msg.Body = txtBody.Text;
            if (upload.HasFile)
            {
                Attachment atc = new Attachment(upload.PostedFile.FileName.ToString());
                Msg.Attachments.Add(atc);
            }
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            // your remote SMTP server IP.
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            Msg = null;
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ClientScript", "alert('Mail Sent Successfully!')", true);
        }
        catch (Exception ex)
        {
            string ErrMsg = ex.Message;
        }
    }


Note:
 Please add these name spaces to page:
using System.Net.Mail;

then run the application and enter ur credentials and send mail using gamil...

Enjoy Happy Coding..