Send Email using SMTP with attachments in ASP.NET

Sample Code

In this article I will explain how to send email with attachment in ASP.Net. The file to be send as attachment along with mail will be uploaded using FileUpload control and will be dynamically added as attachment in the MailMessage class object without saving in any folder on disk.
For this example, for sending emails Gmail SMTP Mail Server will be used. To send email with Gmail SMTP Server, you will need to use an email address and password of a valid Gmail account and along with that you will need the Gmail SMTP Mail Server settings.
HTML Markup
The HTML Markup has a form with some fields such as Recipient Email address, Subject, Body, Attachment, Gmail account email address, Gmail account password and a Button to send the email.
<table border=”0″ cellpadding=”0″ cellspacing=”0″>
    <tr>
        <td style=”width: 80px”>
            To:
        </td>
        <td>
            <asp:TextBox ID=”txtTo” runat=”server”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Subject:
        </td>
        <td>
            <asp:TextBox ID=”txtSubject” runat=”server”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td valign = “top”>
            Body:
        </td>
        <td>
            <asp:TextBox ID=”txtBody” runat=”server” TextMode = “MultiLine” Height = “150” Width = “200”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            File Attachment:
        </td>
        <td>
            <asp:FileUpload ID=”fuAttachment” runat=”server” />
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Email:
        </td>
        <td>
            <asp:TextBox ID=”txtEmail” runat=”server”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Password:
        </td>
        <td>
            <asp:TextBox ID=”txtPassword” runat=”server” TextMode = “Password”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button Text=”Send” OnClick=”SendEmail” runat=”server” />
        </td>
    </tr>
</table
MailMessage Class Properties
Following are the required properties of the MailMessage class.
From – Sender’s email address
To – Recipient(s) Email Address
CC – Carbon Copies (if any)
BCC – Blind Carbon Copies (if any)
Subject – Subject of the Email 
Body – Body of the Email
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any)
ReplyTo – ReplyTo Email address.
SMTP Class Properties
Following are the properties of the SMTP class.
Host – SMTP Server URL (Gmail: smtp.gmail.com)
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True)
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password)
Port – Port Number of the SMTP server (Gmail: 587)
Namespaces
You will need to import the following namespaces
C#
using System.IO;
using System.Net;
using System.Net.Mail;
VB.Net
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Sending email with attachment using Gmail SMTP Account
Below is the code to send email using Gmail account and Gmail SMTP server in ASP.Net, the Recipient email address (to), the Sender email address (from), Subject and Body is fetched from their respective fields.
Then all these values are set into an object of the MailMessage class.
For attaching a File as attachment to the email, one has to select the File to be send as attachment using FileUpload control.
If the FileUpload control has attachment then the attachment is added to the Attachments List of the MailMessage Object.
You will notice that the File is directly added as attachment without saving it on disk, this is possible since the file data is extracted from the FileUpload PostedFile InputStream property which belongs to the type System.IO.Stream. The second parameter supplied is the name of the File that has to be send as attachment which is extracted from the FileUpload control PostedFile FileName property.
After that an object of the SmtpClient class is created, where we need to set the settings of the Mail Server here Gmail is the Mail Server so we will set the Mail Settings of the Gmail SMTP Server.
Note: It is necessary to use the sender’s email address credentials while defining the Gmail SMTP Server Credentials as Gmail the sender’s email address must be equal to Gmail Username specified in credentials.

C#
protected void SendEmail(object sender, EventArgs e)
{
    using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
    {
        mm.Subject = txtSubject.Text;
        mm.Body = txtBody.Text;
        if (fuAttachment.HasFile)
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
            mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }
        mm.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = “smtp.gmail.com”;
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        ClientScript.RegisterStartupScript(GetType(), “alert”, “alert(‘Email sent.’);”, true);
    }
}

Send Email using SMTP with attachments in ASP.NET

Sample Code

In this article I will explain how to send email with attachment in ASP.Net. The file to be send as attachment along with mail will be uploaded using FileUpload control and will be dynamically added as attachment in the MailMessage class object without saving in any folder on disk.
For this example, for sending emails Gmail SMTP Mail Server will be used. To send email with Gmail SMTP Server, you will need to use an email address and password of a valid Gmail account and along with that you will need the Gmail SMTP Mail Server settings.
HTML Markup
The HTML Markup has a form with some fields such as Recipient Email address, Subject, Body, Attachment, Gmail account email address, Gmail account password and a Button to send the email.
<table border=”0″ cellpadding=”0″ cellspacing=”0″>
    <tr>
        <td style=”width: 80px”>
            To:
        </td>
        <td>
            <asp:TextBox ID=”txtTo” runat=”server”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Subject:
        </td>
        <td>
            <asp:TextBox ID=”txtSubject” runat=”server”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td valign = “top”>
            Body:
        </td>
        <td>
            <asp:TextBox ID=”txtBody” runat=”server” TextMode = “MultiLine” Height = “150” Width = “200”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            File Attachment:
        </td>
        <td>
            <asp:FileUpload ID=”fuAttachment” runat=”server” />
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Email:
        </td>
        <td>
            <asp:TextBox ID=”txtEmail” runat=”server”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Password:
        </td>
        <td>
            <asp:TextBox ID=”txtPassword” runat=”server” TextMode = “Password”></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button Text=”Send” OnClick=”SendEmail” runat=”server” />
        </td>
    </tr>
</table
MailMessage Class Properties
Following are the required properties of the MailMessage class.
From – Sender’s email address
To – Recipient(s) Email Address
CC – Carbon Copies (if any)
BCC – Blind Carbon Copies (if any)
Subject – Subject of the Email 
Body – Body of the Email
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any)
ReplyTo – ReplyTo Email address.
SMTP Class Properties
Following are the properties of the SMTP class.
Host – SMTP Server URL (Gmail: smtp.gmail.com)
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True)
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password)
Port – Port Number of the SMTP server (Gmail: 587)
Namespaces
You will need to import the following namespaces
C#
using System.IO;
using System.Net;
using System.Net.Mail;
VB.Net
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Sending email with attachment using Gmail SMTP Account
Below is the code to send email using Gmail account and Gmail SMTP server in ASP.Net, the Recipient email address (to), the Sender email address (from), Subject and Body is fetched from their respective fields.
Then all these values are set into an object of the MailMessage class.
For attaching a File as attachment to the email, one has to select the File to be send as attachment using FileUpload control.
If the FileUpload control has attachment then the attachment is added to the Attachments List of the MailMessage Object.
You will notice that the File is directly added as attachment without saving it on disk, this is possible since the file data is extracted from the FileUpload PostedFile InputStream property which belongs to the type System.IO.Stream. The second parameter supplied is the name of the File that has to be send as attachment which is extracted from the FileUpload control PostedFile FileName property.
After that an object of the SmtpClient class is created, where we need to set the settings of the Mail Server here Gmail is the Mail Server so we will set the Mail Settings of the Gmail SMTP Server.
Note: It is necessary to use the sender’s email address credentials while defining the Gmail SMTP Server Credentials as Gmail the sender’s email address must be equal to Gmail Username specified in credentials.

C#
protected void SendEmail(object sender, EventArgs e)
{
    using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
    {
        mm.Subject = txtSubject.Text;
        mm.Body = txtBody.Text;
        if (fuAttachment.HasFile)
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
            mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }
        mm.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = “smtp.gmail.com”;
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        ClientScript.RegisterStartupScript(GetType(), “alert”, “alert(‘Email sent.’);”, true);
    }
}

Source: