Header Ads Widget

header ads

How to send Email using asp.net c#





1. Open visual studio and create new project.

2.  Add new web form and design like in this example.

3. Design code:

---------------------------------------------------------------------------------------

<head>

<style type="text/css">

        .auto-style1 {

            width: 41%;

        }

        .auto-style2 {

            height: 30px;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

        <table class="auto-style1" border="1" style="background-color: #6699FF">

            <tr>

                <td>

                    <asp:Label ID="Label1" runat="server" Text="To"></asp:Label>

                </td>

                <td>

                    <asp:TextBox ID="TextBox1" runat="server" TextMode="Email" Width="177px"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:Label ID="Label2" runat="server" Text="Subject"></asp:Label>

                </td>

                <td>

                    <asp:TextBox ID="TextBox2" runat="server" Width="177px"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:Label ID="Label3" runat="server" Text="Message"></asp:Label>

                </td>

                <td>

                    <asp:TextBox ID="TextBox3" runat="server" Height="76px" TextMode="MultiLine" Width="177px"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:Label ID="Label4" runat="server" Text="Attachment"></asp:Label>

                </td>

                <td>

                    <asp:FileUpload ID="FileUpload1" runat="server" Width="187px" AllowMultiple="true" />

                </td>

            </tr>

            <tr>

                <td class="auto-style2"></td>

                <td class="auto-style2">

                    <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />

                </td>

            </tr>

        </table>

        <div>

        </div>

    </form>

</body>


-------------------------------------------------------------------------------------------------

send email using c#


4. cs code:
----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

namespace Email
{
    public partial class SendEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage ms = new MailMessage();
                ms.From = new MailAddress("youremail@gmail.com", "Learn with Developer");
                ms.To.Add(TextBox1.Text);
                ms.Subject = TextBox2.Text;
                ms.Body = TextBox3.Text;
                if (FileUpload1.HasFile)
                {
                    HttpFileCollection fc = Request.Files;
                    for(int i = 0; i < fc.Count; i++)
                    {
                        HttpPostedFile pf = fc[i];
                        Attachment attach = new Attachment(pf.InputStream, pf.FileName);
                        ms.Attachments.Add(attach);
                    }
                }
                SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
                sc.Port = 587;
                sc.Credentials = new NetworkCredential("youremail@gmail.com", "yourPassword");
                sc.EnableSsl = true;
                sc.Send(ms);
                Response.Write("Message sent");
            }
            catch(Exception ex)
            {
                Response.Write(ex);
            }
        }
    }
}

----------------------------------------------------------------------------------------------

5. connect to internet and Run project

Output




*If you still have any issue, please follow our YouTube video given above. In YouTube, we have cleared all types off error that can be occurred in this tutorial. So please watch my YouTube video and subscribe my channel.


Thank you all.

Post a Comment

0 Comments