Monday, June 17, 2013

How to send Email with Attachment in

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

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</b></tr>
        <asp:TextBox ID="txtemail" runat="server"></asp:TextBox></td></tr>
            <asp:TextBox ID="txtsubject" runat="server"></asp:TextBox></td></tr>
                <asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine"></asp:TextBox></td></tr>
                    <asp:FileUpload ID="fileupload" runat="server" /></td></tr>
                        <asp:Button ID="Button1" runat="server" Text="Send Email"
                            onclick="Button1_Click" /></td></tr>

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

      protected void Button1_Click(object sender, EventArgs e)
                MailMessage message = new MailMessage();
                message.From = new MailAddress("", "Saklani");
                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.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("Enter Email Id here", "Enter Password here");
                smtp.EnableSsl = true;
            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
            Dim message As New MailMessage()
            message.From = New MailAddress("", "Saklani")
            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.Port = 587
            smtp.Credentials = New System.Net.NetworkCredential("Enter Email Id here", "Enter Password here")
            smtp.EnableSsl = True
        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 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.

No comments:

Post a Comment