asp.net using mvc 3 sending feedback via email - asp.net-mvc-3

I have a problem with sending feedback through email.. sending feedback is good i can receive the feedbacks. but i cant see who is the sender... it always indicate that the email is being sent by myself also... help pls.
heres my code:
[HttpGet]
public ActionResult Feedback()
{
return View();
}
[HttpPost]
public ActionResult Feedback(string email, string subject, string body)
{
try
{
WebMail.SmtpServer = "smtp.gmail.com";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "aaaa#gmail.com";
WebMail.From = email;
WebMail.Password = "12345";
WebMail.Send("aaaa#gmail.com",subject,body,email);
return RedirectToAction("FeedbackSent");
}
catch (Exception ex)
{
ViewData.ModelState.AddModelError("_FORM", ex.ToString());
}
return View();
}
public ActionResult FeedbackSent()
{
return View();
}
tnx for those who can help.... the email and password indicate are not legitimate.

it always indicate that the email is being sent by myself
That's normal. You cannot send an email on behalf of someone else. You could include a FirstName, LastName and Email fields in the feedback form asking the users to fill them. And if the users are kind enough to fill this information you could include it in the body of the email that is being sent.
UPDATE:
As requested in the comments section here's what you could do. Use the email field to include the information in the body:
WebMail.Send(
"aaaa#gmail.com",
subject,
string.Format("{0} wrote the following feedback: {1}", email, body),
email
);

Related

ASP.NET HTTP POST and Redirect

I have users that will be directed to my application from an external site, and during this redirecting, user's email addresses will be sent to me by HTTP POST.
What I'm trying to accomplish is to receive user's email address, and send the user to the Index view.
I have the following methods:
[HttpGet]
public ActionResult Index()
{
string email = "";
string[] keys = Request.Form.AllKeys;
for (int i = 0; i < keys.Length; i++)
{
Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
System.Diagnostics.Debug.WriteLine(keys[i] + ": " + Request.Form[keys[i]]);
email = Request.Form[keys[i]].ToString();
}
return Index(email);
}
[HttpPost]
public ActionResult Index(string email)
{
return View();
}
And this is the method I have to mimic the external site's job, which was redirecting user to my view with user's email address.
public ActionResult Httppost()
{
using (var wb = new WebClient())
{
var data = new NameValueCollection();
data["email"] = "test#email.com";
var response = wb.UploadValues("http://localhost:57695/Home/Index", "POST", data);
}
return RedirectToAction("Index");
}
The problem is, Index never receives the email address from my Httppost() method, and email is always received as "". How can I receive the email from Httppost() into Index()?
Why are you sending the email address in a separate request? These two requests have nothing to do with one another. The data received in one isn't available to the other, and they're not guaranteed to happen in the same order.
Instead of this:
var response = wb.UploadValues("http://localhost:57695/Home/Index", "POST", data);
return RedirectToAction("Index");
Just send the value in one request so that it will be available in that request:
return RedirectToAction("Index", new { email = "test#email.com" });
Then you only need one action:
[HttpGet]
public ActionResult Index(string email)
{
// use the email parameter
}
Side note: I don't think this is going to do what you expect:
return Index(email);
That's going to look for a view named after the value of the email variable. Because of how method overloading works, you can't really use a string by itself as a model. You're going to want to either set it on an actual model or put it in something like ViewBag instead.

The specified string is not in the form required for an e-mail addressThe specified string is not in the form required for an e-mail address

The specified string is not in the form required for an e-mail addressThe specified string is not in the form required for an e-mail address
return Email("SampleEmail", model);
am trying to send emails automatically when anyone register with him email
here is the code
Mail Controller
public class MailController : MailerBase
{
public EmailResult SampleEmail(Users model)
{
From = "no-reply#mysite.com";
To.Add(model.Email);
Subject = "Welcome To Our Site ";
return Email("SampleEmail", model);
}
this is the UserContoller
public ActionResult Create()
{
var user = new Users
{
UserName = "user.UserName",
Email = "user.Email"
};
new MailController().SampleEmail(user).DeliverAsync();
return View();
}

ASP.Net MVC Validation (server side)

asp.net mvc server side validation when the javascript is disabled in the browser? i used "remote" in my modal class it validates only when the javascript is enabled it doesnt validate when the javascript is disabled.
Scenario for my problem is i have a table in my db with a column "code" with the datatype varchar. any one inserts the data they must insert the unique code.
Please do help me out
I would suggest to forget about remote because if you are using code first entity framework, you can't have more that one unique column in your table. I would just write code for it like this:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Insert a new user into the database
using (UsersContext db = new UsersContext())
{
UserProfile email = db.UserProfiles.FirstOrDefault(u => u.Email.ToLower() == model.Email.ToLower());
try
{
// Check if email already exists
if (email == null)
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email });
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("Email", "Email address already exists. Please enter a different email address.");
}
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
}
Replace the email with the property you want to validate. At post, this will compare with entries with what already exists in your database, and depending on results, it will give you feedback. Throws exception if such data exists.

