Sending form to email in webmatrix - asp.net

I have a question regarding emails, i want to send the whole contact form to email, and in this tutorial http://www.asp.net/web-pages/tutorials/email-and-search/11-adding-email-to-your-web-site it has almost everything except this line in code
// Send email
WebMail.Send(to: customerEmail,
subject: "Help request from - " + customerName,
body: customerRequest
);
}
i do not understand how to edit it,now the thing is it is working but only sending me customerRequest in email because now there is a form with more details and it is only sending customerRequest part not email , number, items and other categories, so kindly assist how to send the whole form or other columns through this.
Thanks

The customerRequest variable can contain any string you want it to contain. In the tutorial, it represents the value of the customerRequest form field. You can add other fields to the form and use their values to build up the body of the email. For example, you can add a partNumber field:
Your name:
<div>
Your email address:
<input type="text" name="customerEmail" />
</div>
<div>
Part Number:
<input type="text" name="partNumber" />
</div>
<div>
Details about your problem: <br />
<textarea name="customerRequest" cols="45" rows="4"></textarea>
</div>
<div>
<input type="submit" value="Submit" />
</div>
And in the server-side code, add that to the body:
#{
var customerName = Request["customerName"];
var customerEmail = Request["customerEmail"];
var customerRequest = Request["customerRequest"];
var partNumber = Request["partNumber"];
var errorMessage = "";
var debuggingFlag = false;
//etc
}
This is how you could concatenate the values:
WebMail.Send(to: customerEmail,
subject: "Help request from - " + customerName,
body: "Part Number: " + partNumber + "\n\n" + customerRequest
);

Related

post id from view to controller through the url in thymeleaf

I want to find a user in my Spring-Boot application by its ID.
The Controller-method is:
#RequestMapping(value = "finduser/{id}", method = RequestMethod.GET)
public User get(#PathVariable Long id) throws NotFoundException {
log.info("Invoked method: get with ID: " + id);
log.warn("Searching for user with ID " + id);
User findOne = userRepository.findOne(id);
if (findOne == null){
log.error("Unexpected error, User with ID " + id + " not found");
throw new NotFoundException("User with ID " + id + " not found");
}
log.info("User found. Sending request back. ID of user is " + id);
return findOne;
}
The body of my finduser.html is:
<body>
<h1>Find User:</h1>
<form th:object="${user}" th:action="#{finduser}" method="post">
<p>Find by ID: <input type="text" th:field="*{id}" /></p>
<p><input type="submit" value="Submit" /></p>
</form>
</body>
What should i change to pass the id from the view to my controller, so my controller can use the id and search for a user?
I think the id must be passed through the url because of
#RequestMapping(value = "finduser/{id}"...)
you can specify the form action (doc):
You can also include parameters in the form of path variables
similarly to normal parameters but specifying a placeholder inside
your URL’s path:
<a th:href="#{/order/{id}/details(id=3,action='show_all')}">
in your case you can do like this for example:
<form th:action="#{/finduser/{id}(id=${user.id})}">
or if you prefer having the url in a variable:
<form th:action="#{{finduser}/{id}(finduser=${finduser},id=${user.id})}">

How to Create ASP.NET Registration Page

Hello I am currently using Web Matrix to create a Hotel Booking website. I have had some prior expereince using Visual Studio 2010, but changed to Web Matrix as it seems to have quite a lot of information online. Following a tutorial on W3Schools to help me get the website started I have completed the tutorial but my registration page seems to have some sort of error.
This is following code:
#{Layout = "Layout.cshtml";
// Initialize page
var email = "";
var password = "";
var confirmPassword = "";
var ErrorMessage = "";
// If this is a POST request, validate and process data
if (IsPost)
{
email = Request.Form["email"];
password = Request.Form["password"];
confirmPassword = Request.Form["confirmPassword"];
if (email.IsEmpty() || password.IsEmpty())
{ErrorMessage = "You must specify both email and password.";}
if (password != confirmPassword)
{ErrorMessage = "Password and confirmation do not match.";}
// If all information is valid, create a new account
if (ErrorMessage=="")
{
var db = Database.Open("Users");
var user = db.QuerySingle("SELECT Email FROM UserProfile
WHERE LOWER(Email) = LOWER(#0)", email);
if (user == null)
{
db.Execute("INSERT INTO UserProfile (Email) VALUES (#0)", email);
WebSecurity.CreateAccount(email, password, false);
// Navigate back to the homepage and exit
Response.Redirect("Default.cshtml");
}
else
{ErrorMessage = "Email address is already in use.";}
}
}
if (ErrorMessage!="")
{
<p>#ErrorMessage</p>
<p>Please correct the errors and try again.</p>
}
}
<h1>Register</h1>
<form method="post" action="">
<fieldset>
<legend>Sign-up Form</legend>
<ol>
<li>
<label>Email:</label>
<input type="text" id="email" name="email" />
</li>
<li>
<label>Password:</label>
<input type="password" id="password" name="password" />
</li>
<li>
<label>Confirm Password:</label>
<input type="password" id="confirmPassword" name="confirmPassword" />
</li>
<li>
<p><input type="submit" value="Register" /></p>
</li>
</ol>
</fieldset>
</form>
This is the corresponding part of the tutorial, W3School ASP.NET Tutorial
Any other useful information or tutorials that I could follow to get this system well in to development feel free to share, deadline for testing phase isn't so far away.
Thanks!

Paypal IPN Spring MVC - Java

My notify url is not working even it is already activated in paypal. Is there something wrong with my code in the form? i have the same notify url in paypal account. By the way, I'm using heroku,spring mvc. Thanks. I hope you can help me.
<form method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_donations"/>
<input type="hidden" name="business" value="1234businessaccount#gmail.com"/>
<input type="hidden" name="item_name" value="${student.lastname }, ${student.firstname}"/>
<input type="hidden" name="currency_code" value="PHP"/>
<input type="hidden" name="lc" value="PH"/>
<input type="hidden" name="rm" value="2"/>
<input type="hidden" name="return" value="http://atriev.herokuapp.com/"/>
<input type="hidden" name="cancel_return" value="http://atriev.herokuapp.com/atriev-index.html"/>
<input type="hidden" name="notify_url" value="http://atriev.herokuapp.com/paypal.html"/>
paypal.jsp
<%# page import="java.util.*" %>
<%# page import="java.net.*" %>
<%# page import="java.io.*" %>
<%
// read post from PayPal system and add 'cmd'
Enumeration en = request.getParameterNames();
String str = "cmd=_notify-validate";
while(en.hasMoreElements()){
String paramName = (String)en.nextElement();
String paramValue = request.getParameter(paramName);
str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue);
}
// post back to PayPal system to validate
// NOTE: change http: to https: in the following URL to verify using SSL (for increased security).
// using HTTPS requires either Java 1.4 or greater, or Java Secure Socket Extension (JSSE)
// and configured for older versions.
URL u = new URL("https://www.sandbox.paypal.com/cgi-bin/webscr");
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(str);
pw.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(uc.getInputStream()));
String res = in.readLine();
in.close();
// assign posted variables to local variables
String itemName = request.getParameter("item_name");
String itemNumber = request.getParameter("item_number");
String paymentStatus = request.getParameter("payment_status");
String paymentAmount = request.getParameter("mc_gross");
String paymentCurrency = request.getParameter("mc_currency");
String txnId = request.getParameter("txn_id");
String receiverEmail = request.getParameter("receiver_email");
String payerEmail = request.getParameter("payer_email");
if(res.equals("VERIFIED")) {
// check that paymentStatus=Completed
// check that txnId has not been previously processed
// check that receiverEmail is your Primary PayPal email
// check that paymentAmount/paymentCurrency are correct
// process payment
}
else if(res.equals("INVALID")) {
// log for investigation
}
else {
// error
}
%>

how to send email from web page in the most simple way with asp.net?

