Send email in asp.net,html - html

This is my index.aspx form.
<form role="form" method="post" action="SendMail.aspx">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name"
required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number"
required>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Email"
required>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">
Submit Form</button>
</form>
And this is my SendMail.aspx form.
<%
Response.Write("Need : " & Request.Form("whatneed") & "<br>")
Response.Write("Budget : " & Request.Form("budget") & "<br>")
Response.Write("When : " & Request.Form("whenneed") & "<br>")
Response.Write("Location : " & Request.Form("location") & "<br>")
Response.Write("Name : " & Request.Form("name") & "<br>")
Response.Write("Description : " & Request.Form("address") & "<br>")
Response.Write("Mobile No : " & Request.Form("mobile") & "<br>")
Response.Write("Landline No : " & Request.Form("landline") & "<br>")
Response.Write("Email Id : " & Request.Form("email") & "<br>")
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("saravana17.ams#gmail.com");
mailMessage.From = new MailAddress("saro17.ams#gmail.com");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient = new SmtpClient("mail.feo.co.in");
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
%>
I don't know why mail is not sending here.Please help me to fix this.

As per your JsFiddle, I found that there are so many silly mistakes in your HTML code.
Here is your ASP.NET code:-
<form id="form1" runat="server">
<div class="form-group">
<asp:TextBox ID="txtname" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox ID="txtmobileno" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox ID="txtEmail" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox ID="txtSubject" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<asp:Button ID="btnSubmit" runat="server" CssClass="btn btn-primary pull-right" OnClick="btnSubmit_OnClick" Width="100" Text="Submit" />
</form>
Code behind cs code
Created a SendMail() function which will fire on buttonclick
Note:- I haven't added validations on the controls, so if you want it you can add as per your requirement.
protected void SendMail()
{
// Gmail Address from where you send the mail
var fromAddress = "Gmail#gmail.com";
// any address where the email will be sending
var toAddress = txtEmail.Text.ToString();
//Password of your gmail address
const string fromPassword = "Your gmail password";
// Passing the values and make a email formate to display
string subject = txtSubject.Text.ToString();
// Passing the values and make a email formate to display
string body = "From: " + txtname.Text + "\n";
body += "Email: " + txtEmail.Text + "\n";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
Now the above function will be called on Button click so that every time you enter the details you can call that function.
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
try
{
SendMail(); // Send mail function to send your mail
txtname.Text = "";
txtEmail.Text = "";
txtmobileno.Text = "";
txtSubject.Text = "";
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
For a detail explanation have a look at below link:-
http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp

Related

Input type submit not redirecting to form action

I created a form that can send an email, but it is not redirecting to the php script when hitting submit.
Here is the form by the way
Code:
<form method="post" id="myForm" name="myForm" action="mail_handler.php">
<div class="row">
<div class="col-lg-12">
<div class="single-form">
<label>* FULL NAME</label> <input class="form-control" type="text" name="full_name" id="full_name" placeholder="Enter full name" required="">
</div>
</div><!-- Nice Select -->
<!-- First Name -->
<div class="col-lg-6">
<div class="single-form">
<label>* Email Address</label> <input class="form-control" type="email" name="emailAdd" id="emailAdd" placeholder="Enter email" required="">
</div>
</div>
<div class="col-lg-4">
<div class="single-form">
<input type="submit" name="submit" value="Submit" class="btn apply-btn mt-30">
</div>
</div>
</div>
</form>
and here is the mail_handler.php.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
if(isset($_POST['submit'])){
if(!empty($_POST['full_name']) && !empty($_POST['emailAdd'])){
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.mail.yahoo.com"; // sets yahoo as the SMTP server
$mail->Port = 465; // set the SMTP port
$mail->Username = "my username"; // yahoo username
$mail->Password = "my pass"; // yahoo password
$full_name = $_POST['full_name'];
$emailAdd = $_POST['emailAdd'];
$subject = "Webpage Client Feedback";
$to = "cremlicofficial#yahoo.com";
$subject = "WEBPAGE: PRE-APPLICATION";
$mail->From = "cremlicit#yahoo.com";
$mail->FromName = "CREMLIC ADMIN";
$mail->AddAddress("cremlic#yahoo.com");
$mail->AddCC('cegabrillo#yahoo.com.ph');
$mail->AddCC('ksgaro#yahoo.com');
$mail->Subject = "WEBPAGE:PRE-APPLICATION";
$mail->Body = "To Whom it may concern, \r\n"
. "A visitor submits a pre-application information, please see details below. \r\n \r\n"
. "Name: " . $full_name . " \r\n"
. "Email: " . $emailAdd;
$mail->AddEmbeddedImage('./images/1593998909.jpg', 'palogo', '1593998909.jpg');
if(!$mail->Send()) {
echo '<script type="text/javascript">alert("Message was not sent. \r\nMailer error: '.$mail->ErrorInfo.'");window.history.go(-1);</script>';
}
}else{
echo '<script type="text/javascript">alert("Error Occured! \r\nPlease fill up fields: Name, Address and Contact No.");window.history.go(-1);</script>';
}
}
?>
I am 100% sure that both HTML file and PHP script is on the same folder. Is there any problem with these line of codes?
The form is working fine, kindly check the location of mail_handler.php if its in the same folder or in different folder. Also share mail_handler.php code so we can check if there's any issue with the code.

How to display the response data in a input text field of a form in a html?

I am really new to node.js and do have a problem. I want to show the data from the logged in user in these inputfields.
I can access to the data of the logged in user by following code and can show it also on the console:
Output on console
Here is the code on the server-side:
app.post('/aender', function (req, res) {
res.send(req.session.username);
res.send(req.session.name);
res.send(req.session.email);
var sql = "UPDATE user SET name ='" + [req.body.name] + "', username='" + [req.body.username] + "', email='" + [req.body.email] + "', password='" + [req.body.password] + "' WHERE user_ID='" + [req.session.user_id] + "'";
if(req.body.password === req.body.password2) {
db.query(sql, (err, result) => {
if (err) throw err;
console.log("Daten erfolgreich geaendert");
res.redirect('/aender');
});
}else{
res.redirect('/aender');
}
});
Here is the HTML-Code:
<!doctype html>
<html>
<h2 class="page-header">Registrierung</h2>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="../app.js"></script>
<form id ="form1" action="/aender" method="post">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" name="name" id ="name">
</div>
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" placeholder="username" name="username" id ="username">
<script>
displayUserData()
</script>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" placeholder="Email" name="email" id ="email" pattern="(^[a-zA-Z0-9_-]{1,20}#[a-zA-Z0-9]{1,20}.[a-zA-Z]{2,3}$)">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" placeholder="Password" name="password" pattern="(?=.*[a-zA-Zd!##$%^&*()-_+={}[]|;:<>,./?]).{6,}">
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" class="form-control" placeholder="Password" name="password2" pattern="(?=.*[a-zA-Zd!##$%^&*()-_+={}[]|;:<>,./?]).{6,}">
</div>
<button type="submit" name="submit" class="btn btn-default">Profil ändern</button>
</form>
<form id ="form2" action="/loeschen" method="post">
<button type="submit" name="submit" class="btn btn-default">Konto löschen</button>
</form>
</html>
For example I do want to display the name, username and the e-mail of Max Mustermann, like that:
Data on Form
What you want to do is called interpolation and it is not directly possible in HTML. There are two possible options:
Create an API REST service omn the server side and do your call with ajax or jquery and then retrieve data (not the best option if you are not planning to have it for other purposes and not the best option in terms of security).
You can consider to switch to pug, which can be totally integrated with nodejs. Here is a practical example of interpolation with jade, which is the predecessor of pug.
if you want to do this with ajax you've first to change server logic:
app.post('/aender', function (req, res) {
var sql = "UPDATE user SET name ='" + [req.body.name] + "', username='" + [req.body.username] + "', email='" + [req.body.email] + "', password='" + [req.body.password] + "' WHERE user_ID='" + [req.session.user_id] + "'";
if(req.body.password === req.body.password2) {
db.query(sql, (err, result) => {
if (err) throw err;
console.log("Daten erfolgreich geaendert");
res.json({username:req.session.username,name:req.session.name,email:req.session.email});
});
}else{
res.redirect('/aender');
}
});
(Please note that i have just corrected the function in order to do that, but your logic is still broken, there's no sense at all to pass the session parameters).
Then in your html:
<script type="text/javascript">
$.post(/*hostname+*/ "/aender", function( data ) {
console.log(data); // {username:req.session.username,name:req.session.name,email:req.session.email}
})
</script>
This is not a full complete script, but it is a good start for you to adapt.

How can I sent mail to multiple receivers with html 5, asp.net (without PHP)

I write web site and I have a contact form for send mail.I use Formspree to send mail,it works for one receiver.But I want send email to multiple receivers.I cant find any information.Please help me.
Thanks..
My html code is like as below:
<form id="contact" action="https://formspree.io/emailname#gmail.com" method="POST">
<div class="form-group">
<input type="text" name="contact_author" class="form-control" id="exampleInputEmail1" placeholder="Enter Name">
</div>
<div class="form-group">
<input type="email" name="_replyto" class="form-control" id="exampleInputPassword1" placeholder="Enter Email">
</div>
<div class="form-group">
<input type="text" name="contact_subject" class="form-control" id="exampleInputEmail2" placeholder="Enter Subject">
</div>
<div class="form-group">
<textarea name="contact_content" class="form-control" rows="4" placeholder="Message"></textarea>
</div>
<div id="gglcptch_recaptcha_760104045" class="gglcptch_recaptcha">
</div>
<button type="submit" class="btn btn-primary my-btn dark">Submit</button>
</form>
Is there anybody use this method, please share with me..
You can use this
SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("info#MyWebsiteDomainName.com", "myIDPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//Setting From , To and CC
mail.From = new MailAddress("info#MyWebsiteDomainName", "MyWeb Site");
mail.To.Add(new MailAddress("info#MyWebsiteDomainName"));
mail.CC.Add(new MailAddress("MyEmailID#gmail.com"));
smtpClient.Send(mail);
You could always just loop through the different addresses and keep
private List<string> Addresses = new List<string>
You can then fill the list however you like. I just add them manually here for the sake of simplicity:
protected void Page_Load(object sender, EventArgs e)
{
Addresses.Add("FirstEmail");
Addresses.Add("SecondEmail");
// Now loop through the addresses in the list and send an email for each one
foreach (string s in Adresses)
{
SendMail(s);
}
}
private void SendMail(string Receiver)
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(Receiver);
mailMessage.From = new MailAddress("another#mail-address.com");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient = new SmtpClient("smtp.your-isp.com");
smtpClient.Send(mailMessage);
}
Hope this helps!

ASP Razor Web Pages - Form Submit is Moving My Data

When I submit my Login email (aka username), I want to keep it in my Login form.
When I submit my Register email (aka username), I want to keep it in my Register form.
What is happening is: when I submit my Login form, it writes the email (aka username) to the Register form. Please see image for what is happening.
#{
Page.Title = "Login";
var username = "";
var password = "";
var errorMessage = "";
var confirmPassword = "";
var regMsg = "";
var confToken = "";
var emailBody = "";
var minPass = 2;
var maxPass = 5;
if (!IsPost) {
if (WebSecurity.IsAuthenticated) {errorMessage = String.Format("You are already logged in. (Username: {0})", WebSecurity.CurrentUserName);}
}
if(WebSecurity.IsAuthenticated){
errorMessage = String.Format("You are already logged in. (Username: {0})", WebSecurity.CurrentUserName);
}
if(IsPost){
// Login Form
if (Request.Form["loginSub"] != null){
regMsg = "";
username = Request["username"];
password = Request["password"];
if(WebSecurity.Login(username,password,true)){
Response.Redirect("~/Profile");
}
else
{
errorMessage = "Invalid Username or Password. Please try again.";
}
}
// Register Form
if (Request.Form["registerSub"] != null){
WebSecurity.Logout();
errorMessage = "";
username = Request["username"];
password = Request["password"];
confirmPassword = Request["confirmPassword"];
try {
var mail = new System.Net.Mail.MailAddress(username);
} catch {
regMsg += "<br>Invalid email format.";
}
if (password != confirmPassword) {regMsg += "</br>Passwords don't match.";}
if (WebSecurity.UserExists(username)) {regMsg += String.Format("</br>User '{0}' already exists.", username);}
if (password.Length < minPass || password.Length > maxPass) {regMsg += "</br>Password doesn't meet length requirement.";}
if (regMsg == "") {
WebSecurity.CreateUserAndAccount(username,password,null,false); //Switch with "true" token
regMsg = String.Format("{0} created.", username);
Response.Write("<script>alert('Email verification sent! Please check your email to activate your account.');</script>");
//Response.Write("<script>location.href = 'Default.cshtml';</script>");
}
}
}
}
<style>header {visibility: hidden;}</style>
<body>
<h1>MySite</h1>
<p>
#if(errorMessage != ""){<span>#Html.Raw(errorMessage)</span>}
#if(regMsg != ""){<span>#Html.Raw(regMsg)</span>}
</p>
<fieldset class="fs100">
<legend>Login</legend>
<form method="post" name="login">
#if(WebSecurity.IsAuthenticated){
<p>You are currently logged in as #WebSecurity.CurrentUserName.
Log out
</p>
}
<p>
<label for="username">Email Address:</label><br/>
<input type="text" name="username" id="username" value="" />
</p>
<p>
<label for="password">Password:</label><br/>
<input type="password" name="password" id="password" value="" />
</p>
<p>
<input type="submit" name="loginSub" value="Submit" />
</p>
<p>Forgot Password?</p>
</form>
</fieldset>
<fieldset>
<legend>Register</legend>
<form method="post" name="register">
<p>
<label for="username">Email Address:</label><br/>
<input type="text" name="username" id="username" value='#Request["username"]' />
</p>
<p>
<label for="password">Password #minPass-#maxPass Characters:</label><br/>
<input type="password" name="password" id="password" value="" />
</p>
<p>
<label for="confirmPassword">Confirm Password:</label><br/>
<input type="password" name="confirmPassword" id="confirmPassword" value="" />
</p>
<p>
<input type="submit" name="registerSub" value="Register" />
</p>
</form>
</fieldset>
</body>
Your code is echoing the value from the request in the second form.
If you don't want it to do that, change your code to do something else.
You may want to change each form to have unique field names.

update row in mysql table in jsp

i´m trying to update rows in mysql table, i´m using html form for data insertion. In html form attribute value i´m using an existing data from database.
Edit.jsp
<form action="Update.jsp">
<% Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select u.login,i.name,i.surname,i.age,i.tel,i.email,a.street,a.town,a.zip,ul.name,t.name from users u join info i on i.user_id=u.user_id join user_level ul on ul.ulevel=u.ulevel join teams t on t.team_id=u.team_id join adress a on a.info_id=i.info_id where u.login='" + session.getAttribute("uzivatel") + "'");
while (rs.next()) {
%>
<div class="well well-large">
<font size="2"><b>Welcome, </b></font><font color="RED"><i><%= session.getAttribute("uzivatel")%></i></font><br>
<b>Name:</b> <input type="text" name="name" value="<%= rs.getString(2)%>"><input type="text" name="surname" value="<%= rs.getString(3)%>"> <br>
<b>Age:</b> <input type="text" name="age" value="<%= rs.getString(4)%>"><br>
<b>Telephone:</b> <input type="text" name="tel" value="0<%= rs.getString(5)%>"><br>
<b>E-mail:</b> <input type="text" name="email" value="<%= rs.getString(6)%>"><br>
<b>Adress:</b> <input type="text" name="street" value="<%= rs.getString(7)%>"><input type="text" name="town" value="<%= rs.getString(8)%>"><input type="text" name="zip" value="<%= rs.getString(9)%>"><br>
<b>User level:</b> <%= rs.getString(10)%><br>
<b>Team:</b> <%= rs.getString(11)%><br>
<input type="submit" name="Submit" value="Update" />
</div>
</form>
Update.jsp
<%
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String age = request.getParameter("age");
String telephone = request.getParameter("tel");
String email = request.getParameter("email");
String street = request.getParameter("street");
String town = request.getParameter("town");
String zip = request.getParameter("zip");
try {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st1 = null;
st1 = conn.createStatement();
System.out.println(session.getAttribute("uzivatel"));
st1.executeUpdate("UPDATE users JOIN info ON users.user_id = info.user_id"
+ " JOIN adress ON info.info_id = adress.info_id"
+ "SET info.name = '"+name+"',info.surname = '"+surname+"',"
+ "info.age = '"+age+"',info.tel = '"+telephone+"',info.email = '"+email+"',"
+ "adress.street = '"+street+"',adress.town = '"+town+"',adress.zip = '"+zip+"',"
+ "WHERE users.login ='" + session.getAttribute("uzivatel") + "' ");
response.sendRedirect("AdministrationControlPanel.jsp");
} catch (Exception e) {
System.out.println(e.getMessage());
}
%>
When I pressed the Submit button, it redirected me to Update.jsp and nothing was changed.
There is problem in your query that is why you are not getting any result. give space at properly when you are dividing the string in various lines.
system.out.println() will show message on the console not on the page
st1.executeUpdate("UPDATE users JOIN info ON users.user_id = info.user_id"
+ " JOIN adress ON info.info_id = adress.info_id "
+ "SET info.name = '"+name+"',info.surname = '"+surname+"', "
+ "info.age = '"+age+"', info.tel = '"+telephone+"', info.email = '"+email+"',"
+ "adress.street = '"+street+"',adress.town = '"+town+"',adress.zip = '"+zip+"', "
+ "WHERE users.login ='" + session.getAttribute("uzivatel") + "' ");
Better you use jQuery rather than calling page update.jsp. When you use jQuery that time you can send data to update.jsp page with parameter and take response from that page and print it in you page even you can take all table data from the jQuery
It is very simple Hope this will help you.. for sample example have look of following urls
demo
http://www.roseindia.net/tutorial/jquery/jqDatabaseConnect.html
http://api.jquery.com/jQuery.post/