Add form to subscribe to the newsletter in c#

Asked

Viewed 132 times

-2

Viva. I’m developing a website in c#, Asp.net using visual studio. I would like to place a small form where the website viewer places his email to subscribe to a newsletter. I’ve researched ways to do it but I haven’t found any correct ones. I don’t know how it works, if other tools need resources, etc..

Something like -> (made on WIX.com)

Code used to highlight this operation:

MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient();            
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.To = "[email protected]"; //
mail.From = "[email protected]";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
  • Try to improve your question. post what has already done so that someone can help you.

  • I have a whole site done, it’s just not in production yet. I’ve already edited the question with an example I did on the Wix site.com

1 answer

0

See if that’s what you want :

Model

public class Pessoa
{

    // No seu caso, acho que esse campo é opcional.
    public int Id { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    public string Email { get; set; }
}

Actions within the Controller

[HttpGet]
public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(Pessoa pessoa)
{
    if (ModelState.IsValid)
    {
        // Grava no banco ou o que quiser que seja feito.
    }

    return View();
}

View

@model SeuProjeto.Model.Pessoa

@using (Html.BeginForm("Index", "SeuController", FormMethod.Post))
{
<div class="form-group">
    <label>Nome</label>
    <input type="text" class="form-control" placeholder="Digite o nome">
</div>

<div class="form-group">
    <label>Email</label>
    <input type="email" class="form-control" placeholder="Digite o email">
</div>

<button type="submit" class="btn btn-primary">Enviar</button>
}
  • Sounds good. But how do I receive this information, for example, in my mail? Or in this case, in the company mail?

  • Put in the controller within the method that receives the post, the actions for sending email. See this link https://stackoverflow.com/questions/9201239/send-e-mail-via-smtp-using-c-sharp

  • I couldn’t.. It just doesn’t do anything! It’s because the question is old?

  • I don’t think so. I use the same mechanism until today, working normally. Have you changed the parameters to send email according to your environment ? Host, Port, etc...

  • I used the code I put in the question (edited) but it doesn’t work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.