0

I am looking for a way to send a email when i click the button on my view page.

On my view page i have a textfield where the user can fill in the e-mail from the receiver

 @Html.TextBoxFor(m => m.ReceiverMail, new { @placeholder="E-mail reveiver"})

The e-mail must be send to the the email which is filled in at the textfield after click button.

<input type="submit" class="btn btn-large btn-success" value="Send mail" />

The sender can fill in his email also in a textfield

@Html.TextBoxFor(m => m.SenderMail, new { @placeholder="E-mail sender"})    

The text of the mail can be something like: "This is a test e-mail from (SenderMail)" Who can give me a simple example of this?

Java Afca
  • 2,217
  • 4
  • 16
  • 10

2 Answers2

0

Create a controller with a action post.

[HttpPost]
public ActionResult myaction(string ReceiverMail)
{
    if (ModelState.IsValid)
    {
        // ..

        new mailController().sendMail(ReceiverMail).Deliver(); // Send mail
        return RedirectToRoute("Usuario_default", new RouteValueDictionary { { "controller", "home" }, { "action", "logon" } });
    }

    Response.StatusCode = (int)HttpStatusCode.BadRequest;
    return View(viewModel);
}

Using ActionMailer to send emails.

ridermansb
  • 10,779
  • 24
  • 115
  • 226
0

First of all you can create controller extension method:

  public static partial class ControllerExtensions
    {
        public static string RenderPartialViewToString(this ControllerBase controller, string partialPath, object model)
        {
            if (string.IsNullOrEmpty(partialPath))
                partialPath = controller.ControllerContext.RouteData.GetRequiredString("action");

            controller.ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialPath);
                ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                // copy model state items to the html helper 
                foreach (var item in viewContext.Controller.ViewData.ModelState)
                    if (!viewContext.ViewData.ModelState.Keys.Contains(item.Key))
                    {
                        viewContext.ViewData.ModelState.Add(item);
                    }


                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }
    }

Then, create a controller action method which receives SenderEmail receiverEmail. Then create template variable

   var template= this.RenderPartialViewToString("MailToSend", model);

This will convert View MailToSend into string.

After that you can use MailMessage class to send email:

       SmtpClient client = new SmtpClient();
       client.Host = "smtp.gmail.com";  //if your email is gmail
       client.Port = 25;
       client.UseDefaultCredentials = false;
       client.Credentials = smtpCrede;
       client.EnableSsl = true;

       MailMessage message = new MailMessage();
       message.From = new MailAddress(here goes email");
       message.To.Add(new MailAddress(email));
       message.IsBodyHtml = true;
       message.Body = template;
       client.Send(message);
       return true;

And also declare :

      private NetworkCredential smtpCrede = new NetworkCredential("your email, "your email password"); 

outside of action method.

Vlado Pandžić
  • 4,879
  • 8
  • 44
  • 75