I have feedback form on my mvc site, it looks like
and I need to send my form to email.
I created model for my form
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ComponentTrading.Web.Models
{
public class FeedbackForm
{
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Message { get; set; }
}
}
I created view for my form Contacts.cshtml
@using (Html.BeginForm("Contacts", "Home", FormMethod.Post, new { id = "contact-form" }))
{
<fieldset>
@Html.TextBoxFor(model => model.Name, new { @Value = Name })
@Html.TextBoxFor(model => model.Email, new { @Value = "E-mail" })
@Html.TextBoxFor(model => model.Phone, new { @Value = Phone })
@Html.TextAreaFor(model => model.Message, new { @class = "img-shadow" })
<input class="form-button" data-type="reset" value="Clear" />
<input class="form-button" data-type="submit" type="submit" value="Send" />
</fieldset>
}
And I wrote this code at my controller
[HttpGet]
public ActionResult Contacts()
{
FeedbackForm temp = new FeedbackForm();
temp.Message = @Resources.Global.Contacts_Form_Message_Field;
return View(temp);
}
[HttpPost]
public ActionResult Contacts(FeedbackForm Model)
{
string Text = "<html> <head> </head>" +
" <body style= \" font-size:12px; font-family: Arial\">"+
Model.Message+
"</body></html>";
SendEmail("tayna-anita@mail.ru", Text);
FeedbackForm tempForm = new FeedbackForm();
return View(tempForm);
}
public static bool SendEmail(string SentTo, string Text)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("Test@mail.ru");
msg.To.Add(SentTo);
msg.Subject = "Password";
msg.Body = Text;
msg.Priority = MailPriority.High;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.mail.ru", 25);
client.UseDefaultCredentials = false;
client.EnableSsl = false;
client.Credentials = new NetworkCredential("TestLogin", "TestPassword");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;
try
{
client.Send(msg);
}
catch (Exception)
{
return false;
}
return true;
}
I dont have any errors at my code, but when I push Send button, I dont get email and my form doesnt become clear.
I used Fiddler Web Debugger and I got for Send-button click
As I see, it means all right, so I cant undersnand what's wrong?
Change
To
I have copy-pasted your code and got it to work, with some small modifications:
Credentials
propertyI modified the template to remove the @Value attribute:
@Html.TextBoxFor(model => model.Name) @Html.TextBoxFor(model => model.Email) @Html.TextBoxFor(model => model.Phone)
These are the only changes I made and I got your application to work. This leads me to believe that there must be something wrong in your SMTP client's configuration. Can you examine the exception that is thrown by
client.Send(msg);
? I expect it will say something about invalid network credentials or something like that.