ASP.net MVC 3 customizing Html.ValidationMessageFor based on model logic

I've been reading over several of the questions similar to this, dealing with customizing the #Html.ValidationMessageFor but none of them touched on what I'm looking to do.
The current form I'm working on is editing a user in a database. Within this form I need to check that the email being entered is not already used for another user. I have the logic, but what I don't have is the custom validation message to appear on the page if they use an already in-use email.
Controller code:
[HttpPost]
public ActionResult EditUser(int id, EditUserModel model)
{
if (ModelState.IsValid)
{
tbl_Users editedUser = tblUsers.EditUser(id, model, HttpContext.User.Identity.Name);
tblHSDA.EditHSDAS(id, editedUser, model.hsdas, HttpContext.User.Identity.Name);
return Redirect("~/UserManage/ListActiveUsers");
}
if (tblUsers.ValidateEmailInUse(model.Email))
{
// change validation message and return View(model);
}
tbl_Users tbl_users = db.tbl_Users.SingleOrDefault(item => item.User_id == id);
ViewBag.hsdas = tblHSDA.GetHSDANameAlpha();
ViewBag.Username = tbl_users.Username;
return View(model);
}
Is this something done at the Controller level?
as per your logic the email check part will never execute if the user fills in the form correctly and provides a duplicate email
what you can do is change the ActionResult like
[HttpPost]
public ActionResult EditUser(int id, EditUserModel model)
{
if (ModelState.IsValid)
{
if(!CheckEmail(model.Email)){
tbl_Users editedUser = tblUsers.EditUser(id, model, HttpContext.User.Identity.Name);
tblHSDA.EditHSDAS(id, editedUser, model.hsdas, HttpContext.User.Identity.Name);
return Redirect("~/UserManage/ListActiveUsers");
}else{
ModelState.AddModelError("Email","Email provided is already in use...")
}
}
tbl_Users tbl_users = db.tbl_Users.SingleOrDefault(item => item.User_id == id);
ViewBag.hsdas = tblHSDA.GetHSDANameAlpha();
ViewBag.Username = tbl_users.Username;
return View(model);
}
private bool CheckEmail(string email){
//email check logic
// return true or false
}
also have a look at http://msdn.microsoft.com/en-us/library/gg508808%28v=vs.98%29.aspx

Using MVCMailer with ASPNETDB

I am trying to setup MVCMailer to use the Email and UserId that were setup when they registered with the website. I used the stock MVC framework to setup the user registration framework and thus the ASPNETDB.MDF DB is used to keep track of email userID and password. I am stuck at the moment on how to pull the current user id and email out of the DB so MVCMailer knows where to send the email with the password reset instructions. Below is my attempt at getting the data out of the DB and into the send mail function. I know I am wrong because I have red squiggles below the word membership.
public virtual MailMessage Welcome(string ADID)
{
var mailMessage = new MailMessage{Subject = "Welcome"};
mailMessage.To.Add(membership.Email);
ViewBag.Name = membership.UserId;
PopulateBody(mailMessage, viewName: "Welcome");
return mailMessage;
}
Does anyone have any idea how to do this I have been searching for a couple hours now on how to accomplish this with no luck. The tutorials I have looked at all seem to use hard coded values for where to send an email. Please let me know if you need any other code and Thanks for your help!
You could fetch it by querying your membership provider:
public virtual MailMessage Welcome(string ADID)
{
var mailMessage = new MailMessage{ Subject = "Welcome" };
var user = Membership.GetUser();
mailMessage.To.Add(user.Email);
ViewBag.Name = user.UserName;
PopulateBody(mailMessage, viewName: "Welcome");
return mailMessage;
}
another possibility is to send the email from a controller action and thus pass the information about the currently connected user as parameter:
using Mvc.Mailer;
public class HomeController: Controller
{
private readonly UserMailer _mailer = new UserMailer;
public ActionResult Index()
{
return View();
}
public ActionResult SendEmailToNewUser()
{
var user = Membership.GetUser();
_mailer.Welcome(user.Email, user.UserName).Send();
return View();
}
}

Resources