Career form with attachment via email in asp.net c#

In this article, we have create a form with file uploaded there we can fill the form and  attached a resume and submit the button after that  the details is sent on admin’s email .Many time we want create a career form with attached resume so we can get help from the below code.



Design:-



Source Code:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <br />
    <fieldset style="width: 400px;">
        <legend>Career Form</legend>
        <table>
            <tr>
                <td>
                    Name
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Conatct Number
                </td>
                <td>
                    <asp:TextBox ID="txtcontact" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Email
                </td>
                <td>
                    <asp:TextBox ID="txtemail" 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>
                    Resume Upload
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click"
                        Font-Bold="True" Font-Size="Medium" Width="200px" />
                </td>
            </tr>
        </table>
    </fieldset>
    </form>
</body>
</html>



Code behind(c#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;

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

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {

        try
        {
            MailMessage Msg = new MailMessage();
            //Sender e-mail address.
            Msg.From = new MailAddress(txtemail.Text);
            //Recipient e-mail address.
            Msg.To.Add("sender@gmail.com");
            //Meaages Subject
            Msg.Subject = "Career with-Neerajcodesolutions";
            StringBuilder sb = new StringBuilder();
            sb.Append("Name :" + txtname.Text + "\r\n");
            sb.Append("Contact:" + txtcontact.Text + "\r\n");
            sb.Append("Email:" + txtemail.Text + "\r\n");
            sb.Append("Message:" + txtmessage.Text + "\r\n");
            if (FileUpload1.HasFile)
            {
                string FileName = FileUpload1.PostedFile.FileName;
                Msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));

            }
            Msg.Body = sb.ToString();
            // SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 25;
            smtp.Credentials = new System.Net.NetworkCredential("sender@gmail.com", " password ");
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            //Mail Message
            Response.Write("<Script>alert('Thanks for posting your resume us,our team will be contact you as soon as possible')</Script>");
            // Clear the textbox values
            txtname.Text = "";
            txtcontact.Text = "";
            txtemail.Text = "";
            txtmessage.Text = "";
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);

        }
    }
}


Out-Put:-

Career form with attachment via email in asp.net c# Career form with attachment via email in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 9:46:00 PM Rating: 5

4 comments:

  1. Hi NEERAJ SRIVASTAVA
    I am shifana. This code is not working can you help me to create the contact form

    ReplyDelete
  2. Showing this error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Plz help me to fix the error

    ReplyDelete
    Replies
    1. go to https://www.google.com/settings/security/lesssecureapps
      and turn security on

      Delete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete

Powered by Blogger.