Nowadays, sending an email is a small kind of task. We can send email in ASP.NET using a Local Server easily. But on the GoDaddy Server, a little bit of code should be changed, like Port Number, Hosting Server, and user credentials etc.
Following are the steps.
Note - This code will work only on GoDaddy Server, not on a Local Server.
Step 1 Create a new project.
Step 2 Add bootstrap CSS in the CSS folder.
Step 3 Create a webform aspx page.
Step 4 Design the form like below.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- <link href="css/bootstrap.min.css" rel="stylesheet" />
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div class="container">
- <div class="row">
- <div class="col-md-4 col-md-offset-4">
- <div class="panel panel-primary">
- <div class="panel-heading">
- <h3 class="panel-title">E-mail</h3>
- </div>
- <div class="panel-body">
- <label>Name</label>
- <asp:TextBox ID="txtname" runat="server" CssClass="form-control" placeholder="Name"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Provide Name" ControlToValidate="txtname" ForeColor="Red"></asp:RequiredFieldValidator><br />
- <label>Subject</label>
- <asp:TextBox ID="txtsubj" runat="server" CssClass="form-control" placeholder="Subject"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Provide Subject" ControlToValidate="txtsubj" ForeColor="Red"></asp:RequiredFieldValidator><br />
- <label>Mobile</label>
- <asp:TextBox ID="txtmob" runat="server" CssClass="form-control" placeholder="Mobile Number" MaxLength="12"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Provide Mobile Number" ControlToValidate="txtmob" ForeColor="Red"></asp:RequiredFieldValidator><br />
- <label>Email</label>
- <asp:TextBox ID="txtemail" runat="server" CssClass="form-control" placeholder="Email ex([email protected])"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please Provide Email-ID" ControlToValidate="txtemail" ForeColor="Red"></asp:RequiredFieldValidator><br />
- <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Please Proide Valid Email-ID" ControlToValidate="txtemail" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator><br />
- <label>Address</label>
- <asp:TextBox ID="txtaddress" runat="server" CssClass="form-control" placeholder="Address"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Please Provide Address" ControlToValidate="txtaddress" ForeColor="Red"></asp:RequiredFieldValidator><br />
- <asp:Button ID="Button1" runat="server" Text="Send Me" CssClass="btn btn-block btn-primary" OnClick="Button1_Click" />
- </div>
- </div>
- </div>
- </div>
- </div>
- </form>
- </body>
-
- </html>
Step 5
Add Server-side validation for every field. Use RequiredFieldValidator for checking the empty fields.
Step 6
Each FieldValidator needs to set the ControlToValidate in the properties.
Step 7
The Email Validation requires another validator like RegularExpressionValidator to check a valid Email-ID. Using ValidationExpression, we can check easily.
Step 8
Double click on "Send Me" button and add the below code.
- try {
- MailMessage msgs = new MailMessage();
- msgs.To.Add(txtemail.Text.Trim());
- MailAddress address = new MailAddress("[email protected]");
- msgs.From = address;
- msgs.Subject = "Contact";
- string htmlBody = @ " <
- !DOCTYPE html >
- <
- html >
- <
- head >
- <
- title > Email < /title> <
- /head> <
- body >
- <
- h1 > Hi welcome < /h1> <
- p > Thank you
- for register < /p> <
- /body> <
- /html>
- ";
- msgs.Body = htmlBody;
- msgs.IsBodyHtml = true;
- SmtpClient client = new SmtpClient();
- client.Host = "relay-hosting.secureserver.net";
- client.Port = 25;
- client.UseDefaultCredentials = false;
- client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
-
- client.Send(msgs);
- ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Your Details Sent Successfull.');", true);
- } catch (Exception ex) {}
Step 9
Run the application. You will get an error like the following.
WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
Step 10
So, add the below code in web.config.
- <appSettings>
- <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
- </appSettings>
Step 11
Run the application again. Now, you will get a form like the following.
Step 12
Just click the "Send Me" button. You will get an error message like below.
Step 13
Fill in all the fields and provide a valid Email-ID.
The mail is sent successfully.
That's it. Thank you for reading my blog.