In my web site I have "contuct us" page and I want the manager able to reply via email
to the customers.
All the process need to be in the server side.
It is important to note that I works with Webmatrix development environment...
So considering my web development what is the simple way for me to add the ability to
send email?
My HTML code is:
foreach(var row in db.Query(displayApplicant,nameOfcustomer))
{
<tr>
<td class="dispExpertActScreen">#row.messegeID</td>
<td class="dispExpertActScreen">#row.name</td>
<td class="dispExpertActScreen">#row.email</td>
<td class="dispExpertActScreen">#row.isCustomer</td>
<td class="dispExpertActScreen">#row.userID</td>
<td class="dispExpertActScreen">#row.content</td>
<td class="dispExpertActScreen">answer the question</td>
<td class="dispExpertActScreen">remove</td>
</tr>
}
the javascript function: (to save details in hidden fields)
<script type="text/javascript">
function answerBox(messegeID,userID) {
var msg = prompt('answer to customer:');
document.getElementById('answer').value = msg;
document.getElementById('ansMode').value = 'true';
document.getElementById('msgID').value = messegeID;
document.getElementById('user').value = userID;
document.getElementById('ansMessege').submit();
}
</script>
the hidden fields:
<form method="post" id="ansMessege" style="font-size: medium; margin-top: 10%" dir="rtl">
<input type="hidden" name="answer" id="answer" value="">
<input type="hidden" name="msgID" id="msgID" value="">
<input type="hidden" name="user" id="user" value="">
<input type="hidden" name="ansMode" id="ansMode" value="">
</form>
the asp.net code:
<%# Import Namespace="System.Web.Mail" %>
#{
Layout = "~/_ManagerLayout.cshtml";
Page.Title = "Management Applications";
}
#{
var db = Database.Open("MyProjectSite");
var display="no";
var displayApplicant="";
var nameOfcustomer="";
var category="";
var yesNo="";
if(IsPost)
{
if(Request.Form["ansMode"] == "true")
{
var selectQuery="SELECT * FROM messegesFromCustomers";
var id=Request.Form["msgID"];
var msg=Request.Form["answer"];
var user=Request.Form["user"];
foreach(var row in db.Query(selectQuery))
{
if(row.messegeID == Convert.ToInt32(id))
{
if(row.isCustomer == "yes")// **send the messege to customer account**
{
var insertQuery="UPDATE messegesFromCustomers SET answer=#0 WHERE messegeID=#1";
db.Execute(insertQuery,msg,id);
Response.Write("<script>alert('your answer sent successfully');</script>");
break;
}
else
{
// **Send messege to Occasional customer via email**
// **here I want to add code for sending email...**
string from = "ofirhgy#gmail.com";
string to = "ofirhgy#gmail.com";
string subject = "Hi!";
string body = "How are you?";
SmtpMail.SmtpServer = "mail.gmail.com";
SmtpMail.Send(from, to, subject, body);
break;
}
}
}
}
}
}
As you see I try to use WebMail.Send(...) but I don't know how...
In addition to that I saw that someone wrote that there is need to add some code
to the Web.config or something like that....
thanks you for help.
Edit:I edit the code and I put the line:
<%# Import Namespace="System.Web.Mail" %>
in the top of the asp.net page but I get this error:
Parser Error Message: A space or line break was encountered after the "#" character. Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following "#" with no space in between.
Can anybody place for me the exact code?
First, we need to import the System.Web.Mail namespace:
<%# Import Namespace="System.Web.Mail" %>
Sending the message is a matter of calling SmtpMail.Send() with the following arguments: sender, recipient, subject and body. For example, we'd send an email in C# like this:
string from = "sender#example.com";
string to = "recipient#example.com";
string subject = "Hi!";
string body = "How are you?";
SmtpMail.SmtpServer = "mail.example.com";
SmtpMail.Send(from, to, subject, body);
SmtpMail.SmtpServer lets you specify the mail server used to deliver your message.
Content from here
I found a solution to that
In my code I added this and the email sent sucessfully:
if(isCustomer == "yes")
{
db.Execute(insertQuery,msg,id);
db.Execute(deleteQuery,id);
}
else
{
WebMail.SmtpServer = "smtp.gmail.com";
WebMail.EnableSsl = true;
WebMail.SmtpPort = 587;//25
WebMail.UserName = "my gmail user name";
WebMail.Password = "my gmail password";
WebMail.From = "my address of email";
// Send email
WebMail.Send(
to: email,
subject: "Message from "some name of site" - Site",
body: msg);
db.Execute(deleteQuery,id);
}

Using ASP.NET Razor Edit Data Page in WebMatrix

i have some problem here with Edit Data Using ASP.NET Razor in WebMatrix
i write this code for edit a data using the Update command but unfortunately it doesnt work :s :s
Razor code :
#{
{
var userId = Request["UserId"];
var db = Database.Open("intranet");
var query = "UPDATE Personne SET Demande = #0 WHERE UserId LIKE '%#1%'";
db.Execute(query,"refuser", userId);
}
}
the html code :
<form action="responsable.cshtml" method="post">
<input type="hidden" name="UserId" value="saadwafqui" />
<input type="submit" value="Oui" />
</form>
Your code is vulnerable to SQL injection. I would recommend you fixing this. Also you seem to be using some IsPost variable which is not quite clear where is being defined.
Example:
#{
var userId = Request["userid"];
var db = Database.Open("intranet");
var query = "UPDATE Personne SET Demande = #0 WHERE UserId LIKE '%' + #1 + '%'";
db.Execute(query, "refuser", userId);
}
Notice the syntax around the LIKE clause:
LIKE '%' + #1 + '%'
This will match all records that have UserId in the middle. If you wanted to match only records that the UserId starts with the value in the request:
LIKE '%' + #1
and if you wanted exact match simply use the = operator instead of a LIKE clause.
Also your markup looks completely broken. There's no window.location attribute. Maybe you meant something like this:
<form action="responsable.cshtml" method="post">
<input type="hidden" name="userid" value="saadwafqui" />
<input type="submit" value="Oui" />
</form>
or with a GET request if you prefer:
<form action="responsable.cshtml" method="get">
<input type="hidden" name="userid" value="saadwafqui" />
<input type="submit" value="Oui" />
</form>

Resources