Send mail with attachment using gmail in asp.net C# sample code

If you had not read our last most popular articles "How to send email using asp.net C#" and "How to send email using gmail in asp.net C#". This type we comes with send email with attachment using gmail in asp.net C#. In this article we will do minor changes from existing example and make new sample code works for send mail with attachment.

Configure to send mail with attachment

you have to create object of Attachment and pass it to MailMessage attachment property. You can use multiple upload options which we will discuss in later articles. You can use jquery to add multiple file upload and can select multiple file at a time.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Send Mail using Asp.net - Dotnetspan.com</title>
</head>
<body>
    
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td colspan="2"><center><span style="font-size:26px;font-weight:bold">Dotnetspan.com</span><br /><span style="font-size:16px;font-weight:bold">Send Mail with attachment using <br />Gmail Credentials in Asp.net</span></center></td>
        </tr>
        <tr>
            <td>From:</td>
        <td>
            <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
            <td>To</td>
           
        <td> <asp:TextBox ID="txtTo" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Subject</td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
            <td>Attachment</td>
            <td>
                <asp:FileUpload ID="fuploadmail" runat="server" />
            </td>

        </tr>
        <tr>
            <td>Message</td>
        <td>
            <asp:TextBox ID="txtBody" TextMode="MultiLine" Rows="10" Columns="20" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
            <td></td>
        <td><asp:Button ID="btnSend" Text="Send" runat="server" OnClick="btnSend_Click" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblmessage" runat="server" Visible="false"></asp:Label>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

Default.aspx.cs
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(txtTo.Text.ToString());
            mailMessage.From = new MailAddress(txtFrom.Text.Trim());
            mailMessage.Body = txtBody.Text.Trim();
            if (fuploadmail.HasFile)
            {
                mailMessage.Attachments.Add(new Attachment(fuploadmail.PostedFile.InputStream,fuploadmail.PostedFile.FileName));
            }
            mailMessage.IsBodyHtml = false;
            System.Net.Mail.SmtpClient smptClient = new System.Net.Mail.SmtpClient();
            smptClient.Host = "smtp.gmail.com";
            smptClient.Port=587;
            smptClient.Credentials = new System.Net.NetworkCredential("gmail username", "gmail password");
            smptClient.EnableSsl = true;
            smptClient.Send(mailMessage);

            lblmessage.Visible = true;
            lblmessage.Text = "Email Sent Successfully";
            lblmessage.ForeColor = System.Drawing.Color.Green;
        }
        catch
        {
            lblmessage.Visible = true;
            lblmessage.Text = "Error Occur while processing your request.";
            lblmessage.ForeColor = System.Drawing.Color.Red;
        }
    }
Download Sample Code