How to send Email with Attachment in Asp.net - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

How to send Email with Attachment in Asp.net

Introduction: In this article I will explain how we can send the Email with attachment in Asp.net.
send Email with Attachment

Description:
In the last article I explained How to send Email in Asp.netusing Web.config file. Add a new webform to project. After that add the below given code to .aspx page:
<table border="1px solid">
    <tr><b>Send Email with Attachment in Asp.net</b></tr>
    <tr><td>To:</td><td>
        <asp:TextBox ID="txtemail" runat="server"></asp:TextBox></td></tr>
        <tr><td>Subject:</td><td>
            <asp:TextBox ID="txtsubject" runat="server"></asp:TextBox></td></tr>
            <tr><td>Message:</td><td>
                <asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine"></asp:TextBox></td></tr>
                <tr><td>Attachment:</td><td>
                    <asp:FileUpload ID="fileupload" runat="server" /></td></tr>
                    <tr><td>&nbsp;</td><td>
                        <asp:Button ID="Button1" runat="server" Text="Send Email"
                            onclick="Button1_Click" /></td></tr>
        </table>


Now go to .aspx.cs page and write the below given code:
using System.Net.Mail;

      protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.From = new MailAddress("saklanivijay87@gmail.com", "Saklani");
                message.To.Add(txtemail.Text);
                message.Subject = txtsubject.Text;
                message.Body = txtmessage.Text;
                if (fileupload.HasFile)
                {
                    message.Attachments.Add(new Attachment(fileupload.PostedFile.InputStream, fileupload.FileName));
                }
                message.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("Enter Email Id here", "Enter Password here");
                smtp.EnableSsl = true;
                smtp.Send(message);
                Clear();
            }
            catch (Exception ex)
            {
            }
        }
        public void Clear()
        {
            txtemail.Text = "";
            txtmessage.Text = "";
            txtsubject.Text = "";
        }

In VB (.aspx.vb)
Imports System.Net.Mail

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim message As New MailMessage()
            message.From = New MailAddress("saklanivijay87@gmail.com", "Saklani")
            message.[To].Add(txtemail.Text)
            message.Subject = txtsubject.Text
            message.Body = txtmessage.Text
            If fileupload.HasFile Then
                message.Attachments.Add(New Attachment(fileupload.PostedFile.InputStream, fileupload.FileName))
            End If
            message.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.Port = 587
            smtp.Credentials = New System.Net.NetworkCredential("Enter Email Id here", "Enter Password here")
            smtp.EnableSsl = True
            smtp.Send(message)
            Clear()
        Catch ex As Exception
        End Try
    End Sub
    Public Sub Clear()
        txtemail.Text = ""
        txtmessage.Text = ""
        txtsubject.Text = ""
    End Sub


Now run the project and check result.

Related Articles on Send Email:
How to send Email in Asp.net using web.config

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon